Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add OccupiedEntry.get_entry_mut, VacantEntry.insert_entry #118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,10 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
pub fn get_mut(&mut self) -> &mut V {
&mut self.map.entries[self.index].value
}
pub fn get_entry_mut(&mut self) -> (&K, &mut V) {
let bucket = &mut self.map.entries[self.index];
(&bucket.key, &mut bucket.value)
}

/// Put the new key in the occupied entry's key slot
pub(crate) fn replace_key(self) -> K {
Expand All @@ -707,6 +711,10 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
pub fn into_mut(self) -> &'a mut V {
&mut self.map.entries[self.index].value
}
pub fn into_entry_mut(self) -> (&'a K, &'a mut V) {
let bucket = &mut self.map.entries[self.index];
(&bucket.key, &mut bucket.value)
}

/// Sets the value of the entry to `value`, and returns the entry's old value.
pub fn insert(&mut self, value: V) -> V {
Expand Down Expand Up @@ -811,6 +819,14 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
}
}

pub fn insert_entry(self, value: V) -> (&'a K, &'a mut V) {
if self.map.size_class_is_64bit() {
self.insert_entry_impl::<u64>(value)
} else {
self.insert_entry_impl::<u32>(value)
}
}

fn insert_impl<Sz>(self, value: V) -> &'a mut V
where
Sz: Size,
Expand All @@ -825,6 +841,22 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
self.map.insert_phase_2::<Sz>(self.probe, old_pos);
&mut { self.map }.entries[index].value
}

fn insert_entry_impl<Sz>(self, value: V) -> (&'a K, &'a mut V)
where
Sz: Size,
{
let index = self.map.entries.len();
self.map.entries.push(Bucket {
hash: self.hash,
key: self.key,
value,
});
let old_pos = Pos::with_hash::<Sz>(index, self.hash);
self.map.insert_phase_2::<Sz>(self.probe, old_pos);
let entry = &mut { self.map }.entries[index];
(&entry.key, &mut entry.value)
}
}

impl<'a, K: 'a + fmt::Debug, V: 'a> fmt::Debug for VacantEntry<'a, K, V> {
Expand Down