Skip to content

Commit 6033fa1

Browse files
committed
Auto merge of #429 - Amanieu:raw_cleanup, r=Amanieu
Clean up `RawTable` API This cleans up various issues in the `RawTable` API: - `RawIter::{reflect_insert, reflect_remove}` are now unsafe. - `RawTable::find_potential` is renamed to `find_or_find_insert_slot` and returns an `InsertSlot`. - `RawTable::remove` now also returns an `InsertSlot`. - `InsertSlot` can be used to insert an element with `RawTable::insert_in_slot`. Fixes #412
2 parents 89184d4 + cc51a5f commit 6033fa1

File tree

3 files changed

+157
-128
lines changed

3 files changed

+157
-128
lines changed

src/map.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,19 +1783,16 @@ where
17831783
#[cfg_attr(feature = "inline-more", inline)]
17841784
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
17851785
let hash = make_insert_hash::<K, S>(&self.hash_builder, &k);
1786-
self.table
1787-
.reserve(1, make_hasher::<_, V, S>(&self.hash_builder));
1788-
1789-
unsafe {
1790-
let (index, found) = self.table.find_potential(hash, equivalent_key(&k));
1791-
1792-
let bucket = self.table.bucket(index);
1793-
1794-
if found {
1795-
Some(mem::replace(&mut bucket.as_mut().1, v))
1796-
} else {
1797-
self.table.mark_inserted(index, hash);
1798-
bucket.write((k, v));
1786+
let hasher = make_hasher::<_, V, S>(&self.hash_builder);
1787+
match self
1788+
.table
1789+
.find_or_find_insert_slot(hash, equivalent_key(&k), hasher)
1790+
{
1791+
Ok(bucket) => Some(mem::replace(unsafe { &mut bucket.as_mut().1 }, v)),
1792+
Err(slot) => {
1793+
unsafe {
1794+
self.table.insert_in_slot(hash, slot, (k, v));
1795+
}
17991796
None
18001797
}
18011798
}
@@ -2178,7 +2175,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
21782175
/// {
21792176
/// let raw_table = map.raw_table_mut();
21802177
/// match raw_table.find(hash, is_match) {
2181-
/// Some(bucket) => Some(unsafe { raw_table.remove(bucket) }),
2178+
/// Some(bucket) => Some(unsafe { raw_table.remove(bucket).0 }),
21822179
/// None => None,
21832180
/// }
21842181
/// }
@@ -2801,7 +2798,7 @@ impl<K, V, A: Allocator + Clone> ExtractIfInner<'_, K, V, A> {
28012798
for item in &mut self.iter {
28022799
let &mut (ref key, ref mut value) = item.as_mut();
28032800
if f(key, value) {
2804-
return Some(self.table.remove(item));
2801+
return Some(self.table.remove(item).0);
28052802
}
28062803
}
28072804
}
@@ -3922,7 +3919,7 @@ impl<'a, K, V, S, A: Allocator + Clone> RawOccupiedEntryMut<'a, K, V, S, A> {
39223919
/// ```
39233920
#[cfg_attr(feature = "inline-more", inline)]
39243921
pub fn remove_entry(self) -> (K, V) {
3925-
unsafe { self.table.remove(self.elem) }
3922+
unsafe { self.table.remove(self.elem).0 }
39263923
}
39273924

39283925
/// Provides shared access to the key and owned access to the value of
@@ -5295,7 +5292,7 @@ impl<'a, K, V, S, A: Allocator + Clone> OccupiedEntry<'a, K, V, S, A> {
52955292
/// ```
52965293
#[cfg_attr(feature = "inline-more", inline)]
52975294
pub fn remove_entry(self) -> (K, V) {
5298-
unsafe { self.table.table.remove(self.elem) }
5295+
unsafe { self.table.table.remove(self.elem).0 }
52995296
}
53005297

53015298
/// Gets a reference to the value in the entry.
@@ -6017,7 +6014,7 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator + Clone> OccupiedEntryRef<'a, 'b,
60176014
/// ```
60186015
#[cfg_attr(feature = "inline-more", inline)]
60196016
pub fn remove_entry(self) -> (K, V) {
6020-
unsafe { self.table.table.remove(self.elem) }
6017+
unsafe { self.table.table.remove(self.elem).0 }
60216018
}
60226019

60236020
/// Gets a reference to the value in the entry.
@@ -8365,7 +8362,7 @@ mod test_map {
83658362
let e = map.table.find(hash_value, |q| q.0.eq(&i));
83668363
if let Some(e) = e {
83678364
it.reflect_remove(&e);
8368-
let t = map.table.remove(e);
8365+
let t = map.table.remove(e).0;
83698366
removed.push(t);
83708367
left -= 1;
83718368
} else {

0 commit comments

Comments
 (0)