Trait VacantEntry

Source
pub trait VacantEntry<'a, K, V> {
    // Required methods
    fn key(&self) -> K;
    fn insert(self, value: V) -> &'a mut V;
}
Expand description

A view into a vacant entry in a Map. It is part of the Entry enum.

Required Methods§

Source

fn key(&self) -> K

Gets a copy of the key that would be used when inserting a value through the VacantEntry.

§Examples
use fixed_map::{Key, Map};
use fixed_map::map::{Entry, VacantEntry};

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

let mut map: Map<MyKey, i32> = Map::new();
let vacant = match map.entry(MyKey::First) {
    Entry::Vacant(entry) => entry,
    _ => unreachable!(),
};

assert_eq!(vacant.key(), MyKey::First);

Using a composite key:

use fixed_map::{Key, Map};
use fixed_map::map::{Entry, VacantEntry};

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

let mut map: Map<MyKey, i32> = Map::new();
let vacant = match map.entry(MyKey::First(false)) {
    Entry::Vacant(entry) => entry,
    _ => unreachable!(),
};

assert_eq!(vacant.key(), MyKey::First(false));
Source

fn insert(self, value: V) -> &'a mut V

Sets the value of the entry with the VacantEntry’s key, and returns a mutable reference to it.

§Examples
use fixed_map::{Key, Map};
use fixed_map::map::{Entry, VacantEntry};

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

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

if let Entry::Vacant(vacant) = map.entry(MyKey::First) {
    assert_eq!(vacant.insert(37), &37);
}

assert_eq!(map.get(MyKey::First), Some(&37));

Using a composite key:

use fixed_map::{Key, Map};
use fixed_map::map::{Entry, VacantEntry};

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

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

if let Entry::Vacant(vacant) = map.entry(MyKey::First(false)) {
    assert_eq!(vacant.insert(37), &37);
}

assert_eq!(map.get(MyKey::First(false)), Some(&37));

Implementations on Foreign Types§

Source§

impl<'a, K, V> VacantEntry<'a, K, V> for VacantEntry<'a, K, V, DefaultHashBuilder>
where K: Copy + Hash,

Source§

fn key(&self) -> K

Source§

fn insert(self, value: V) -> &'a mut V

Implementors§