Skip to content

Commit d8463d0

Browse files
author
Markus Westerlind
committed
Address review comments
1 parent 63832ed commit d8463d0

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/map.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,18 @@ impl<K: Clone, V: Clone, S: Clone> Clone for HashMap<K, V, S> {
206206
}
207207
}
208208

209+
/// Ensures that a single closure type across uses of this which, in turn prevents multiple
210+
/// instances of any functions like RawTable::reserve from being generated
209211
#[cfg_attr(feature = "inline-more", inline)]
210212
pub(crate) fn make_hasher<K: Hash, V>(
211213
hash_builder: &impl BuildHasher,
212214
) -> impl Fn(&(K, V)) -> u64 + '_ {
213215
move |val| make_hash(hash_builder, &val.0)
214216
}
215217

218+
/// Ensures that a single closure type across uses of this which, in turn prevents multiple
219+
/// instances of any functions like RawTable::reserve from being generated
220+
#[cfg_attr(feature = "inline-more", inline)]
216221
fn equivalent<Q, K, V>(k: &Q) -> impl Fn(&(K, V)) -> bool + '_
217222
where
218223
K: Borrow<Q>,
@@ -221,6 +226,9 @@ where
221226
move |x| k.eq(x.0.borrow())
222227
}
223228

229+
/// Ensures that a single closure type across uses of this which, in turn prevents multiple
230+
/// instances of any functions like RawTable::reserve from being generated
231+
#[cfg_attr(feature = "inline-more", inline)]
224232
fn equivalent_single<Q, K>(k: &Q) -> impl Fn(&K) -> bool + '_
225233
where
226234
K: Borrow<Q>,
@@ -686,8 +694,8 @@ where
686694
/// ```
687695
#[cfg_attr(feature = "inline-more", inline)]
688696
pub fn reserve(&mut self, additional: usize) {
689-
let hash_builder = &self.hash_builder;
690-
self.table.reserve(additional, make_hasher(hash_builder));
697+
self.table
698+
.reserve(additional, make_hasher(&self.hash_builder));
691699
}
692700

693701
/// Tries to reserve capacity for at least `additional` more elements to be inserted
@@ -708,9 +716,8 @@ where
708716
/// ```
709717
#[cfg_attr(feature = "inline-more", inline)]
710718
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
711-
let hash_builder = &self.hash_builder;
712719
self.table
713-
.try_reserve(additional, make_hasher(hash_builder))
720+
.try_reserve(additional, make_hasher(&self.hash_builder))
714721
}
715722

716723
/// Shrinks the capacity of the map as much as possible. It will drop
@@ -731,8 +738,7 @@ where
731738
/// ```
732739
#[cfg_attr(feature = "inline-more", inline)]
733740
pub fn shrink_to_fit(&mut self) {
734-
let hash_builder = &self.hash_builder;
735-
self.table.shrink_to(0, make_hasher(hash_builder));
741+
self.table.shrink_to(0, make_hasher(&self.hash_builder));
736742
}
737743

738744
/// Shrinks the capacity of the map with a lower limit. It will drop
@@ -760,9 +766,8 @@ where
760766
/// ```
761767
#[cfg_attr(feature = "inline-more", inline)]
762768
pub fn shrink_to(&mut self, min_capacity: usize) {
763-
let hash_builder = &self.hash_builder;
764769
self.table
765-
.shrink_to(min_capacity, make_hasher(hash_builder));
770+
.shrink_to(min_capacity, make_hasher(&self.hash_builder));
766771
}
767772

768773
/// Gets the given key's corresponding entry in the map for in-place manipulation.
@@ -1016,8 +1021,8 @@ where
10161021
if let Some((_, item)) = self.table.get_mut(hash, equivalent(&k)) {
10171022
Some(mem::replace(item, v))
10181023
} else {
1019-
let hash_builder = &self.hash_builder;
1020-
self.table.insert(hash, (k, v), make_hasher(hash_builder));
1024+
self.table
1025+
.insert(hash, (k, v), make_hasher(&self.hash_builder));
10211026
None
10221027
}
10231028
}
@@ -1966,10 +1971,9 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
19661971
K: Hash,
19671972
S: BuildHasher,
19681973
{
1969-
let hash_builder = self.hash_builder;
19701974
let &mut (ref mut k, ref mut v) =
19711975
self.table
1972-
.insert_entry(hash, (key, value), make_hasher(hash_builder));
1976+
.insert_entry(hash, (key, value), make_hasher(self.hash_builder));
19731977
(k, v)
19741978
}
19751979

@@ -1998,7 +2002,7 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
19982002
S: BuildHasher,
19992003
{
20002004
let hash_builder = self.hash_builder;
2001-
let mut hasher = self.hash_builder.build_hasher();
2005+
let mut hasher = hash_builder.build_hasher();
20022006
key.hash(&mut hasher);
20032007

20042008
let elem = self
@@ -2007,7 +2011,7 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
20072011
RawOccupiedEntryMut {
20082012
elem,
20092013
table: self.table,
2010-
hash_builder: self.hash_builder,
2014+
hash_builder,
20112015
}
20122016
}
20132017
}
@@ -2996,9 +3000,12 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
29963000
K: Hash,
29973001
S: BuildHasher,
29983002
{
2999-
let hash_builder = &self.table.hash_builder;
30003003
let table = &mut self.table.table;
3001-
let entry = table.insert_entry(self.hash, (self.key, value), make_hasher(hash_builder));
3004+
let entry = table.insert_entry(
3005+
self.hash,
3006+
(self.key, value),
3007+
make_hasher(&self.table.hash_builder),
3008+
);
30023009
&mut entry.1
30033010
}
30043011

@@ -3008,11 +3015,11 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
30083015
K: Hash,
30093016
S: BuildHasher,
30103017
{
3011-
let hash_builder = &self.table.hash_builder;
3012-
let elem = self
3013-
.table
3014-
.table
3015-
.insert(self.hash, (self.key, value), make_hasher(hash_builder));
3018+
let elem = self.table.table.insert(
3019+
self.hash,
3020+
(self.key, value),
3021+
make_hasher(&self.table.hash_builder),
3022+
);
30163023
OccupiedEntry {
30173024
hash: self.hash,
30183025
key: None,

0 commit comments

Comments
 (0)