Skip to content

Commit 78945d0

Browse files
committed
Parametrize RawTable over an allocator
* `RawTable` has a new type parameter, `A: Alloc + Clone` * When the `nightly` flag is passed, `Alloc` will be the trait in `alloc::Alloc`. * On stable, a minimal shim implementation is provided, along with an implementation for the global allocator. * No public APIs changed. * For `HashMap`, everything is monomorphized to the global allocator, and there should be no performance or size overhead.
1 parent 2c60ecb commit 78945d0

File tree

3 files changed

+101
-57
lines changed

3 files changed

+101
-57
lines changed

src/map.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::raw::{Bucket, RawDrain, RawIntoIter, RawIter, RawTable};
1+
use crate::raw::{Bucket, RawDrain, RawIntoIter, RawIter, RawTable, Global};
22
use crate::CollectionAllocErr;
33
use core::borrow::Borrow;
44
use core::fmt::{self, Debug};
@@ -193,7 +193,7 @@ pub enum DefaultHashBuilder {}
193193
#[derive(Clone)]
194194
pub struct HashMap<K, V, S = DefaultHashBuilder> {
195195
pub(crate) hash_builder: S,
196-
pub(crate) table: RawTable<(K, V)>,
196+
pub(crate) table: RawTable<Global, (K, V)>,
197197
}
198198

199199
#[cfg_attr(feature = "inline-more", inline)]
@@ -263,7 +263,7 @@ impl<K, V, S> HashMap<K, V, S> {
263263
pub fn with_hasher(hash_builder: S) -> Self {
264264
Self {
265265
hash_builder,
266-
table: RawTable::new(),
266+
table: RawTable::new(Global),
267267
}
268268
}
269269

@@ -292,7 +292,7 @@ impl<K, V, S> HashMap<K, V, S> {
292292
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
293293
Self {
294294
hash_builder,
295-
table: RawTable::with_capacity(capacity),
295+
table: RawTable::with_capacity(Global, capacity),
296296
}
297297
}
298298

@@ -1146,7 +1146,7 @@ impl<K, V> IterMut<'_, K, V> {
11461146
/// [`into_iter`]: struct.HashMap.html#method.into_iter
11471147
/// [`HashMap`]: struct.HashMap.html
11481148
pub struct IntoIter<K, V> {
1149-
inner: RawIntoIter<(K, V)>,
1149+
inner: RawIntoIter<Global, (K, V)>,
11501150
}
11511151

11521152
impl<K, V> IntoIter<K, V> {
@@ -1222,7 +1222,7 @@ impl<K, V: Debug> fmt::Debug for Values<'_, K, V> {
12221222
/// [`drain`]: struct.HashMap.html#method.drain
12231223
/// [`HashMap`]: struct.HashMap.html
12241224
pub struct Drain<'a, K, V> {
1225-
inner: RawDrain<'a, (K, V)>,
1225+
inner: RawDrain<'a, Global, (K, V)>,
12261226
}
12271227

12281228
impl<K, V> Drain<'_, K, V> {
@@ -1280,7 +1280,7 @@ pub enum RawEntryMut<'a, K, V, S> {
12801280
/// [`RawEntryMut`]: enum.RawEntryMut.html
12811281
pub struct RawOccupiedEntryMut<'a, K, V> {
12821282
elem: Bucket<(K, V)>,
1283-
table: &'a mut RawTable<(K, V)>,
1283+
table: &'a mut RawTable<Global, (K, V)>,
12841284
}
12851285

12861286
unsafe impl<K, V> Send for RawOccupiedEntryMut<'_, K, V>
@@ -1301,7 +1301,7 @@ where
13011301
///
13021302
/// [`RawEntryMut`]: enum.RawEntryMut.html
13031303
pub struct RawVacantEntryMut<'a, K, V, S> {
1304-
table: &'a mut RawTable<(K, V)>,
1304+
table: &'a mut RawTable<Global, (K, V)>,
13051305
hash_builder: &'a S,
13061306
}
13071307

src/raw/alloc.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub use self::inner::*;
2+
3+
#[cfg(feature = "nightly")]
4+
mod inner {
5+
pub use crate::alloc::alloc::{Alloc, Global};
6+
}
7+
8+
#[cfg(not(feature = "nightly"))]
9+
mod inner {
10+
use core::ptr::NonNull;
11+
use crate::alloc::alloc::{Layout, alloc, dealloc};
12+
13+
pub trait Alloc {
14+
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, ()>;
15+
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
16+
}
17+
18+
#[derive(Copy, Clone)]
19+
pub struct Global;
20+
impl Alloc for Global {
21+
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, ()> {
22+
Ok(NonNull::new_unchecked(alloc(layout)))
23+
}
24+
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
25+
dealloc(ptr.as_ptr(), layout)
26+
}
27+
}
28+
}
29+

0 commit comments

Comments
 (0)