Enum Entry

Source
pub enum Entry<'a, S, K, V>
where S: MapStorage<K, V> + 'a,
{ Occupied(S::Occupied<'a>), Vacant(S::Vacant<'a>), }
Expand description

A view into a single entry in a map, which may either be vacant or occupied.

This enum is constructed from the entry method on Map.

Variants§

§

Occupied(S::Occupied<'a>)

An occupied entry.

§

Vacant(S::Vacant<'a>)

A vacant entry.

Implementations§

Source§

impl<'a, S, K, V> Entry<'a, S, K, V>
where S: MapStorage<K, V> + 'a,

Source

pub fn or_insert(self, default: V) -> &'a mut V

Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry.

§Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();

map.entry(MyKey::First).or_insert(3);
assert_eq!(map.get(MyKey::First), Some(&3));

*map.entry(MyKey::First).or_insert(10) *= 2;
assert_eq!(map.get(MyKey::First), Some(&6));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();

map.entry(MyKey::First(false)).or_insert(3);
assert_eq!(map.get(MyKey::First(false)), Some(&3));

*map.entry(MyKey::First(false)).or_insert(10) *= 2;
assert_eq!(map.get(MyKey::First(false)), Some(&6));
Source

pub fn or_insert_with<F>(self, default: F) -> &'a mut V
where F: FnOnce() -> V,

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.

§Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, String> = Map::new();

map.entry(MyKey::First).or_insert_with(|| format!("{}", 3));
assert_eq!(map.get(MyKey::First), Some(&"3".to_string()));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, String> = Map::new();

map.entry(MyKey::First(false)).or_insert_with(|| format!("{}", 3));
assert_eq!(map.get(MyKey::First(false)), Some(&"3".to_string()));
Source

pub fn or_insert_with_key<F>(self, default: F) -> &'a mut V
where F: FnOnce(K) -> V,

Ensures a value is in the entry by inserting, if empty, the result of the default function. This method allows for generating key-derived values for insertion by providing the default function a copy of the key that was passed to the .entry(key) method call.

§Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key, Debug)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, String> = Map::new();

map.entry(MyKey::First).or_insert_with_key(|k| format!("{:?} = {}", k, 3));
assert_eq!(map.get(MyKey::First), Some(&"First = 3".to_string()));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key, Debug)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, String> = Map::new();

map.entry(MyKey::First(false)).or_insert_with_key(|k| format!("{:?} = {}", k, 3));
assert_eq!(map.get(MyKey::First(false)), Some(&"First(false) = 3".to_string()));
Source

pub fn key(&self) -> K

Returns a copy of this entry’s key.

§Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key, Debug, PartialEq)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
assert_eq!(map.entry(MyKey::First).key(), MyKey::First);

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key, Debug, PartialEq)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();
assert_eq!(map.entry(MyKey::First(false)).key(), MyKey::First(false));
Source

pub fn and_modify<F>(self, f: F) -> Self
where F: FnOnce(&mut V),

Provides in-place mutable access to an occupied entry before any potential inserts into the map.

§Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();

map.entry(MyKey::First)
   .and_modify(|e| { *e += 1 })
   .or_insert(42);
assert_eq!(map.get(MyKey::First), Some(&42));

map.entry(MyKey::First)
   .and_modify(|e| { *e += 1 })
   .or_insert(42);
assert_eq!(map.get(MyKey::First), Some(&43));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();

map.entry(MyKey::First(true))
   .and_modify(|e| { *e += 1 })
   .or_insert(42);
assert_eq!(map.get(MyKey::First(true)), Some(&42));

map.entry(MyKey::First(true))
   .and_modify(|e| { *e += 1 })
   .or_insert(42);
assert_eq!(map.get(MyKey::First(true)), Some(&43));
Source

pub fn or_default(self) -> &'a mut V
where V: Default,

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.

§Examples
use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First,
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();

map.entry(MyKey::First).or_default();
assert_eq!(map.get(MyKey::First), Some(&0));

Using a composite key:

use fixed_map::{Key, Map};

#[derive(Clone, Copy, Key)]
enum MyKey {
    First(bool),
    Second,
}

let mut map: Map<MyKey, i32> = Map::new();

map.entry(MyKey::First(false)).or_default();
assert_eq!(map.get(MyKey::First(false)), Some(&0));

Auto Trait Implementations§

§

impl<'a, S, K, V> Freeze for Entry<'a, S, K, V>
where <S as MapStorage<K, V>>::Occupied<'a>: Freeze, <S as MapStorage<K, V>>::Vacant<'a>: Freeze,

§

impl<'a, S, K, V> RefUnwindSafe for Entry<'a, S, K, V>
where <S as MapStorage<K, V>>::Occupied<'a>: RefUnwindSafe, <S as MapStorage<K, V>>::Vacant<'a>: RefUnwindSafe,

§

impl<'a, S, K, V> Send for Entry<'a, S, K, V>
where <S as MapStorage<K, V>>::Occupied<'a>: Send, <S as MapStorage<K, V>>::Vacant<'a>: Send,

§

impl<'a, S, K, V> Sync for Entry<'a, S, K, V>
where <S as MapStorage<K, V>>::Occupied<'a>: Sync, <S as MapStorage<K, V>>::Vacant<'a>: Sync,

§

impl<'a, S, K, V> Unpin for Entry<'a, S, K, V>
where <S as MapStorage<K, V>>::Occupied<'a>: Unpin, <S as MapStorage<K, V>>::Vacant<'a>: Unpin,

§

impl<'a, S, K, V> UnwindSafe for Entry<'a, S, K, V>
where <S as MapStorage<K, V>>::Occupied<'a>: UnwindSafe, <S as MapStorage<K, V>>::Vacant<'a>: UnwindSafe,

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

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.