Enum fixed_map::map::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§

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 Key {
    First,
    Second,
}

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

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

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

Using a composite key:

use fixed_map::{Key, Map};

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

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

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

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

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 Key {
    First,
    Second,
}

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

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

Using a composite key:

use fixed_map::{Key, Map};

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

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

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

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 Key {
    First,
    Second,
}

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

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

Using a composite key:

use fixed_map::{Key, Map};

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

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

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

Returns a copy of this entry’s key.

Examples
use fixed_map::{Key, Map};

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

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

Using a composite key:

use fixed_map::{Key, Map};

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

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

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 Key {
    First,
    Second,
}

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

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

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

Using a composite key:

use fixed_map::{Key, Map};

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

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

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

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

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 Key {
    First,
    Second,
}

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

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

Using a composite key:

use fixed_map::{Key, Map};

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

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

map.entry(Key::First(false)).or_default();
assert_eq!(map.get(Key::First(false)), 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.