Correctly drop key on override

This commit is contained in:
Gwilym Kuiper 2022-03-20 13:42:28 +00:00
parent 9c7d9520a7
commit 3ae0e30d3c

View file

@ -21,7 +21,7 @@ struct Node<K, V> {
} }
impl<K, V> Node<K, V> { impl<K, V> Node<K, V> {
fn with_new_key_value(self, new_key: K, new_value: V) -> (Self, Option<V>) { fn with_new_key_value(mut self, new_key: K, new_value: V) -> (Self, Option<V>) {
( (
Self { Self {
hash: self.hash, hash: self.hash,
@ -29,7 +29,7 @@ impl<K, V> Node<K, V> {
key: MaybeUninit::new(new_key), key: MaybeUninit::new(new_key),
value: MaybeUninit::new(new_value), value: MaybeUninit::new(new_value),
}, },
self.get_owned_value(), self.take_key_value().map(|(_, v, _)| v),
) )
} }
@ -67,17 +67,6 @@ impl<K, V> Node<K, V> {
} }
} }
fn get_owned_value(mut self) -> Option<V> {
if self.has_value() {
let value = mem::replace(&mut self.value, MaybeUninit::uninit());
self.distance_to_initial_bucket = -1;
Some(unsafe { value.assume_init() })
} else {
None
}
}
fn key_ref(&self) -> Option<&K> { fn key_ref(&self) -> Option<&K> {
if self.distance_to_initial_bucket >= 0 { if self.distance_to_initial_bucket >= 0 {
Some(unsafe { self.key.assume_init_ref() }) Some(unsafe { self.key.assume_init_ref() })