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<P, T> Entry<'_, P, T>
impl<P, T> Entry<'_, P, T>
Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Get the value if it exists
let mut pm: PrefixMap<ipnet::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<ipnet::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<ipnet::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. This will also
replace the key with the one provided to the entry function.
let mut pm: PrefixMap<ipnet::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));This function will replace the prefix in the map with the one provided to the entry call:
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
pm.entry("192.168.1.2/24".parse()?).insert(2);
assert_eq!(
pm.get_key_value(&"192.168.1.0/24".parse()?),
Some((&"192.168.1.2/24".parse()?, &2)) // prefix is overwritten
);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<ipnet::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));This function will not replace the prefix in the map if it already exists.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
pm.entry("192.168.1.2/24".parse()?).or_insert(2);
assert_eq!(
pm.get_key_value(&"192.168.1.0/24".parse()?),
Some((&"192.168.1.1/24".parse()?, &1)) // prefix is not overwritten.
);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<ipnet::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));This function will not replace the prefix in the map if it already exists.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
pm.entry("192.168.1.2/24".parse()?).or_insert_with(|| 2);
assert_eq!(
pm.get_key_value(&"192.168.1.0/24".parse()?),
Some((&"192.168.1.1/24".parse()?, &1)) // prefix is not overwritten.
);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<ipnet::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);This function will not replace the prefix in the map if it already exists.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
pm.entry("192.168.1.2/24".parse()?).and_modify(|x| *x += 1);
assert_eq!(
pm.get_key_value(&"192.168.1.0/24".parse()?),
Some((&"192.168.1.1/24".parse()?, &2)) // prefix is not overwritten.
);Source§impl<'a, P, T> Entry<'a, P, T>
impl<'a, P, T> Entry<'a, P, T>
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<ipnet::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));This function will not replace the prefix in the map if it already exists.
let mut pm: PrefixMap<ipnet::Ipv4Net, _> = PrefixMap::new();
pm.insert("192.168.1.1/24".parse()?, 1);
pm.entry("192.168.1.2/24".parse()?).or_default();
assert_eq!(
pm.get_key_value(&"192.168.1.0/24".parse()?),
Some((&"192.168.1.1/24".parse()?, &1)) // prefix is not overwritten.
);Auto Trait Implementations§
impl<'a, P, T> Freeze for Entry<'a, P, T>where
P: Freeze,
impl<'a, P, T> !RefUnwindSafe for Entry<'a, P, T>
impl<'a, P, T> Send for Entry<'a, P, T>
impl<'a, P, T> Sync for Entry<'a, P, T>
impl<'a, P, T> Unpin for Entry<'a, P, T>where
P: Unpin,
impl<'a, P, T> !UnwindSafe for Entry<'a, P, T>
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> 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