pub trait VacantEntry<'a, K, V> {
// Required methods
fn key(&self) -> K;
fn insert(self, value: V) -> &'a mut V;
}
Required Methods§
Sourcefn key(&self) -> K
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));
Sourcefn insert(self, value: V) -> &'a mut V
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));