Skip to content

Commit 86f4e37

Browse files
committed
add low-level HashTable API
1 parent dd6f585 commit 86f4e37

7 files changed

Lines changed: 3351 additions & 3172 deletions

File tree

src/map.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,14 @@ use std::collections::hash_map::RandomState;
77
use std::fmt;
88
use std::hash::{BuildHasher, Hash};
99
use std::marker::PhantomData;
10-
use std::mem::ManuallyDrop;
1110

1211
/// A concurrent hash table.
1312
///
1413
/// Most hash table operations require a [`Guard`](crate::Guard), which can be acquired through
1514
/// [`HashMap::guard`] or using the [`HashMap::pin`] API. See the [crate-level documentation](crate#usage)
1615
/// for details.
1716
pub struct HashMap<K, V, S = RandomState> {
18-
raw: ManuallyDrop<raw::HashMap<K, V, S>>,
19-
}
20-
21-
impl<K, V, S> Drop for HashMap<K, V, S> {
22-
fn drop(&mut self) {
23-
// Safety: We don't access `self` after taking the inner map.
24-
raw::drop_map(unsafe { ManuallyDrop::take(&mut self.raw) });
25-
}
17+
raw: raw::HashMap<K, V, S>,
2618
}
2719

2820
// Safety: `HashMap` acts as a single-threaded collection on a single thread.
@@ -139,12 +131,7 @@ impl<K, V, S> HashMapBuilder<K, V, S> {
139131
/// Construct a [`HashMap`] from the builder, using the configured options.
140132
pub fn build(self) -> HashMap<K, V, S> {
141133
HashMap {
142-
raw: ManuallyDrop::new(raw::HashMap::new(
143-
self.capacity,
144-
self.hasher,
145-
self.collector,
146-
self.resize_mode,
147-
)),
134+
raw: raw::HashMap::new(self.capacity, self.hasher, self.collector, self.resize_mode),
148135
}
149136
}
150137
}
@@ -311,12 +298,12 @@ impl<K, V, S> HashMap<K, V, S> {
311298
/// ```
312299
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S> {
313300
HashMap {
314-
raw: ManuallyDrop::new(raw::HashMap::new(
301+
raw: raw::HashMap::new(
315302
capacity,
316303
hash_builder,
317304
Collector::default(),
318305
ResizeMode::default(),
319-
)),
306+
),
320307
}
321308
}
322309

@@ -1133,13 +1120,8 @@ impl<K, V, S> IntoIterator for HashMap<K, V, S> {
11331120
/// let v: Vec<(&str, i32)> = map.into_iter().collect();
11341121
/// ```
11351122
fn into_iter(self) -> Self::IntoIter {
1136-
let mut map = ManuallyDrop::new(self);
1137-
1138-
// Safety: We don't access `self` after taking the inner map.
1139-
let raw = unsafe { ManuallyDrop::take(&mut map.raw) };
1140-
11411123
IntoIter {
1142-
raw: raw.into_iter(),
1124+
raw: self.raw.into_iter(),
11431125
}
11441126
}
11451127
}

src/raw/alloc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::marker::PhantomData;
33
use std::sync::atomic::{AtomicPtr, AtomicU8, Ordering};
44
use std::{alloc, mem, ptr};
55

6-
use super::{probe, State};
6+
use super::probe;
7+
use super::table::{meta, State};
78

89
// A hash-table laid out in a single allocation.
910
//
@@ -88,7 +89,7 @@ impl<T> Table<T> {
8889
// Initialize the meta table.
8990
ptr.add(mem::size_of::<TableLayout<T>>())
9091
.cast::<u8>()
91-
.write_bytes(super::meta::EMPTY, len);
92+
.write_bytes(meta::EMPTY, len);
9293
}
9394

9495
Table {

0 commit comments

Comments
 (0)