Skip to content

Commit

Permalink
Auto merge of #257 - optimalstrategy:add-allocator-getters, r=Amanieu
Browse files Browse the repository at this point in the history
Add an allocator() getter to HashMap and HashSet

As far as I can tell, both HashMap and HashSet are currently missing an API to retrieve the underlying allocator, which makes using them less ergonomic due to the need to plumb it everywhere. [Vec](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.allocator) and [Box](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.allocator) have this method, and I couldn't think of anything that would make its presence unsuitable, so this PR adds the getter to the  collections.
  • Loading branch information
bors committed Apr 20, 2021
2 parents ba4ff68 + a0069dd commit 805b5e2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ impl<K, V, S> HashMap<K, V, S> {
}

impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
/// Returns a reference to the underlying allocator.
#[inline]
pub fn allocator(&self) -> &A {
self.table.allocator()
}

/// Creates an empty `HashMap` which will use the given hash builder to hash
/// keys. It will be allocated with the given allocator.
///
Expand Down
6 changes: 6 additions & 0 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,12 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
}
}

/// Returns a reference to the underlying allocator.
#[inline]
pub fn allocator(&self) -> &A {
&self.table.alloc
}

/// Deallocates the table without dropping any entries.
#[cfg_attr(feature = "inline-more", inline)]
unsafe fn free_buckets(&mut self) {
Expand Down
6 changes: 6 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ impl<T, S, A> HashSet<T, S, A>
where
A: Allocator + Clone,
{
/// Returns a reference to the underlying allocator.
#[inline]
pub fn allocator(&self) -> &A {
self.map.allocator()
}

/// Creates a new empty hash set which will use the given hasher to hash
/// keys.
///
Expand Down

0 comments on commit 805b5e2

Please sign in to comment.