pub struct RawVacantEntryMut<'a, K, V, S, A: Allocator + Clone = Global> {
    table: &'a mut RawTable<(K, V), A>,
    hash_builder: &'a S,
}
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 hashbrown::hash_map::{HashMap, RawEntryMut, RawVacantEntryMut};

let mut map = HashMap::<&str, i32>::new();

fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
    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.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.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.insert("c", 30), (&mut "c", &mut 30));
    }
}
assert!(map[&"c"] == 30 && map.len() == 3);

Fields§

§table: &'a mut RawTable<(K, V), A>§hash_builder: &'a S

Implementations§

source§

impl<'a, K, V, S, A: Allocator + Clone> RawVacantEntryMut<'a, K, V, S, A>

source

pub fn insert(self, key: K, value: V) -> (&'a mut K, &'a mut V)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 hashbrown::hash_map::{HashMap, RawEntryMut};

let mut map: HashMap<&str, u32> = [("a", 100), ("b", 200)].into();

match map.raw_entry_mut().from_key(&"c") {
    RawEntryMut::Occupied(_) => panic!(),
    RawEntryMut::Vacant(v) => assert_eq!(v.insert("c", 300), (&mut "c", &mut 300)),
}

assert_eq!(map[&"c"], 300);
source

pub fn insert_hashed_nocheck( self, hash: u64, key: K, value: V ) -> (&'a mut K, &'a mut V)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 hashbrown::hash_map::{HashMap, RawEntryMut};

fn compute_hash<K: Hash + ?Sized, S: BuildHasher>(hash_builder: &S, key: &K) -> u64 {
    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)].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.insert_hashed_nocheck(hash, key, 300),
        (&mut "c", &mut 300)
    ),
}

assert_eq!(map[&"c"], 300);
source

pub fn insert_with_hasher<H>( self, hash: u64, key: K, value: V, hasher: H ) -> (&'a mut K, &'a mut V)where H: Fn(&K) -> u64,

Set the value of an entry with a custom hasher function.

Examples
use core::hash::{BuildHasher, Hash};
use hashbrown::hash_map::{HashMap, RawEntryMut};

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.insert_with_hasher(hash, key, 100, make_hasher(&hash_builder)),
        (&mut "a", &mut 100)
    ),
}
map.extend([("b", 200), ("c", 300), ("d", 400), ("e", 500), ("f", 600)]);
assert_eq!(map[&"a"], 100);
source

fn insert_entry(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V, S, A>where K: Hash, S: BuildHasher,

Trait Implementations§

source§

impl<K, V, S, A: Allocator + Clone> Debug for RawVacantEntryMut<'_, K, V, S, A>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, K, V, S, A> RefUnwindSafe for RawVacantEntryMut<'a, K, V, S, A>where A: RefUnwindSafe, K: RefUnwindSafe, S: RefUnwindSafe, V: RefUnwindSafe,

§

impl<'a, K, V, S, A> Send for RawVacantEntryMut<'a, K, V, S, A>where A: Send, K: Send, S: Sync, V: Send,

§

impl<'a, K, V, S, A> Sync for RawVacantEntryMut<'a, K, V, S, A>where A: Sync, K: Sync, S: Sync, V: Sync,

§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.