pub struct RawVacantEntryMut<'a, K, V, S, A: Allocator = Global> { /* private fields */ }Expand description
A view into a vacant entry in a HashMap.
It is part of the RawEntryMut enum.
§Examples
use core::hash::{BuildHasher, Hash};
use rune::alloc::hash_map::{RawEntryMut, RawVacantEntryMut};
use rune::alloc::HashMap;
let mut map = HashMap::<&str, i32>::new();
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_v: RawVacantEntryMut<_, _, _> = match map.raw_entry_mut().from_key(&"a") {
RawEntryMut::Vacant(view) => view,
RawEntryMut::Occupied(_) => unreachable!(),
};
raw_v.try_insert("a", 10)?;
assert!(map[&"a"] == 10 && map.len() == 1);
// Nonexistent key (insert and update)
let hash = compute_hash(map.hasher(), &"b");
match map.raw_entry_mut().from_key_hashed_nocheck(hash, &"b") {
RawEntryMut::Occupied(_) => unreachable!(),
RawEntryMut::Vacant(view) => {
let (k, value) = view.try_insert("b", 2)?;
assert_eq!((*k, *value), ("b", 2));
*value = 20;
}
}
assert!(map[&"b"] == 20 && map.len() == 2);
let hash = compute_hash(map.hasher(), &"c");
match map.raw_entry_mut().from_hash(hash, |q| *q == "c") {
RawEntryMut::Occupied(_) => unreachable!(),
RawEntryMut::Vacant(view) => {
assert_eq!(view.try_insert("c", 30)?, (&mut "c", &mut 30));
}
}
assert!(map[&"c"] == 30 && map.len() == 3);Implementations§
Source§impl<'a, K, V, S, A> RawVacantEntryMut<'a, K, V, S, A>where
A: Allocator,
impl<'a, K, V, S, A> RawVacantEntryMut<'a, K, V, S, A>where
A: Allocator,
Sourcepub fn try_insert(
self,
key: K,
value: V,
) -> Result<(&'a mut K, &'a mut V), Error>where
K: Hash,
S: BuildHasher,
pub fn try_insert(
self,
key: K,
value: V,
) -> Result<(&'a mut K, &'a mut V), Error>where
K: Hash,
S: BuildHasher,
Sets the value of the entry with the VacantEntry’s key, and returns a mutable reference to it.
§Examples
use rune::alloc::hash_map::{HashMap, RawEntryMut};
let mut map: HashMap<&str, u32> = [("a", 100), ("b", 200)].try_into()?;
match map.raw_entry_mut().from_key(&"c") {
RawEntryMut::Occupied(_) => panic!(),
RawEntryMut::Vacant(v) => assert_eq!(v.try_insert("c", 300)?, (&mut "c", &mut 300)),
}
assert_eq!(map[&"c"], 300);Sourcepub fn try_insert_hashed_nocheck(
self,
hash: u64,
key: K,
value: V,
) -> Result<(&'a mut K, &'a mut V), Error>where
K: Hash,
S: BuildHasher,
pub fn try_insert_hashed_nocheck(
self,
hash: u64,
key: K,
value: V,
) -> Result<(&'a mut K, &'a mut V), Error>where
K: Hash,
S: BuildHasher,
Sets the value of the entry with the VacantEntry’s key, and returns a mutable reference to it.
§Examples
use core::hash::{BuildHasher, Hash};
use rune::alloc::hash_map::{HashMap, RawEntryMut};
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 mut map: HashMap<&str, u32> = [("a", 100), ("b", 200)].try_into()?;
let key = "c";
let hash = compute_hash(map.hasher(), &key);
match map.raw_entry_mut().from_key_hashed_nocheck(hash, &key) {
RawEntryMut::Occupied(_) => panic!(),
RawEntryMut::Vacant(v) => assert_eq!(
v.try_insert_hashed_nocheck(hash, key, 300)?,
(&mut "c", &mut 300)
),
}
assert_eq!(map[&"c"], 300);Sourcepub fn try_insert_with_hasher<H>(
self,
hash: u64,
key: K,
value: V,
hasher: H,
) -> Result<(&'a mut K, &'a mut V), Error>
pub fn try_insert_with_hasher<H>( self, hash: u64, key: K, value: V, hasher: H, ) -> Result<(&'a mut K, &'a mut V), Error>
Set the value of an entry with a custom hasher function.
§Examples
use core::hash::{BuildHasher, Hash};
use rune::alloc::hash_map::{HashMap, RawEntryMut};
use rune::alloc::prelude::*;
fn make_hasher<K, S>(hash_builder: &S) -> impl Fn(&K) -> u64 + '_
where
K: Hash + ?Sized,
S: BuildHasher,
{
move |key: &K| {
use core::hash::Hasher;
let mut state = hash_builder.build_hasher();
key.hash(&mut state);
state.finish()
}
}
let mut map: HashMap<&str, u32> = HashMap::new();
let key = "a";
let hash_builder = map.hasher().clone();
let hash = make_hasher(&hash_builder)(&key);
match map.raw_entry_mut().from_hash(hash, |q| q == &key) {
RawEntryMut::Occupied(_) => panic!(),
RawEntryMut::Vacant(v) => assert_eq!(
v.try_insert_with_hasher(hash, key, 100, make_hasher(&hash_builder))?,
(&mut "a", &mut 100)
),
}
map.try_extend([("b", 200), ("c", 300), ("d", 400), ("e", 500), ("f", 600)])?;
assert_eq!(map[&"a"], 100);Trait Implementations§
Auto Trait Implementations§
impl<'a, K, V, S, A> Freeze for RawVacantEntryMut<'a, K, V, S, A>
impl<'a, K, V, S, A> RefUnwindSafe for RawVacantEntryMut<'a, K, V, S, A>
impl<'a, K, V, S, A> Send for RawVacantEntryMut<'a, K, V, S, A>
impl<'a, K, V, S, A> Sync for RawVacantEntryMut<'a, K, V, S, A>
impl<'a, K, V, S, A> Unpin for RawVacantEntryMut<'a, K, V, S, A>
impl<'a, K, V, S, A = Global> !UnwindSafe for RawVacantEntryMut<'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