Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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: 15 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ rust-version = "nightly"
[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "nightly"]

[dependencies.hashbrown]
version = "0.15.1"
optional = true
default-features = false
features = [
"nightly",
"default-hasher",
"inline-more",
"allocator-api2",
"equivalent",
"raw-entry"
]

[features]
default = []
default = ["hash_collections"]
hash_collections = ["hashbrown"]
no_std = []
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The wrapper collection types provide two main benefits:
* They can't be used incorrectly by virtue of lacking APIs which panic.
* Provide additional convenience methods for working with the collection in a checked manner.

*N.B.:* You might find the [`heapless`](https://docs.rs/heapless/latest/heapless/) crate useful too!

## Restrictions

The crate requires a recent build of the Rust "nightly" compiler, as it uses the
Expand Down
3 changes: 2 additions & 1 deletion src/claim.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::alloc::Global;
use alloc::rc::Rc;
use alloc::sync::Arc;
use core::convert::Infallible;
Expand Down Expand Up @@ -26,7 +27,7 @@ macro_rules! impl_claim_for {
impl_claim_for! {
(), u8, u16, u32, u64, u128, usize,
i8, i16, i32, i64, i128, isize,
f32, f64, bool, char
f32, f64, bool, char, Global
}

impl<T: ?Sized> Claim for *const T {}
Expand Down
106 changes: 106 additions & 0 deletions src/hash/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::claim::Claim;
use core::alloc::Allocator;
use core::hash::{BuildHasher, Hash};
pub use hashbrown::hash_map::{Iter, IterMut, Keys, Values, ValuesMut};
pub use hashbrown::DefaultHashBuilder;
use hashbrown::{HashMap as InnerHashMap, TryReserveError};

pub struct HashMap<K, V, A: Allocator, S = DefaultHashBuilder> {
inner: InnerHashMap<K, V, S, A>,
}

impl<K, V, A: Allocator + Claim> HashMap<K, V, A, DefaultHashBuilder> {
#[inline]
pub fn new_in(alloc: A) -> Self {
Self::with_hasher_in(DefaultHashBuilder::default(), alloc)
}

#[inline]
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
Self::with_capacity_and_hasher_in(capacity, DefaultHashBuilder::default(), alloc)
}
}

impl<K, V, A: Allocator + Claim, S> HashMap<K, V, A, S> {
#[inline]
pub fn allocator(&self) -> &A {
self.inner.allocator()
}

#[inline]
pub fn with_hasher_in(hash_builder: S, alloc: A) -> Self {
Self {
inner: InnerHashMap::with_hasher_in(hash_builder, alloc),
}
}

#[inline]
pub fn with_capacity_and_hasher_in(capacity: usize, hash_builder: S, alloc: A) -> Self {
Self {
inner: InnerHashMap::with_capacity_and_hasher_in(capacity, hash_builder, alloc),
}
}

#[inline]
pub fn hasher(&self) -> &S {
self.inner.hasher()
}

#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}

#[inline]
pub fn clear(&mut self) {
// TODO(amunra): May this reallocate memory?
}

#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}

#[inline]
pub fn keys(&self) -> Keys<'_, K, V> {
self.inner.keys()
}

#[inline]
pub fn values(&self) -> Values<'_, K, V> {
self.inner.values()
}

#[inline]
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
self.inner.values_mut()
}

#[inline]
pub fn iter(&self) -> Iter<'_, K, V> {
self.inner.iter()
}

#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
self.inner.iter_mut()
}
}

impl<K, V, A, S> HashMap<K, V, A, S>
where
K: Eq + Hash,
A: Allocator + Claim,
S: BuildHasher,
{
#[inline]
pub fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.inner.try_reserve(additional)
}

// #[inline]
// pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> {
// // TODO(amunra): May this reallocate memory?
// self.inner.remove(k)
// }
}
1 change: 1 addition & 0 deletions src/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod map;
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub mod try_clone;
pub mod vec;
pub mod vec_deque;

#[cfg(feature = "hash_collections")]
pub mod hash;

#[cfg(test)]
pub(crate) mod testing;

Expand Down