pub struct DomainMap<T> {
data: Vec<(Domain, T)>,
}
Expand description
A map with domain name keys, with support for wildcards
A DomainMap<T>
holds “[presented identifiers]” (domain names possibly
with wildcards) as Domain
s. A DomainMap
can be indexed using a
Domain
, which stores either a “reference identifier” (for matching
methods, e.g. get
or get_mut
) or a “presented identifier” (for
equality-comparing methods, e.g. get_eq
or remove
).
Currently, this is implemented using an associative array, but this may change in the future.
§Examples
use links_domainmap::{Domain, DomainMap};
// Create a new `DomainMap` with `u32` values
let mut domainmap = DomainMap::<u32>::new(); // or `with_capacity()`
// Set a value for `example.com`
domainmap.set(Domain::presented("example.com")?, 5);
// Set a value for the wildcard domain `*.example.net`
domainmap.set(Domain::presented("*.example.net")?, 100);
// Get the value for the domain matching `example.com`
assert_eq!(domainmap.get(&Domain::reference("example.com")?), Some(&5));
// Get the value for the domain matching `foo.example.net`
assert_eq!(
domainmap.get(&Domain::reference("foo.example.net")?),
Some(&100)
);
// Get the value for the domain `*.example.net` (using `==` internally)
assert_eq!(
domainmap.get_eq(&Domain::presented("*.example.net")?),
Some(&100)
);
// Try to get the value for the domain matching `a.b.c.example.net`
assert_eq!(
domainmap.get(&Domain::reference("a.b.c.example.net")?),
None // Wildcards only work for one label
);
// Update the value for `example.com`
let old_value = domainmap.set(Domain::presented("example.com")?, 50);
assert_eq!(old_value, Some(5));
// Modify the value for the domain matching `foo.example.net`
let val = domainmap.get_mut(&Domain::reference("foo.example.net")?);
if let Some(val) = val {
*val += 1;
assert_eq!(val, &101);
}
// Set a value for `www.example.net`, overriding the wildcard `*.example.net`
domainmap.set(Domain::presented("www.example.net")?, 250);
// The wildcard still exists, but is overridden for `www.example.net`
assert_eq!(
domainmap.get(&Domain::reference("www.example.net")?),
Some(&250)
);
assert_eq!(
domainmap.get(&Domain::reference("other.example.net")?),
Some(&101)
);
// Remove the entry for `example.com`
let old_value = domainmap.remove(&Domain::presented("example.com")?);
assert_eq!(old_value, Some(50));
assert_eq!(
domainmap.get(&Domain::reference("example.com")?),
None // Not in the map anymore
);
// Show the amount of key-value pairs in the map
assert_eq!(domainmap.len(), 2); // `*.example.net` and `www.example.net`
// Clear the map
domainmap.clear();
assert!(domainmap.is_empty());
Fields§
§data: Vec<(Domain, T)>
Implementations§
Source§impl<T> DomainMap<T>
impl<T> DomainMap<T>
Sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Create a new empty DomainMap
with enough capacity for at least cap
key-value pairs
Sourcepub fn set(&mut self, domain: Domain, value: T) -> Option<T>
pub fn set(&mut self, domain: Domain, value: T) -> Option<T>
Set the value for the given domain, adding a new entry if the domain was not already in the map, and returning the old value otherwise
§Examples
let mut domainmap = DomainMap::<Certificate>::new();
domainmap.set(Domain::presented("example.com")?, get_certificate());
domainmap.set(Domain::presented("*.example.com")?, get_certificate());
assert!(domainmap.get(&Domain::presented("example.com")?).is_some());
Sourcepub fn get(&self, domain: &Domain) -> Option<&T>
pub fn get(&self, domain: &Domain) -> Option<&T>
Get the value matching the reference identifier domain
If there is a value for a wildcard domain matching the given domain, and for the given domain itself, the specific (non-wildcard) domain’s value is always returned, regardless of insertion order
§Examples
let mut domainmap = DomainMap::<Certificate>::new();
domainmap.set(Domain::presented("*.example.com")?, get_certificate());
assert!(domainmap.get(&Domain::reference("example.com")?).is_none());
assert!(domainmap
.get(&Domain::reference("www.example.com")?)
.is_some());
let mut domainmap = DomainMap::<u64>::new();
domainmap.set(Domain::presented("foo.example.com")?, 10);
domainmap.set(Domain::presented("*.example.com")?, 50);
assert_eq!(
domainmap.get(&Domain::reference("bar.example.com")?),
Some(&50)
);
assert_eq!(
domainmap.get(&Domain::reference("foo.example.com")?),
Some(&10)
);
Sourcepub fn get_mut(&mut self, domain: &Domain) -> Option<&mut T>
pub fn get_mut(&mut self, domain: &Domain) -> Option<&mut T>
Get a mutable reference to the value matching the reference identifier
If there is a value for a wildcard domain matching the given domain, and for the given domain itself, the specific (non-wildcard) domain’s value is always returned, regardless of insertion order
§Examples
let mut domainmap = DomainMap::<Certificate>::new();
domainmap.set(Domain::presented("*.example.com")?, get_certificate());
assert!(domainmap
.get_mut(&Domain::reference("example.com")?)
.is_none());
assert!(domainmap
.get_mut(&Domain::reference("www.example.com")?)
.is_some());
let mut domainmap = DomainMap::<u64>::new();
domainmap.set(Domain::presented("foo.example.com")?, 10);
domainmap.set(Domain::presented("*.example.com")?, 50);
assert_eq!(
domainmap.get_mut(&Domain::reference("bar.example.com")?),
Some(&mut 50)
);
assert_eq!(
domainmap.get_mut(&Domain::reference("foo.example.com")?),
Some(&mut 10)
);
Sourcepub fn get_eq(&self, domain: &Domain) -> Option<&T>
pub fn get_eq(&self, domain: &Domain) -> Option<&T>
Get the value for the given domain, checking using ==
instead of
matching
§Examples
let mut domainmap = DomainMap::<Certificate>::new();
domainmap.set(Domain::presented("*.example.com")?, get_certificate());
assert!(domainmap
.get_eq(&Domain::presented("www.example.com")?)
.is_none());
assert!(domainmap
.get_eq(&Domain::presented("*.example.com")?)
.is_some());
Sourcepub fn remove(&mut self, domain: &Domain) -> Option<T>
pub fn remove(&mut self, domain: &Domain) -> Option<T>
Remove the given domain from the map, returning its value, if any
Note that unlike DomainMap::get
, this method compares the domain using
==
instead of checking for a match
§Examples
let mut domainmap = DomainMap::<Certificate>::new();
domainmap.set(Domain::presented("*.example.com")?, get_certificate());
assert!(domainmap
.remove(&Domain::presented("example.com")?)
.is_none());
assert!(domainmap
.remove(&Domain::presented("*.example.com")?)
.is_some());
assert!(domainmap
.remove(&Domain::presented("*.example.com")?)
.is_none());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the DomainMap
, removing all contents
§Examples
let mut domainmap = DomainMap::<Certificate>::new();
domainmap.set(Domain::presented("example.com")?, get_certificate());
assert!(domainmap.get(&Domain::reference("example.com")?).is_some());
domainmap.clear();
assert!(domainmap.get(&Domain::reference("example.com")?).is_none());
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Return an iterator over references to this map’s key-value pairs in unspecified order
§Examples
let mut domainmap = DomainMap::<u32>::new();
domainmap.set(Domain::presented("example.com")?, 1);
let mut iterator = domainmap.iter();
assert_eq!(
iterator.next(),
Some((&Domain::presented("example.com")?, &1))
);
assert_eq!(iterator.next(), None);
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Return an iterator over mutable references to this map’s key-value pairs in unspecified order
§Examples
let mut domainmap = DomainMap::<u32>::new();
domainmap.set(Domain::presented("example.com")?, 1);
let mut iterator = domainmap.iter_mut();
assert_eq!(
iterator.next(),
Some((&Domain::presented("example.com")?, &mut 1))
);
assert_eq!(iterator.next(), None);
Trait Implementations§
Source§impl<'de, T: Deserialize<'de>> Deserialize<'de> for DomainMap<T>
impl<'de, T: Deserialize<'de>> Deserialize<'de> for DomainMap<T>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T> Extend<(Domain, T)> for DomainMap<T>
impl<T> Extend<(Domain, T)> for DomainMap<T>
Source§fn extend<I: IntoIterator<Item = (Domain, T)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (Domain, T)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)