pub struct RawOccupiedEntryMut<'a, K, V, S, A = Global>where
A: Allocator,{ /* private fields */ }Expand description
A view into an occupied entry in a HashMap.
It is part of the RawEntryMut enum.
§Examples
use core::hash::{BuildHasher, Hash};
use rune::alloc::hash_map::{RawEntryMut, RawOccupiedEntryMut};
use rune::alloc::HashMap;
use rune::alloc::prelude::*;
let mut map = HashMap::new();
map.try_extend([("a", 10), ("b", 20), ("c", 30)])?;
fn compute_hash<K, S>(hash_builder: &S, key: &K) -> u64
where
K: Hash + ?Sized,
S: BuildHasher,
{
use core::hash::Hasher;
let mut state = hash_builder.build_hasher();
key.hash(&mut state);
state.finish()
}
let _raw_o: RawOccupiedEntryMut<_, _, _> = map.raw_entry_mut().from_key(&"a").try_insert("a", 100)?;
assert_eq!(map.len(), 3);
// Existing key (insert and update)
match map.raw_entry_mut().from_key(&"a") {
RawEntryMut::Vacant(_) => unreachable!(),
RawEntryMut::Occupied(mut view) => {
assert_eq!(view.get(), &100);
let v = view.get_mut();
let new_v = (*v) * 10;
*v = new_v;
assert_eq!(view.insert(1111), 1000);
}
}
assert_eq!(map[&"a"], 1111);
assert_eq!(map.len(), 3);
// Existing key (take)
let hash = compute_hash(map.hasher(), &"c");
match map.raw_entry_mut().from_key_hashed_nocheck(hash, &"c") {
RawEntryMut::Vacant(_) => unreachable!(),
RawEntryMut::Occupied(view) => {
assert_eq!(view.remove_entry(), ("c", 30));
}
}
assert_eq!(map.raw_entry().from_key(&"c"), None);
assert_eq!(map.len(), 2);
let hash = compute_hash(map.hasher(), &"b");
match map.raw_entry_mut().from_hash(hash, |q| *q == "b") {
RawEntryMut::Vacant(_) => unreachable!(),
RawEntryMut::Occupied(view) => {
assert_eq!(view.remove_entry(), ("b", 20));
}
}
assert_eq!(map.get(&"b"), None);
assert_eq!(map.len(), 1);Auto Trait Implementations§
impl<'a, K, V, S, A> Freeze for RawOccupiedEntryMut<'a, K, V, S, A>
impl<'a, K, V, S, A> RefUnwindSafe for RawOccupiedEntryMut<'a, K, V, S, A>
impl<'a, K, V, S, A> Unpin for RawOccupiedEntryMut<'a, K, V, S, A>
impl<'a, K, V, S, A = Global> !UnwindSafe for RawOccupiedEntryMut<'a, K, V, S, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more