pub struct PrefixMap<P, T> { /* private fields */ }Expand description
Prefix map implemented as a TreeBitMap.
Implementations§
Source§impl<P, T> PrefixMap<P, T>where
P: Prefix,
impl<P, T> PrefixMap<P, T>where
P: Prefix,
Sourcepub fn mem_size(&self) -> usize
pub fn mem_size(&self) -> usize
Returns the amount of memory used by this datastructure in bytes.
Warning: This number does not include any heap allocations of T!
Sourcepub fn get<'a>(&'a self, prefix: &P) -> Option<&'a T>
pub fn get<'a>(&'a self, prefix: &P) -> Option<&'a T>
Get the value of an element by matching exactly on the prefix.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&1));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.0.0/23".parse()?), None);
assert_eq!(pm.get(&"192.168.1.128/25".parse()?), None);Sourcepub fn get_mut<'a>(&'a mut self, prefix: &P) -> Option<&'a mut T>
pub fn get_mut<'a>(&'a mut self, prefix: &P) -> Option<&'a mut T>
Get a mutable reference to a value of an element by matching exactly on the prefix.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let prefix = "192.168.1.0/24".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get_mut(&prefix), Some(&mut 1));
*pm.get_mut(&prefix).unwrap() += 1;
assert_eq!(pm.get_mut(&prefix), Some(&mut 2));Sourcepub fn get_key_value<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>
pub fn get_key_value<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>
Get the value of an element by matching exactly on the prefix.
Prefixes are not stored verbatim. They are reconstructed from the trie position, so host bits masked out by the prefix length are not preserved.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let prefix = "192.168.1.0/24".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get_key_value(&prefix), Some((prefix, &1)));Warning The table does not store the prefix, but it is reconstructed. This means, that any bits in the host part will be truncated:
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let prefix = "192.168.1.0/24".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get_key_value(&prefix), Some((prefix.trunc(), &1)));Sourcepub fn get_lpm<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>
pub fn get_lpm<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>
Get a value of an element by using longest prefix matching
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_lpm(&"192.168.1.1/32".parse()?), Some(("192.168.1.0/24".parse()?, &1)));
assert_eq!(pm.get_lpm(&"192.168.1.0/24".parse()?), Some(("192.168.1.0/24".parse()?, &1)));
assert_eq!(pm.get_lpm(&"192.168.0.0/24".parse()?), Some(("192.168.0.0/23".parse()?, &2)));
assert_eq!(pm.get_lpm(&"192.168.2.0/24".parse()?), None);Warning The table does not store the prefix, but it is reconstructed. This means, that any bits in the host part will be truncated:
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
assert_eq!(pm.get_lpm(&"192.168.1.1/32".parse()?), Some(("192.168.1.0/24".parse()?, &1)));Sourcepub fn get_lpm_mut<'a>(&'a mut self, prefix: &P) -> Option<(P, &'a mut T)>
pub fn get_lpm_mut<'a>(&'a mut self, prefix: &P) -> Option<(P, &'a mut T)>
Get a mutable reference to a value of an element by using longest prefix matching
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_lpm_mut(&"192.168.1.1/32".parse()?), Some(("192.168.1.0/24".parse()?, &mut 1)));
*pm.get_lpm_mut(&"192.168.1.64/26".parse()?).unwrap().1 += 1;
assert_eq!(pm.get_lpm_mut(&"192.168.1.1/32".parse()?), Some(("192.168.1.0/24".parse()?, &mut 2)));Warning The table does not store the prefix, but it is reconstructed. This means, that any bits in the host part will be truncated.
Sourcepub fn get_lpm_prefix(&self, prefix: &P) -> Option<P>
pub fn get_lpm_prefix(&self, prefix: &P) -> Option<P>
Get the longest prefix in the datastructure that matches the given prefix.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_lpm_prefix(&"192.168.1.1/32".parse()?), Some("192.168.1.0/24".parse()?));
assert_eq!(pm.get_lpm_prefix(&"192.168.1.0/24".parse()?), Some("192.168.1.0/24".parse()?));
assert_eq!(pm.get_lpm_prefix(&"192.168.0.0/24".parse()?), Some("192.168.0.0/23".parse()?));
assert_eq!(pm.get_lpm_prefix(&"192.168.2.0/24".parse()?), None);Warning The table does not store the prefix, but it is reconstructed. This means, that any bits in the host part will be truncated:
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
assert_eq!(pm.get_lpm_prefix(&"192.168.1.1/32".parse()?), Some("192.168.1.0/24".parse()?));Sourcepub fn contains_key(&self, prefix: &P) -> bool
pub fn contains_key(&self, prefix: &P) -> bool
Check if a key is present in the datastructure.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert!(pm.contains_key(&"192.168.1.0/24".parse()?));
assert!(!pm.contains_key(&"192.168.2.0/24".parse()?));
assert!(!pm.contains_key(&"192.168.0.0/23".parse()?));
assert!(!pm.contains_key(&"192.168.1.128/25".parse()?));Sourcepub fn get_spm<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>
pub fn get_spm<'a>(&'a self, prefix: &P) -> Option<(P, &'a T)>
Get a value of an element by using shortest prefix matching.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_spm(&"192.168.1.1/32".parse()?), Some(("192.168.0.0/23".parse()?, &2)));
assert_eq!(pm.get_spm(&"192.168.1.0/24".parse()?), Some(("192.168.0.0/23".parse()?, &2)));
assert_eq!(pm.get_spm(&"192.168.0.0/23".parse()?), Some(("192.168.0.0/23".parse()?, &2)));
assert_eq!(pm.get_spm(&"192.168.2.0/24".parse()?), None);Warning The table does not store the prefix, but it is reconstructed. This means, that any bits in the host part will be truncated.
Sourcepub fn get_spm_prefix(&self, prefix: &P) -> Option<P>
pub fn get_spm_prefix(&self, prefix: &P) -> Option<P>
Get the shortest prefix in the datastructure that contains the given prefix.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
assert_eq!(pm.get_spm_prefix(&"192.168.1.1/32".parse()?), Some("192.168.0.0/23".parse()?));
assert_eq!(pm.get_spm_prefix(&"192.168.1.0/24".parse()?), Some("192.168.0.0/23".parse()?));
assert_eq!(pm.get_spm_prefix(&"192.168.0.0/23".parse()?), Some("192.168.0.0/23".parse()?));
assert_eq!(pm.get_spm_prefix(&"192.168.2.0/24".parse()?), None);Warning The table does not store the prefix, but it is reconstructed. This means, that any bits in the host part will be truncated.
Sourcepub fn insert(&mut self, prefix: P, value: T) -> Option<T>
pub fn insert(&mut self, prefix: P, value: T) -> Option<T>
Insert a new item into the prefix-map. This function may return any value that existed before.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
assert_eq!(pm.insert("192.168.0.0/23".parse()?, 1), None);
assert_eq!(pm.insert("192.168.1.0/24".parse()?, 2), None);
assert_eq!(pm.insert("192.168.1.0/24".parse()?, 3), Some(2));Warning: You cannot store additional information in the host-part of the prefix. Prefixes are reconstructed from the trie position, so host bits are not preserved.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.1/24".parse()?, 1);
assert_eq!(
pm.get_key_value(&"192.168.0.0/24".parse()?),
Some(("192.168.0.0/24".parse()?, &1)) // notice that the host part is zero.
);Sourcepub fn entry(&mut self, prefix: P) -> Entry<'_, P, T>
pub fn entry(&mut self, prefix: P) -> Entry<'_, P, T>
Gets the given key’s corresponding entry in the map for in-place manipulation.
Prefixes are not stored verbatim. They are reconstructed from the trie position, so host
bits masked out by the prefix length are not preserved. See the documentation of
Entry, OccupiedEntry, and VacantEntry.
let mut pm: PrefixMap<ipnet::Ipv4Net, Vec<i32>> = PrefixMap::new();
pm.insert("192.168.0.0/23".parse()?, vec![1]);
pm.entry("192.168.0.1/23".parse()?).or_default().push(2);
pm.entry("192.168.0.0/24".parse()?).or_default().push(3);
assert_eq!(pm.get(&"192.168.0.0/23".parse()?), Some(&vec![1, 2]));
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), Some(&vec![3]));Sourcepub fn remove(&mut self, prefix: &P) -> Option<T>
pub fn remove(&mut self, prefix: &P) -> Option<T>
Removes a key from the map, returning the value at the key if the key was previously in the
map. In contrast to Self::remove_keep_tree, this operation may prune empty trie nodes,
reducing the memory footprint.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let prefix = "192.168.1.0/24".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get(&prefix), Some(&1));
assert_eq!(pm.remove(&prefix), Some(1));
assert_eq!(pm.get(&prefix), None);Sourcepub fn remove_keep_tree(&mut self, prefix: &P) -> Option<T>
pub fn remove_keep_tree(&mut self, prefix: &P) -> Option<T>
Removes a key from the map, returning the value at the key if the key was previously in the
map. In contrast to Self::remove, this operation only removes the stored value and may
leave empty trie nodes in place.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let prefix = "192.168.1.0/24".parse()?;
pm.insert(prefix, 1);
assert_eq!(pm.get(&prefix), Some(&1));
assert_eq!(pm.remove_keep_tree(&prefix), Some(1));
assert_eq!(pm.get(&prefix), None);Sourcepub fn remove_children(&mut self, prefix: &P)
pub fn remove_children(&mut self, prefix: &P)
Remove all entries that are contained within prefix. This will change the tree
structure. This operation is O(n), as the entries must be freed up one-by-one.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/21".parse()?, 1);
pm.insert("192.168.0.0/22".parse()?, 2);
pm.insert("192.168.0.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.4.0/22".parse()?, 5);
pm.insert("192.168.4.0/23".parse()?, 6);
assert_eq!(pm.len(), 6);
pm.remove_children(&"192.168.0.0/22".parse()?);
assert_eq!(pm.len(), 3);
assert_eq!(pm.get(&"192.168.0.0/22".parse()?), None);
assert_eq!(pm.get(&"192.168.0.0/23".parse()?), None);
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.4.0/22".parse()?), Some(&5));
assert_eq!(pm.get(&"192.168.4.0/23".parse()?), Some(&6));Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the map but keep the allocated memory.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/24".parse()?, 1);
pm.insert("192.168.1.0/24".parse()?, 2);
pm.clear();
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), None);Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Keep only the elements in the map that satisfy the given condition f.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/24".parse()?, 1);
pm.insert("192.168.1.0/24".parse()?, 2);
pm.insert("192.168.2.0/24".parse()?, 3);
pm.insert("192.168.2.0/25".parse()?, 4);
pm.retain(|_, t| *t % 2 == 0);
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&2));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.2.0/25".parse()?), Some(&4));You can also use the prefix for filtering
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/24".parse()?, 1);
pm.insert("192.168.1.0/24".parse()?, 2);
pm.insert("192.168.2.0/24".parse()?, 3);
pm.insert("192.168.2.0/25".parse()?, 4);
pm.retain(|p, _| p.prefix_len() > 24);
assert_eq!(pm.get(&"192.168.0.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), None);
assert_eq!(pm.get(&"192.168.2.0/25".parse()?), Some(&4));Sourcepub fn cover<'a>(&'a self, prefix: &P) -> Cover<'a, P, T> ⓘ
pub fn cover<'a>(&'a self, prefix: &P) -> Cover<'a, P, T> ⓘ
Iterate over all entries in the map that covers the given prefix (including prefix
itself if that is present in the map). The returned iterator yields (P, &'a T), with
reconstructed prefixes P.
The iterator will always yield elements ordered by their prefix length, i.e., their depth in the tree.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let p0 = "10.0.0.0/8".parse()?;
let p1 = "10.1.0.0/16".parse()?;
let p2 = "10.1.1.0/24".parse()?;
pm.insert(p0, 0);
pm.insert(p1, 1);
pm.insert(p2, 2);
pm.insert("10.1.2.0/24".parse()?, 3); // disjoint prefixes are not covered
pm.insert("10.1.1.0/25".parse()?, 4); // more specific prefixes are not covered
pm.insert("11.0.0.0/8".parse()?, 5); // Branch points that don't contain values are skipped
assert_eq!(
pm.cover(&p2).collect::<Vec<_>>(),
vec![(p0, &0), (p1, &1), (p2, &2)]
);This function also yields the root node if it is part of the map:
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let root = "0.0.0.0/0".parse()?;
pm.insert(root, 0);
assert_eq!(pm.cover(&"10.0.0.0/8".parse()?).collect::<Vec<_>>(), vec![(root, &0)]);Sourcepub fn cover_keys<'a>(&'a self, prefix: &P) -> CoverKeys<'a, P, T> ⓘ
pub fn cover_keys<'a>(&'a self, prefix: &P) -> CoverKeys<'a, P, T> ⓘ
Iterate over all keys (prefixes) in the map that covers the given prefix (including
prefix itself if that is present in the map). The returned iterator yields reconstructed
prefixes P.
The iterator will always yield elements ordered by their prefix length, i.e., their depth in the tree.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let p0 = "10.0.0.0/8".parse()?;
let p1 = "10.1.0.0/16".parse()?;
let p2 = "10.1.1.0/24".parse()?;
pm.insert(p0, 0);
pm.insert(p1, 1);
pm.insert(p2, 2);
pm.insert("10.1.2.0/24".parse()?, 3); // disjoint prefixes are not covered
pm.insert("10.1.1.0/25".parse()?, 4); // more specific prefixes are not covered
pm.insert("11.0.0.0/8".parse()?, 5); // Branch points that don't contain values are skipped
assert_eq!(pm.cover_keys(&p2).collect::<Vec<_>>(), vec![p0, p1, p2]);Sourcepub fn cover_values<'a>(&'a self, prefix: &P) -> CoverValues<'a, P, T> ⓘ
pub fn cover_values<'a>(&'a self, prefix: &P) -> CoverValues<'a, P, T> ⓘ
Iterate over all values in the map that covers the given prefix (including prefix
itself if that is present in the map). The returned iterator yields &'a T.
The iterator will always yield elements ordered by their prefix length, i.e., their depth in the tree.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
let p0 = "10.0.0.0/8".parse()?;
let p1 = "10.1.0.0/16".parse()?;
let p2 = "10.1.1.0/24".parse()?;
pm.insert(p0, 0);
pm.insert(p1, 1);
pm.insert(p2, 2);
pm.insert("10.1.2.0/24".parse()?, 3); // disjoint prefixes are not covered
pm.insert("10.1.1.0/25".parse()?, 4); // more specific prefixes are not covered
pm.insert("11.0.0.0/8".parse()?, 5); // Branch points that don't contain values are skipped
assert_eq!(pm.cover_values(&p2).collect::<Vec<_>>(), vec![&0, &1, &2]);Sourcepub fn iter(&self) -> Iter<'_, P, T> ⓘ
pub fn iter(&self) -> Iter<'_, P, T> ⓘ
An iterator visiting all key-value pairs in lexicographic order. The iterator element type
is (P, &T), with reconstructed prefixes P.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
pm.iter().collect::<Vec<_>>(),
vec![
("192.168.0.0/22".parse()?, &1),
("192.168.0.0/23".parse()?, &2),
("192.168.0.0/24".parse()?, &4),
("192.168.2.0/23".parse()?, &3),
("192.168.2.0/24".parse()?, &5),
]
);Sourcepub fn iter_mut(&mut self) -> IterMut<'_, P, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, P, T> ⓘ
Get a mutable iterator over all key-value pairs. The order of this iterator is lexicographic.
Sourcepub fn iter_from<'a>(&'a self, prefix: &P, inclusive: bool) -> Iter<'a, P, T> ⓘ
pub fn iter_from<'a>(&'a self, prefix: &P, inclusive: bool) -> Iter<'a, P, T> ⓘ
Return an iterator starting at the given prefix in lexicographic order.
If inclusive is true, the iterator includes the entry at prefix (if present).
If inclusive is false, the iterator starts after prefix.
If prefix is not present in the map, the iterator starts at the first entry that
would come after prefix in lexicographic order, regardless of inclusive.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("10.0.0.0/8".parse()?, 1);
pm.insert("10.1.0.0/16".parse()?, 2);
pm.insert("10.2.0.0/16".parse()?, 3);
pm.insert("10.3.0.0/16".parse()?, 4);
pm.insert("10.4.0.0/16".parse()?, 5);
// Inclusive: start at 10.2.0.0/16 and take the next 2 entries
let page: Vec<_> = pm.iter_from(&"10.2.0.0/16".parse()?, true).take(2).collect();
assert_eq!(page, vec![("10.2.0.0/16".parse()?, &3), ("10.3.0.0/16".parse()?, &4)]);
// Exclusive: cursor pagination — skip last seen, fetch next page
let last_seen: ipnet::Ipv4Net = "10.2.0.0/16".parse()?;
let next_page: Vec<_> = pm.iter_from(&last_seen, false).take(2).collect();
assert_eq!(next_page, vec![("10.3.0.0/16".parse()?, &4), ("10.4.0.0/16".parse()?, &5)]);Sourcepub fn iter_from_mut<'a>(
&'a mut self,
prefix: &P,
inclusive: bool,
) -> IterMut<'a, P, T> ⓘ
pub fn iter_from_mut<'a>( &'a mut self, prefix: &P, inclusive: bool, ) -> IterMut<'a, P, T> ⓘ
Return a mutable iterator starting at the given prefix in lexicographic order.
If inclusive is true, the iterator includes the entry at prefix (if present).
If inclusive is false, the iterator starts after prefix.
If prefix is not present in the map, the iterator starts at the first entry that
would come after prefix in lexicographic order, regardless of inclusive.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("10.0.0.0/8".parse()?, 1);
pm.insert("10.1.0.0/16".parse()?, 2);
pm.insert("10.2.0.0/16".parse()?, 3);
// Mutate all entries starting from 10.1.0.0/16 (inclusive)
pm.iter_from_mut(&"10.1.0.0/16".parse()?, true).for_each(|(_, v)| *v *= 10);
assert_eq!(pm.get(&"10.0.0.0/8".parse()?), Some(&1));
assert_eq!(pm.get(&"10.1.0.0/16".parse()?), Some(&20));
assert_eq!(pm.get(&"10.2.0.0/16".parse()?), Some(&30));Sourcepub fn keys(&self) -> Keys<'_, P, T> ⓘ
pub fn keys(&self) -> Keys<'_, P, T> ⓘ
An iterator visiting all keys in lexicographic order. The iterator element type is
reconstructed prefixes P.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
pm.keys().collect::<Vec<_>>(),
vec![
"192.168.0.0/22".parse()?,
"192.168.0.0/23".parse()?,
"192.168.0.0/24".parse()?,
"192.168.2.0/23".parse()?,
"192.168.2.0/24".parse()?,
]
);Sourcepub fn into_keys(self) -> IntoKeys<P, T> ⓘ
pub fn into_keys(self) -> IntoKeys<P, T> ⓘ
Creates a consuming iterator visiting all keys in lexicographic order. The iterator element
type is reconstructed prefixes P.
Sourcepub fn values(&self) -> Values<'_, P, T> ⓘ
pub fn values(&self) -> Values<'_, P, T> ⓘ
An iterator visiting all values in lexicographic order. The iterator element type is &T.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(pm.values().collect::<Vec<_>>(), vec![&1, &2, &4, &3, &5]);Sourcepub fn into_values(self) -> IntoValues<P, T> ⓘ
pub fn into_values(self) -> IntoValues<P, T> ⓘ
Creates a consuming iterator visiting all values in lexicographic order. The iterator
element type is T.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, P, T> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, P, T> ⓘ
Get a mutable iterator over all values. The order of this iterator is lexicographic.
Source§impl<P, T> PrefixMap<P, T>where
P: Prefix,
impl<P, T> PrefixMap<P, T>where
P: Prefix,
Sourcepub fn children<'a>(&'a self, prefix: &P) -> Iter<'a, P, T> ⓘ
pub fn children<'a>(&'a self, prefix: &P) -> Iter<'a, P, T> ⓘ
Get an iterator over the node itself and all children. All elements returned have a prefix
that is contained within prefix itself (or are the same). The iterator yields
(P, &'a T), with reconstructed prefixes P. The iterator yields elements in
lexicographic order.
Note: Consider using crate::AsView::view_at as an alternative.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
pm.children(&"192.168.0.0/23".parse()?).collect::<Vec<_>>(),
vec![
("192.168.0.0/23".parse()?, &2),
("192.168.0.0/24".parse()?, &4),
]
);Sourcepub fn children_mut<'a>(&'a mut self, prefix: &P) -> IterMut<'a, P, T> ⓘ
pub fn children_mut<'a>(&'a mut self, prefix: &P) -> IterMut<'a, P, T> ⓘ
Get an iterator of mutable references of the node itself and all its children. All elements
returned have a prefix that is contained within prefix itself (or are the same). The
iterator yields (P, &'a mut T), with reconstructed prefixes P. The iterator yields
elements in lexicographic order.
Note: Consider using crate::AsView::view_at on a mutable map reference as an
alternative.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.0.0/24".parse()?, 3);
pm.insert("192.168.2.0/23".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
pm.children_mut(&"192.168.0.0/23".parse()?).for_each(|(_, x)| *x *= 10);
assert_eq!(
pm.into_iter().collect::<Vec<_>>(),
vec![
("192.168.0.0/22".parse()?, 1),
("192.168.0.0/23".parse()?, 20),
("192.168.0.0/24".parse()?, 30),
("192.168.2.0/23".parse()?, 4),
("192.168.2.0/24".parse()?, 5),
]
);Sourcepub fn into_children(self, prefix: &P) -> IntoIter<P, T> ⓘ
pub fn into_children(self, prefix: &P) -> IntoIter<P, T> ⓘ
Get an iterator over the node itself and all children with a value. All elements returned
have a prefix that is contained within prefix itself (or are the same). This function will
consume self, returning an iterator over all owned children.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.0.0/22".parse()?, 1);
pm.insert("192.168.0.0/23".parse()?, 2);
pm.insert("192.168.2.0/23".parse()?, 3);
pm.insert("192.168.0.0/24".parse()?, 4);
pm.insert("192.168.2.0/24".parse()?, 5);
assert_eq!(
pm.into_children(&"192.168.0.0/23".parse()?).collect::<Vec<_>>(),
vec![
("192.168.0.0/23".parse()?, 2),
("192.168.0.0/24".parse()?, 4),
]
);Trait Implementations§
Source§impl<'a, P: Prefix, T> AsView<'a> for &'a mut PrefixMap<P, T>
impl<'a, P: Prefix, T> AsView<'a> for &'a mut PrefixMap<P, T>
Source§type View = TrieRefMut<'a, P, T>
type View = TrieRefMut<'a, P, T>
view.Source§fn view(self) -> TrieRefMut<'a, P, T> ⓘ
fn view(self) -> TrieRefMut<'a, P, T> ⓘ
Source§impl<'a, P: Prefix, T> IntoIterator for &'a PrefixMap<P, T>
impl<'a, P: Prefix, T> IntoIterator for &'a PrefixMap<P, T>
Source§impl<P: Prefix, T> IntoIterator for PrefixMap<P, T>
impl<P: Prefix, T> IntoIterator for PrefixMap<P, T>
impl<P: Prefix + Eq, T: Eq> Eq for PrefixMap<P, T>
Auto Trait Implementations§
impl<P, T> !Freeze for PrefixMap<P, T>
impl<P, T> !RefUnwindSafe for PrefixMap<P, T>
impl<P, T> Send for PrefixMap<P, T>
impl<P, T> Sync for PrefixMap<P, T>
impl<P, T> Unpin for PrefixMap<P, T>
impl<P, T> UnsafeUnpin for PrefixMap<P, T>
impl<P, T> UnwindSafe for PrefixMap<P, T>where
P: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more