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

Note: For an occupied entry, this is the canonical stored prefix (host bits masked out). For a vacant entry, the prefix is returned exactly as it was passed to PrefixMap::entry, including any host bits.

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> !RefUnwindSafe for Entry<'a, P, T>

§

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

§

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

§

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,

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

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
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.