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>

source

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

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

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,

source

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

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

Auto Trait Implementations§

§

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> !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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.