Skip to main content

Entry

Enum 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<P: Prefix, T> Entry<'_, P, T>

Source

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

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

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,

Source

pub fn insert(self, v: T) -> Option<T>

Replace the current entry, and return the entry that was stored before.

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

Host bits from the entry argument are not preserved:

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.0/24".parse()?, &2))
);
Source

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

Host bits from an existing matching prefix are not preserved.

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.0/24".parse()?, &1))
);
Source

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

Host bits from an existing matching prefix are not preserved.

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.0/24".parse()?, &1))
);
Source

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

Host bits from an existing matching prefix are not preserved.

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.0/24".parse()?, &2))
);
Source§

impl<'a, P, T> Entry<'a, P, T>
where P: Prefix, T: Default,

Source

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

Host bits from an existing matching prefix are not preserved.

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.0/24".parse()?, &1))
);

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>
where P: Send, T: Send,

§

impl<'a, P, T> Sync for Entry<'a, P, T>
where P: Sync, T: Sync,

§

impl<'a, P, T> Unpin for Entry<'a, P, T>
where P: Unpin,

§

impl<'a, P, T> UnsafeUnpin for Entry<'a, P, T>
where P: UnsafeUnpin,

§

impl<'a, P, T> !UnwindSafe for Entry<'a, P, T>

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.