pub struct OccupiedEntryRef<'a, 'b, K, Q, V, S, A = Global>{ /* private fields */ }Expand description
A view into an occupied entry in a HashMap.
It is part of the EntryRef enum.
§Examples
use rune::alloc::hash_map::{EntryRef, HashMap, OccupiedEntryRef};
use rune::alloc::prelude::*;
let mut map = HashMap::new();
map.try_extend([
("a".try_to_owned()?, 10),
("b".try_to_owned()?, 20),
("c".try_to_owned()?, 30)
])?;
let key = String::try_from("a")?;
let _entry_o: OccupiedEntryRef<_, _, _, _> = map.entry_ref(&key).try_insert(100)?;
assert_eq!(map.len(), 3);
// Existing key (insert and update)
match map.entry_ref("a") {
EntryRef::Vacant(_) => unreachable!(),
EntryRef::Occupied(mut view) => {
assert_eq!(view.get(), &100);
let v = view.get_mut();
*v *= 10;
assert_eq!(view.insert(1111), 1000);
}
}
assert_eq!(map["a"], 1111);
assert_eq!(map.len(), 3);
// Existing key (take)
match map.entry_ref("c") {
EntryRef::Vacant(_) => unreachable!(),
EntryRef::Occupied(view) => {
assert_eq!(view.remove_entry(), ("c".try_to_owned()?, 30));
}
}
assert_eq!(map.get("c"), None);
assert_eq!(map.len(), 2);Auto Trait Implementations§
impl<'a, 'b, K, Q, V, S, A> Freeze for OccupiedEntryRef<'a, 'b, K, Q, V, S, A>
impl<'a, 'b, K, Q, V, S, A> RefUnwindSafe for OccupiedEntryRef<'a, 'b, K, Q, V, S, A>where
K: RefUnwindSafe,
S: RefUnwindSafe,
Q: RefUnwindSafe + ?Sized,
A: RefUnwindSafe,
V: RefUnwindSafe,
impl<'a, 'b, K, Q, V, S, A> Unpin for OccupiedEntryRef<'a, 'b, K, Q, V, S, A>
impl<'a, 'b, K, Q, V, S, A = Global> !UnwindSafe for OccupiedEntryRef<'a, 'b, K, Q, 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