From 37139216a7881c952964b420f7d10a0cc0ef7b38 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 26 Mar 2020 18:19:58 -0700 Subject: [PATCH 1/2] feat: add OccupiedEntry.get_entry_mut, VacantEntry.insert_entry --- src/map.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/map.rs b/src/map.rs index b346b77d..5f4e0298 100644 --- a/src/map.rs +++ b/src/map.rs @@ -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 { @@ -811,6 +815,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::(value) + } else { + self.insert_entry_impl::(value) + } + } + fn insert_impl(self, value: V) -> &'a mut V where Sz: Size, @@ -825,6 +837,22 @@ impl<'a, K, V> VacantEntry<'a, K, V> { self.map.insert_phase_2::(self.probe, old_pos); &mut { self.map }.entries[index].value } + + fn insert_entry_impl(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::(index, self.hash); + self.map.insert_phase_2::(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> { From dc2665fe2b8af07fe2a0d0dd9b85a92399bbed98 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 26 Mar 2020 18:35:27 -0700 Subject: [PATCH 2/2] add: OccupiedEntry.into_entry_mut too --- src/map.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/map.rs b/src/map.rs index 5f4e0298..9ae9beaa 100644 --- a/src/map.rs +++ b/src/map.rs @@ -711,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 {