pub enum Entry<'a, K1: 'a, K2: 'a, V: 'a> {
Occupied(OccupiedEntry<'a, K1, K2, V>),
Vacant(VacantEntry<'a, K1, K2, V>),
}
Expand description
Variants§
Occupied(OccupiedEntry<'a, K1, K2, V>)
An occupied entry.
Vacant(VacantEntry<'a, K1, K2, V>)
A vacant entry.
Implementations§
Source§impl<'a, K1: Clone, K2: Clone, V> Entry<'a, K1, K2, V>
impl<'a, K1: Clone, K2: Clone, V> Entry<'a, K1, K2, V>
Sourcepub fn or_insert(self, default: V) -> &'a mut V
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 double_map::DHashMap;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
match map.entry("poneyland", 0) {
Ok(entry) => {
entry.or_insert(3);
}
Err(_) => unreachable!(),
}
assert_eq!(map.get_key1(&"poneyland"), Some(&3));
map.entry("poneyland", 0).map(|entry| *entry.or_insert(10) *= 2);
assert_eq!(map.get_key1(&"poneyland"), Some(&6));
Sourcepub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut 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 double_map::DHashMap;
let mut map: DHashMap<&str, u32, String> = DHashMap::new();
let s = "hoho".to_owned();
match map.entry("poneyland", 0) {
Ok(entry) => {
entry.or_insert_with(|| s);
}
Err(_) => unreachable!(),
}
assert_eq!(map.get_key1("poneyland"), Some(&"hoho".to_owned()));
let k = "another string".to_owned();
map.entry("poneyland", 0).map(|entry| entry.or_insert_with(|| k).push_str("ho"));
assert_eq!(map.get_key1(&"poneyland"), Some(&"hohoho".to_owned()));
Sourcepub fn or_insert_with_key1<F: FnOnce(&K1) -> V>(self, default: F) -> &'a mut V
pub fn or_insert_with_key1<F: FnOnce(&K1) -> V>(self, default: F) -> &'a mut V
Ensures a value is in the entry by inserting, if empty, the result of the default function.
This method allows generating key-derived (with using key # 1 K1
) value for
insertion by providing the default function a reference to the key that was moved
during the .entry(key)
method call.
The reference to the moved key is provided so that cloning or copying the key is
unnecessary, unlike with .or_insert_with(|| ... )
.
§Examples
use double_map::DHashMap;
let mut map: DHashMap<&str, usize, u64> = DHashMap::new();
match map.entry("poneyland", 0) {
Ok(entry) => {
entry.or_insert_with_key1(|k1| k1.chars().count() as u64);
},
Err(_) => unreachable!(),
}
assert_eq!(map.get_key1(&"poneyland"), Some(&9));
map.entry("bearland", 1).map(
|ent| ent.or_insert_with_key1(|k1| (k1.chars().count() * 2) as u64)
);
assert_eq!(map.get_key1(&"bearland"), Some(&16));
Sourcepub fn or_insert_with_key2<F: FnOnce(&K2) -> V>(self, default: F) -> &'a mut V
pub fn or_insert_with_key2<F: FnOnce(&K2) -> V>(self, default: F) -> &'a mut V
Ensures a value is in the entry by inserting, if empty, the result of the default function.
This method allows generating key-derived (with using key # 2 K2
) value for
insertion by providing the default function a reference to the key that was moved
during the .entry(key)
method call.
The reference to the moved key is provided so that cloning or copying the key is
unnecessary, unlike with .or_insert_with(|| ... )
.
§Examples
use double_map::DHashMap;
let mut map: DHashMap<&str, usize, u64> = DHashMap::new();
match map.entry("poneyland", 10) {
Ok(entry) => {
entry.or_insert_with_key2(|k2| (k2 + 10) as u64);
},
Err(_) => unreachable!(),
}
assert_eq!(map.get_key1(&"poneyland"), Some(&20));
map.entry("bearland", 11).map(
|ent| ent.or_insert_with_key2(|k1| (k1 * 3) as u64)
);
assert_eq!(map.get_key1(&"bearland"), Some(&33));
Sourcepub fn or_insert_with_keys<F: FnOnce(&K1, &K2) -> V>(
self,
default: F,
) -> &'a mut V
pub fn or_insert_with_keys<F: FnOnce(&K1, &K2) -> V>( self, default: F, ) -> &'a mut V
Ensures a value is in the entry by inserting, if empty, the result of the default function.
This method allows generating key-derived (with using key #1 of type K1
and
key # 2 of type K2
) values for insertion by providing the default function
a reference to the keys that were moved during the .entry(key)
method call.
The reference to the moved keys is provided so that cloning or copying the key is
unnecessary, unlike with .or_insert_with(|| ... )
.
§Examples
use double_map::DHashMap;
let mut map: DHashMap<&str, usize, u64> = DHashMap::new();
match map.entry("poneyland", 10) {
Ok(entry) => {
entry.or_insert_with_keys(|k1, k2| (k1.chars().count() + k2) as u64);
},
Err(_) => unreachable!(),
}
assert_eq!(map.get_key1(&"poneyland"), Some(&19));
map.entry("bearland", 11).map(
|ent| ent.or_insert_with_keys(|k1, k2| (k1.chars().count() + k2 * 3) as u64)
);
assert_eq!(map.get_key1(&"bearland"), Some(&41));
Source§impl<'a, K1, K2, V> Entry<'a, K1, K2, V>
impl<'a, K1, K2, V> Entry<'a, K1, K2, V>
Sourcepub fn key1(&self) -> &K1
pub fn key1(&self) -> &K1
Returns a reference to this entry’s first key (key #1).
§Examples
use double_map::DHashMap;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
// It is VacantEntry
match map.entry("poneyland", 0) {
Ok(entry) => {
// key equal to provided one
assert_eq!(entry.key1(), &"poneyland");
// we insert some value
entry.or_insert(25);
},
Err(_) => unreachable!(),
}
// As we can see, now this element exists
assert_eq!(map.get_key1(&"poneyland"), Some(&25));
// So now it is OccupiedEntry
match map.entry("poneyland", 0) {
Ok(entry) => {
// And key equals to provided one too
assert_eq!(entry.key1(), &"poneyland");
entry.or_insert(25);
},
Err(_) => unreachable!(),
}
Sourcepub fn key2(&self) -> &K2
pub fn key2(&self) -> &K2
Returns a reference to this entry’s second key (key #2).
§Examples
use double_map::DHashMap;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
// It is VacantEntry
match map.entry("poneyland", 10) {
Ok(entry) => {
// key equal to provided one
assert_eq!(entry.key2(), &10);
// we insert some value
entry.or_insert(25);
},
Err(_) => unreachable!(),
}
// As we can see, now this element exists
assert_eq!(map.get_key2(&10), Some(&25));
// So now it is OccupiedEntry
match map.entry("poneyland", 10) {
Ok(entry) => {
// And key equals to provided one too
assert_eq!(entry.key2(), &10);
entry.or_insert(25);
},
Err(_) => unreachable!(),
}
Sourcepub fn keys(&self) -> (&K1, &K2)
pub fn keys(&self) -> (&K1, &K2)
Returns a reference to this entry’s keys. Return tuple of type (&’a K1, &’a K2).
§Examples
use double_map::DHashMap;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
// It is VacantEntry
match map.entry("poneyland", 10) {
Ok(entry) => {
// keys equal to provided one
assert_eq!(entry.keys(), (&"poneyland", &10));
// we insert some value
entry.or_insert(25);
},
Err(_) => unreachable!(),
}
// As we can see, now this element exists
assert_eq!(map.get_key1(&"poneyland"), Some(&25));
// So now it is OccupiedEntry
match map.entry("poneyland", 10) {
Ok(entry) => {
// And keys equal to provided one too
assert_eq!(entry.keys(), (&"poneyland", &10));
entry.or_insert(25);
},
Err(_) => unreachable!(),
}
Sourcepub fn and_modify<F>(self, f: F) -> Self
pub fn and_modify<F>(self, f: F) -> Self
Provides in-place mutable access to an occupied entry before any potential inserts into the map.
§Examples
use double_map::DHashMap;
let mut map: DHashMap<&str, u32, u32> = DHashMap::new();
map.entry("poneyland", 1).map( |entry|
entry.and_modify(|value| { *value += 100 })
.or_insert(42)
);
assert_eq!(map.get_key1(&"poneyland"), Some(&42));
map.entry("poneyland", 1).map( |entry|
entry.and_modify(|value| { *value += 100 })
.or_insert(42)
);
assert_eq!(map.get_key1(&"poneyland"), Some(&142));
Source§impl<'a, K1: Clone, K2: Clone, V: Default> Entry<'a, K1, K2, V>
impl<'a, K1: Clone, K2: Clone, V: Default> Entry<'a, K1, K2, V>
Sourcepub fn or_default(self) -> &'a mut V
pub fn or_default(self) -> &'a mut V
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 double_map::DHashMap;
let mut map: DHashMap<&str, usize, Option<u32>> = DHashMap::new();
map.entry("poneyland", 1).map(|entry| entry.or_default());
assert_eq!(map.get_key1(&"poneyland"), Option::<&Option<u32>>::Some(&None));