Skip to content

Commit 837f5cc

Browse files
committed
Use table entries (requires rust-lang#669)
1 parent 541dd95 commit 837f5cc

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/map.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,9 +2812,8 @@ impl<K: Debug, V: Debug, S, A: Allocator> Debug for Entry<'_, K, V, S, A> {
28122812
/// assert_eq!(map.len(), 2);
28132813
/// ```
28142814
pub struct OccupiedEntry<'a, K, V, S = DefaultHashBuilder, A: Allocator = Global> {
2815-
hash: u64,
2816-
elem: Bucket<(K, V)>,
2817-
table: &'a mut HashMap<K, V, S, A>,
2815+
inner: hash_table::OccupiedEntry<'a, (K, V), A>,
2816+
marker: PhantomData<&'a mut S>,
28182817
}
28192818

28202819
unsafe impl<K, V, S, A> Send for OccupiedEntry<'_, K, V, S, A>
@@ -3817,7 +3816,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
38173816
/// ```
38183817
#[cfg_attr(feature = "inline-more", inline)]
38193818
pub fn key(&self) -> &K {
3820-
unsafe { &self.elem.as_ref().0 }
3819+
&self.inner.get().0
38213820
}
38223821

38233822
/// Take the ownership of the key and value from the map.
@@ -3846,7 +3845,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
38463845
/// ```
38473846
#[cfg_attr(feature = "inline-more", inline)]
38483847
pub fn remove_entry(self) -> (K, V) {
3849-
unsafe { self.table.table.raw.remove(self.elem).0 }
3848+
self.inner.remove().0
38503849
}
38513850

38523851
/// Gets a reference to the value in the entry.
@@ -3867,7 +3866,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
38673866
/// ```
38683867
#[cfg_attr(feature = "inline-more", inline)]
38693868
pub fn get(&self) -> &V {
3870-
unsafe { &self.elem.as_ref().1 }
3869+
&self.inner.get().1
38713870
}
38723871

38733872
/// Gets a mutable reference to the value in the entry.
@@ -3899,7 +3898,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
38993898
/// ```
39003899
#[cfg_attr(feature = "inline-more", inline)]
39013900
pub fn get_mut(&mut self) -> &mut V {
3902-
unsafe { &mut self.elem.as_mut().1 }
3901+
&mut self.inner.get_mut().1
39033902
}
39043903

39053904
/// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
@@ -3930,7 +3929,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
39303929
/// ```
39313930
#[cfg_attr(feature = "inline-more", inline)]
39323931
pub fn into_mut(self) -> &'a mut V {
3933-
unsafe { &mut self.elem.as_mut().1 }
3932+
&mut self.inner.into_mut().1
39343933
}
39353934

39363935
/// Sets the value of the entry, and returns the entry's old value.
@@ -4039,27 +4038,23 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
40394038
unsafe {
40404039
let mut spare_key = None;
40414040

4042-
self.table
4043-
.table
4044-
.raw
4045-
.replace_bucket_with(self.elem.clone(), |(key, value)| {
4041+
match self.inner
4042+
.replace_entry_with(|(key, value)| {
40464043
if let Some(new_value) = f(&key, value) {
40474044
Some((key, new_value))
40484045
} else {
40494046
spare_key = Some(key);
40504047
None
40514048
}
4052-
});
4053-
4054-
if let Some(key) = spare_key {
4055-
Entry::Vacant(VacantEntry {
4056-
hash: self.hash,
4057-
key,
4058-
table: self.table,
4059-
})
4060-
} else {
4061-
Entry::Occupied(self)
4062-
}
4049+
}) {
4050+
hash_table::Entry::Occupied(entry) => Entry::Occupied(OccupiedEntry {
4051+
inner: entry
4052+
}),
4053+
hash_table::Entry::Vacant(entry) => Entry::Vacant(VacantEntry {
4054+
inner: entry,
4055+
key,
4056+
})
4057+
}
40634058
}
40644059
}
40654060
}

0 commit comments

Comments
 (0)