Struct DomainMap

Source
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 Domains. 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>

Source

pub const fn new() -> Self

Create a new empty DomainMap

Source

pub fn with_capacity(cap: usize) -> Self

Create a new empty DomainMap with enough capacity for at least cap key-value pairs

Source

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());
Source

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)
);
Source

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)
);
Source

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());
Source

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());
Source

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());
Source

pub fn len(&self) -> usize

Get the number of key-value pairs in the DomainMap

§Examples
let mut domainmap = DomainMap::<Certificate>::new();

assert_eq!(domainmap.len(), 0);

domainmap.set(Domain::presented("example.com")?, get_certificate());

assert_eq!(domainmap.len(), 1);
Source

pub fn is_empty(&self) -> bool

Check whether the DomainMap is empty, i.e. if its length is 0

§Examples
let mut domainmap = DomainMap::<Certificate>::new();

assert!(domainmap.is_empty());

domainmap.set(Domain::presented("example.com")?, get_certificate());

assert!(!domainmap.is_empty());
Source

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);
Source

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<T: Clone> Clone for DomainMap<T>

Source§

fn clone(&self) -> DomainMap<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for DomainMap<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for DomainMap<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, T: Deserialize<'de>> Deserialize<'de> for DomainMap<T>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Extend<(Domain, T)> for DomainMap<T>

Source§

fn extend<I: IntoIterator<Item = (Domain, T)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> FromIterator<(Domain, T)> for DomainMap<T>

Source§

fn from_iter<I: IntoIterator<Item = (Domain, T)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for DomainMap<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: 'a> IntoIterator for &'a DomainMap<T>

Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

type Item = (&'a Domain, &'a T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T: 'a> IntoIterator for &'a mut DomainMap<T>

Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

type Item = (&'a Domain, &'a mut T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for DomainMap<T>

Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

type Item = (Domain, T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for DomainMap<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Serialize> Serialize for DomainMap<T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Eq> Eq for DomainMap<T>

Auto Trait Implementations§

§

impl<T> Freeze for DomainMap<T>

§

impl<T> RefUnwindSafe for DomainMap<T>
where T: RefUnwindSafe,

§

impl<T> Send for DomainMap<T>
where T: Send,

§

impl<T> Sync for DomainMap<T>
where T: Sync,

§

impl<T> Unpin for DomainMap<T>
where T: Unpin,

§

impl<T> UnwindSafe for DomainMap<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T