Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parametrize RawTable, HashSet and HashMap over an allocator #133

Merged
merged 16 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::raw::{Bucket, RawDrain, RawIntoIter, RawIter, RawTable};
use crate::raw::{Bucket, RawDrain, RawIntoIter, RawIter, RawTable, Global};
use crate::CollectionAllocErr;
use core::borrow::Borrow;
use core::fmt::{self, Debug};
Expand Down Expand Up @@ -193,7 +193,7 @@ pub enum DefaultHashBuilder {}
#[derive(Clone)]
pub struct HashMap<K, V, S = DefaultHashBuilder> {
pub(crate) hash_builder: S,
pub(crate) table: RawTable<(K, V)>,
pub(crate) table: RawTable<Global, (K, V)>,
}

#[cfg_attr(feature = "inline-more", inline)]
Expand Down Expand Up @@ -263,7 +263,7 @@ impl<K, V, S> HashMap<K, V, S> {
pub fn with_hasher(hash_builder: S) -> Self {
Self {
hash_builder,
table: RawTable::new(),
table: RawTable::new(Global),
}
}

Expand Down Expand Up @@ -292,7 +292,7 @@ impl<K, V, S> HashMap<K, V, S> {
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
Self {
hash_builder,
table: RawTable::with_capacity(capacity),
table: RawTable::with_capacity(Global, capacity),
}
}

Expand Down Expand Up @@ -1146,7 +1146,7 @@ impl<K, V> IterMut<'_, K, V> {
/// [`into_iter`]: struct.HashMap.html#method.into_iter
/// [`HashMap`]: struct.HashMap.html
pub struct IntoIter<K, V> {
inner: RawIntoIter<(K, V)>,
inner: RawIntoIter<Global, (K, V)>,
}

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

impl<K, V> Drain<'_, K, V> {
Expand Down Expand Up @@ -1280,7 +1280,7 @@ pub enum RawEntryMut<'a, K, V, S> {
/// [`RawEntryMut`]: enum.RawEntryMut.html
pub struct RawOccupiedEntryMut<'a, K, V> {
elem: Bucket<(K, V)>,
table: &'a mut RawTable<(K, V)>,
table: &'a mut RawTable<Global, (K, V)>,
}

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

Expand Down
29 changes: 29 additions & 0 deletions src/raw/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pub use self::inner::*;

#[cfg(feature = "nightly")]
mod inner {
pub use crate::alloc::alloc::{Alloc, Global};
}

#[cfg(not(feature = "nightly"))]
mod inner {
use core::ptr::NonNull;
use crate::alloc::alloc::{Layout, alloc, dealloc};

pub trait Alloc {
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, ()>;
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
}

#[derive(Copy, Clone)]
pub struct Global;
impl Alloc for Global {
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, ()> {
Ok(NonNull::new_unchecked(alloc(layout)))
hansihe marked this conversation as resolved.
Show resolved Hide resolved
}
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
dealloc(ptr.as_ptr(), layout)
}
}
}

Loading