Skip to content

Commit dac885a

Browse files
committed
use Global for with_capacity, and add with_capacity_in
1 parent ae86b4e commit dac885a

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ impl<K, V, S> HashMap<K, V, S> {
405405
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
406406
Self {
407407
hash_builder,
408-
table: RawTable::with_capacity(Global, capacity),
408+
table: RawTable::with_capacity(capacity),
409409
}
410410
}
411411
}
@@ -464,7 +464,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
464464
pub fn with_capacity_and_hasher_in(capacity: usize, hash_builder: S, alloc: A) -> Self {
465465
Self {
466466
hash_builder,
467-
table: RawTable::with_capacity(alloc, capacity),
467+
table: RawTable::with_capacity_in(capacity, alloc),
468468
}
469469
}
470470

src/raw/mod.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,19 @@ impl<T> RawTable<T, Global> {
406406
alloc: Global,
407407
}
408408
}
409+
410+
/// Attempts to allocate a new hash table with at least enough capacity
411+
/// for inserting the given number of elements without reallocating.
412+
#[cfg(feature = "raw")]
413+
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
414+
Self::try_with_capacity_in(capacity, Global)
415+
}
416+
417+
/// Allocates a new hash table with at least enough capacity for inserting
418+
/// the given number of elements without reallocating.
419+
pub fn with_capacity(capacity: usize) -> Self {
420+
Self::with_capacity_in(capacity, Global)
421+
}
409422
}
410423

411424
impl<T, A: Allocator + Clone> RawTable<T, A> {
@@ -483,16 +496,16 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
483496
}
484497
}
485498

486-
/// Attempts to allocate a new hash table with at least enough capacity
487-
/// for inserting the given number of elements without reallocating.
499+
/// Attempts to allocate a new hash table using the given allocator, with at least enough
500+
/// capacity for inserting the given number of elements without reallocating.
488501
#[cfg(feature = "raw")]
489-
pub fn try_with_capacity(alloc: A, capacity: usize) -> Result<Self, TryReserveError> {
502+
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
490503
Self::fallible_with_capacity(alloc, capacity, Fallibility::Fallible)
491504
}
492505

493-
/// Allocates a new hash table with at least enough capacity for inserting
494-
/// the given number of elements without reallocating.
495-
pub fn with_capacity(alloc: A, capacity: usize) -> Self {
506+
/// Allocates a new hash table using the given allocator, with at least enough capacity for
507+
/// inserting the given number of elements without reallocating.
508+
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
496509
// Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
497510
match Self::fallible_with_capacity(alloc, capacity, Fallibility::Infallible) {
498511
Ok(capacity) => capacity,
@@ -748,7 +761,7 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
748761
if min_buckets < self.buckets() {
749762
// Fast path if the table is empty
750763
if self.items == 0 {
751-
*self = Self::with_capacity(self.alloc.clone(), min_size)
764+
*self = Self::with_capacity_in(min_size, self.alloc.clone())
752765
} else {
753766
// Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
754767
if self

0 commit comments

Comments
 (0)