Skip to content

Commit 01bff89

Browse files
committed
add low-level HashTable API
1 parent dd6f585 commit 01bff89

9 files changed

Lines changed: 3371 additions & 3184 deletions

File tree

src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@
215215
//! [benchmarks]: https://github.com/ibraheemdev/papaya/blob/master/BENCHMARKS.md
216216
217217
#![deny(
218-
missing_debug_implementations,
219-
missing_docs,
218+
// TODO missing_debug_implementations,
219+
// TODO missing_docs,
220220
dead_code,
221221
unsafe_op_in_unsafe_fn
222222
)]
@@ -241,3 +241,8 @@ pub use map::{
241241
};
242242
pub use seize::{Guard, LocalGuard, OwnedGuard};
243243
pub use set::{HashSet, HashSetBuilder, HashSetRef};
244+
245+
/// A low-level hash table interface.
246+
pub mod table {
247+
pub use crate::raw::table::{HashTable, InsertResult, IntoIter, Iter, IterMut};
248+
}

src/map.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
use crate::raw::utils::MapGuard;
2-
use crate::raw::{self, InsertResult};
2+
use crate::raw::{map, map::InsertResult};
33
use crate::Equivalent;
44
use seize::{Collector, Guard, LocalGuard, OwnedGuard};
55

66
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: map::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: map::HashMap::new(self.capacity, self.hasher, self.collector, self.resize_mode),
148135
}
149136
}
150137
}
@@ -264,7 +251,7 @@ impl<K, V, S> HashMap<K, V, S> {
264251
///
265252
/// Warning: `hash_builder` is normally randomly generated, and is designed
266253
/// to allow HashMaps to be resistant to attacks that cause many collisions
267-
/// and very actuallypoor performance. Setting it manually using this function can
254+
/// and very poor performance. Setting it manually using this function can
268255
/// expose a DoS attack vector.
269256
///
270257
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
@@ -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: map::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
}
@@ -1650,7 +1632,7 @@ where
16501632
///
16511633
/// This struct is created by the [`iter`](HashMap::iter) method on [`HashMap`]. See its documentation for details.
16521634
pub struct Iter<'g, K, V, G> {
1653-
raw: raw::Iter<'g, K, V, MapGuard<G>>,
1635+
raw: map::Iter<'g, K, V, MapGuard<G>>,
16541636
}
16551637

16561638
impl<'g, K: 'g, V: 'g, G> Iterator for Iter<'g, K, V, G>
@@ -1688,7 +1670,7 @@ where
16881670
///
16891671
/// This struct is created by the [`iter_mut`](HashMap::iter_mut) method on [`HashMap`]. See its documentation for details.
16901672
pub struct IterMut<'map, K, V> {
1691-
raw: raw::IterMut<'map, K, V>,
1673+
raw: map::IterMut<'map, K, V>,
16921674
}
16931675

16941676
impl<'map, K, V> Iterator for IterMut<'map, K, V> {
@@ -1779,7 +1761,7 @@ where
17791761
///
17801762
/// [`into_iter`]: IntoIterator::into_iter
17811763
pub struct IntoIter<K, V> {
1782-
pub(crate) raw: raw::IntoIter<K, V>,
1764+
pub(crate) raw: map::IntoIter<K, V>,
17831765
}
17841766

17851767
impl<K, V> Iterator for IntoIter<K, V> {

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)