Skip to content

Commit 805b5e2

Browse files
committed
Auto merge of #257 - optimalstrategy:add-allocator-getters, r=Amanieu
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.
2 parents ba4ff68 + a0069dd commit 805b5e2

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/map.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ impl<K, V, S> HashMap<K, V, S> {
395395
}
396396

397397
impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
398+
/// Returns a reference to the underlying allocator.
399+
#[inline]
400+
pub fn allocator(&self) -> &A {
401+
self.table.allocator()
402+
}
403+
398404
/// Creates an empty `HashMap` which will use the given hash builder to hash
399405
/// keys. It will be allocated with the given allocator.
400406
///

src/raw/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
492492
}
493493
}
494494

495+
/// Returns a reference to the underlying allocator.
496+
#[inline]
497+
pub fn allocator(&self) -> &A {
498+
&self.table.alloc
499+
}
500+
495501
/// Deallocates the table without dropping any entries.
496502
#[cfg_attr(feature = "inline-more", inline)]
497503
unsafe fn free_buckets(&mut self) {

src/set.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,12 @@ impl<T, S, A> HashSet<T, S, A>
454454
where
455455
A: Allocator + Clone,
456456
{
457+
/// Returns a reference to the underlying allocator.
458+
#[inline]
459+
pub fn allocator(&self) -> &A {
460+
self.map.allocator()
461+
}
462+
457463
/// Creates a new empty hash set which will use the given hasher to hash
458464
/// keys.
459465
///

0 commit comments

Comments
 (0)