Enum prefix_trie::map::Entry
source · pub enum Entry<'a, P, T> {
Vacant(VacantEntry<'a, P, T>),
Occupied(OccupiedEntry<'a, P, T>),
}Expand description
A mutable view into a single entry in a map, which may either be vacant or occupied.
Variants§
Vacant(VacantEntry<'a, P, T>)
The entry is not present in the tree.
Occupied(OccupiedEntry<'a, P, T>)
The entry is already present in the tree.
Implementations§
source§impl<'a, P, T> Entry<'a, P, T>
impl<'a, P, T> Entry<'a, P, T>
sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Get the value if it exists
let mut pm: PrefixMap<Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert_eq!(pm.entry("192.168.1.0/24".parse()?).get(), Some(&1));
assert_eq!(pm.entry("192.168.2.0/24".parse()?).get(), None);sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Get the value if it exists
let mut pm: PrefixMap<Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
pm.entry("192.168.1.0/24".parse()?).get_mut().map(|x| *x += 1);
pm.entry("192.168.2.0/24".parse()?).get_mut().map(|x| *x += 1);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&2));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), None);sourcepub fn key(&self) -> &P
pub fn key(&self) -> &P
get the key of the current entry
let mut pm: PrefixMap<Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert_eq!(pm.entry("192.168.1.0/24".parse()?).key(), &"192.168.1.0/24".parse()?);
assert_eq!(pm.entry("192.168.2.0/24".parse()?).key(), &"192.168.2.0/24".parse()?);source§impl<'a, P, T> Entry<'a, P, T>where
P: Prefix,
impl<'a, P, T> Entry<'a, P, T>where
P: Prefix,
sourcepub fn insert(self, v: T) -> Option<T>
pub fn insert(self, v: T) -> Option<T>
Replace the current entry, and return the entry that was stored before.
let mut pm: PrefixMap<Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert_eq!(pm.entry("192.168.1.0/24".parse()?).insert(10), Some(1));
assert_eq!(pm.entry("192.168.2.0/24".parse()?).insert(20), None);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&10));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), Some(&20));sourcepub fn or_insert(self, default: T) -> &'a mut T
pub fn or_insert(self, default: T) -> &'a mut T
Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.
let mut pm: PrefixMap<Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert_eq!(pm.entry("192.168.1.0/24".parse()?).or_insert(10), &1);
assert_eq!(pm.entry("192.168.2.0/24".parse()?).or_insert(20), &20);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&1));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), Some(&20));sourcepub fn or_insert_with<F: FnOnce() -> T>(self, default: F) -> &'a mut T
pub fn or_insert_with<F: FnOnce() -> T>(self, default: F) -> &'a mut T
Ensures a value is in the entry by inserting the result of the default function if empty, and returns a mutable reference to the value in the entry.
let mut pm: PrefixMap<Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert_eq!(pm.entry("192.168.1.0/24".parse()?).or_insert_with(|| 10), &1);
assert_eq!(pm.entry("192.168.2.0/24".parse()?).or_insert_with(|| 20), &20);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&1));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), Some(&20));sourcepub fn and_modify<F: FnOnce(&mut T)>(self, f: F) -> Self
pub fn and_modify<F: FnOnce(&mut T)>(self, f: F) -> Self
Provides in-place mutable access to an occupied entry before any potential inserts into the map.
let mut pm: PrefixMap<Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert_eq!(pm.entry("192.168.1.0/24".parse()?).and_modify(|x| *x += 1).get(), Some(&2));
assert_eq!(pm.entry("192.168.2.0/24".parse()?).and_modify(|x| *x += 1).get(), None);source§impl<'a, P, T> Entry<'a, P, T>where
P: Prefix,
T: Default,
impl<'a, P, T> Entry<'a, P, T>where
P: Prefix,
T: Default,
sourcepub fn or_default(self) -> &'a mut T
pub fn or_default(self) -> &'a mut T
Ensures a value is in the entry by inserting the default value if empty, and returns a mutable reference to the value in the entry.
let mut pm: PrefixMap<Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.0/24".parse()?, 1);
assert_eq!(pm.entry("192.168.1.0/24".parse()?).or_default(), &1);
assert_eq!(pm.entry("192.168.2.0/24".parse()?).or_default(), &0);
assert_eq!(pm.get(&"192.168.1.0/24".parse()?), Some(&1));
assert_eq!(pm.get(&"192.168.2.0/24".parse()?), Some(&0));