Skip to main content

RawEntryMut

Enum RawEntryMut 

Source
pub enum RawEntryMut<'a, K, V, S, A = Global>
where A: Allocator,
{ Occupied(RawOccupiedEntryMut<'a, K, V, S, A>), Vacant(RawVacantEntryMut<'a, K, V, S, A>), }
Expand description

A view into a single entry in a map, which may either be vacant or occupied.

This is a lower-level version of Entry.

This enum is constructed through the raw_entry_mut method on HashMap, then calling one of the methods of that RawEntryBuilderMut.

§Examples

use core::hash::{BuildHasher, Hash};

use rune::alloc::prelude::*;
use rune::alloc::hash_map::{HashMap, RawEntryMut, RawOccupiedEntryMut};

let mut map = HashMap::new();
map.try_extend([('a', 1), ('b', 2), ('c', 3)])?;
assert_eq!(map.len(), 3);

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()
}

// Existing key (try_insert)
let raw: RawEntryMut<_, _, _> = map.raw_entry_mut().from_key(&'a');
let _raw_o: RawOccupiedEntryMut<_, _, _> = raw.try_insert('a', 10)?;
assert_eq!(map.len(), 3);

// Nonexistent key (try_insert)
map.raw_entry_mut().from_key(&'d').try_insert('d', 40)?;
assert_eq!(map.len(), 4);

// Existing key (or_try_insert)
let hash = compute_hash(map.hasher(), &'b');
let kv = map
    .raw_entry_mut()
    .from_key_hashed_nocheck(hash, &'b')
    .or_try_insert('b', 20)?;
assert_eq!(kv, (&mut 'b', &mut 2));
*kv.1 = 20;
assert_eq!(map.len(), 4);

// Nonexistent key (or_try_insert)
let hash = compute_hash(map.hasher(), &'e');
let kv = map
    .raw_entry_mut()
    .from_key_hashed_nocheck(hash, &'e')
    .or_try_insert('e', 50)?;
assert_eq!(kv, (&mut 'e', &mut 50));
assert_eq!(map.len(), 5);

// Existing key (or_try_insert_with)
let hash = compute_hash(map.hasher(), &'c');
let kv = map
    .raw_entry_mut()
    .from_hash(hash, |q| q == &'c')
    .or_try_insert_with(|| ('c', 30))?;
assert_eq!(kv, (&mut 'c', &mut 3));
*kv.1 = 30;
assert_eq!(map.len(), 5);

// Nonexistent key (or_try_insert_with)
let hash = compute_hash(map.hasher(), &'f');
let kv = map
    .raw_entry_mut()
    .from_hash(hash, |q| q == &'f')
    .or_try_insert_with(|| ('f', 60))?;
assert_eq!(kv, (&mut 'f', &mut 60));
assert_eq!(map.len(), 6);

println!("Our HashMap: {:?}", map);

let mut vec: Vec<_> = map.iter().map(|(&k, &v)| (k, v)).try_collect()?;
// The `Iter` iterator produces items in arbitrary order, so the
// items must be sorted to test them against a sorted array.
vec.sort_unstable();
assert_eq!(vec, [('a', 10), ('b', 20), ('c', 30), ('d', 40), ('e', 50), ('f', 60)]);

Variants§

§

Occupied(RawOccupiedEntryMut<'a, K, V, S, A>)

An occupied entry.

§Examples

use rune::alloc::hash_map::RawEntryMut;
use rune::alloc::HashMap;

let mut map: HashMap<_, _> = [("a", 100), ("b", 200)].try_into()?;

match map.raw_entry_mut().from_key(&"a") {
    RawEntryMut::Vacant(_) => unreachable!(),
    RawEntryMut::Occupied(_) => { }
}
§

Vacant(RawVacantEntryMut<'a, K, V, S, A>)

A vacant entry.

§Examples

use rune::alloc::{hash_map::RawEntryMut, HashMap};
let mut map: HashMap<&str, i32> = HashMap::new();

match map.raw_entry_mut().from_key("a") {
    RawEntryMut::Occupied(_) => unreachable!(),
    RawEntryMut::Vacant(_) => { }
}