pub struct OccupiedEntry<'a, K1: 'a, K2: 'a, V: 'a> { /* private fields */ }
Expand description
A view into an occupied entry in a DHashMap
.
It is part of the Entry
enum and OccupiedError
struct.
Implementations§
Source§impl<'a, K1, K2, V> OccupiedEntry<'a, K1, K2, V>
impl<'a, K1, K2, V> OccupiedEntry<'a, K1, K2, V>
Sourcepub fn key1(&self) -> &K1
pub fn key1(&self) -> &K1
Gets a reference to the key #1 of type K1
in the entry.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(oc_entry) => {
assert_eq!(oc_entry.key1(), &"poneyland");
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
Sourcepub fn key2(&self) -> &K2
pub fn key2(&self) -> &K2
Gets a reference to the key #2 of type K2
in the entry.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(oc_entry) => {
assert_eq!(oc_entry.key2(), &0);
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
Sourcepub fn keys(&self) -> (&K1, &K2)
pub fn keys(&self) -> (&K1, &K2)
Gets a reference to the keys of type K1
and K2
in the entry.
Return tuple of type (&'a K1, &'a K2)
.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(oc_entry) => {
assert_eq!(oc_entry.keys(), (&"poneyland", &0));
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
Sourcepub fn remove_entry(self) -> (K1, K2, V)
pub fn remove_entry(self) -> (K1, K2, V)
Take the ownership of the keys and value from the map. Keeps the allocated memory for reuse.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
// So lets create some map and insert some element
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 10);
map.insert("bearland", 1, 11);
// And also reserve some space for additional elements
map.reserve(15);
// Now our map can hold at least 17 elements
let capacity_before_entries_remove = map.capacity();
assert!(capacity_before_entries_remove >= 17);
assert!(map.get_key1("poneyland") == Some(&10) && map.get_key2(&0) == Some(&10));
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(oc_entry) => {
// We delete the entry from the map.
let tuple = oc_entry.remove_entry();
assert_eq!(tuple, ("poneyland", 0, 10));
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
assert!(map.get_key1("poneyland") == None && map.get_key2(&0) == None);
assert!(map.get_key1("bearland") == Some(&11) && map.get_key2(&1) == Some(&11));
if let Ok(entry) = map.entry("bearland", 1) {
match entry {
Entry::Occupied(oc_entry) => {
// We delete the entry from the map.
let tuple = oc_entry.remove_entry();
assert_eq!(tuple, ("bearland", 1, 11));
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
assert!(map.get_key1("bearland") == None && map.get_key2(&1) == None);
// But the capacity of our map isn't changed and still equals to the capacity before
// using `remove_entry` method
assert_eq!(map.capacity(), capacity_before_entries_remove);
Sourcepub fn get(&self) -> &V
pub fn get(&self) -> &V
Gets a reference to the value in the entry.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(oc_entry) => {
assert_eq!(oc_entry.get(), &12);
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
Sourcepub fn get_mut(&mut self) -> &mut V
pub fn get_mut(&mut self) -> &mut V
Gets a mutable reference to the value in the entry.
If you need a reference to the OccupiedEntry
which may outlive the
destruction of the Entry
value, see into_mut
.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
assert_eq!(map.get_key1("poneyland"), Some(&12));
assert_eq!(map.get_key2(&0), Some(&12));
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(mut oc_entry) => {
*oc_entry.get_mut() += 10;
assert_eq!(oc_entry.get(), &22);
// We can use the same Entry multiple times.
*oc_entry.get_mut() += 2;
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
assert_eq!(map.get_key1("poneyland"), Some(&24));
assert_eq!(map.get_key2(&0), Some(&24));
Sourcepub fn into_mut(self) -> &'a mut V
pub fn into_mut(self) -> &'a mut V
Converts the OccupiedEntry
into a mutable reference to the value in the entry
with a lifetime bound to the map itself.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
assert_eq!(map.get_key1("poneyland"), Some(&12));
assert_eq!(map.get_key2(&0), Some(&12));
// Let's create a variable that outlives the OccupiedEntry (with some initial value)
let mut value: &mut i32 = &mut 0;
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(oc_entry) => {
// So we can convert the OccupiedEntry into a mutable reference to the value.
value = oc_entry.into_mut();
*value += 10;
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
// We can use the same reference outside the created oc_entry (OccupiedEntry) scope.
*value += 20;
assert_eq!(map.get_key1("poneyland"), Some(&42)); // 12 + 10 + 20
assert_eq!(map.get_key2(&0), Some(&42));
Sourcepub fn insert(&mut self, value: V) -> V
pub fn insert(&mut self, value: V) -> V
Sets the value of the entry, and returns the entry’s old value.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 12);
assert_eq!(map.get_key1("poneyland"), Some(&12));
assert_eq!(map.get_key2(&0), Some(&12));
// Let's create a variable that holds value
let mut owner: i32 = 100;
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(mut oc_entry) => {
// So we can swap our created owner value with value inside the map.
owner = oc_entry.insert(owner);
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
assert_eq!(owner, 12);
assert_eq!(map.get_key1("poneyland"), Some(&100));
assert_eq!(map.get_key2(&0), Some(&100));
Sourcepub fn remove(self) -> V
pub fn remove(self) -> V
Take the value out of the entry (map), and return it. Keeps the allocated memory for reuse.
§Examples
use double_map::DHashMap;
use double_map::dhash_map::Entry;
// So lets create some map and insert some element
let mut map: DHashMap<&str, u32, i32> = DHashMap::new();
map.insert("poneyland", 0, 10);
map.insert("bearland", 1, 11);
// And also reserve some space for additional elements
map.reserve(15);
// Now our map can hold at least 17 elements
let capacity_before_remove = map.capacity();
assert!(capacity_before_remove >= 17);
assert!(map.get_key1("poneyland") == Some(&10) && map.get_key2(&0) == Some(&10));
if let Ok(entry) = map.entry("poneyland", 0) {
match entry {
Entry::Occupied(oc_entry) => {
// We delete the entry from the map.
let value = oc_entry.remove();
assert_eq!(value, 10);
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
assert!(map.get_key1("poneyland") == None && map.get_key2(&0) == None);
assert!(map.get_key1("bearland") == Some(&11) && map.get_key2(&1) == Some(&11));
if let Ok(entry) = map.entry("bearland", 1) {
match entry {
Entry::Occupied(oc_entry) => {
// We delete the entry from the map.
let value = oc_entry.remove();
assert_eq!(value, 11);
}
Entry::Vacant(_) => panic!("Something go wrong!!!")
}
}
assert!(map.get_key1("bearland") == None && map.get_key2(&1) == None);
// But the capacity of our map isn't changed and still equals to the capacity before
// using `remove_entry` method
assert_eq!(map.capacity(), capacity_before_remove);