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§

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

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

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

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

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

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

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

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§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.