@@ -206,6 +206,13 @@ impl<K: Clone, V: Clone, S: Clone> Clone for HashMap<K, V, S> {
206
206
}
207
207
}
208
208
209
+ #[ cfg_attr( feature = "inline-more" , inline) ]
210
+ pub ( crate ) fn make_hasher < K : Hash , V > (
211
+ hash_builder : & impl BuildHasher ,
212
+ ) -> impl Fn ( & ( K , V ) ) -> u64 + ' _ {
213
+ move |val| make_hash ( hash_builder, & val. 0 )
214
+ }
215
+
209
216
#[ cfg_attr( feature = "inline-more" , inline) ]
210
217
pub ( crate ) fn make_hash < K : Hash + ?Sized > ( hash_builder : & impl BuildHasher , val : & K ) -> u64 {
211
218
let mut state = hash_builder. build_hasher ( ) ;
@@ -664,8 +671,7 @@ where
664
671
#[ cfg_attr( feature = "inline-more" , inline) ]
665
672
pub fn reserve ( & mut self , additional : usize ) {
666
673
let hash_builder = & self . hash_builder ;
667
- self . table
668
- . reserve ( additional, |x| make_hash ( hash_builder, & x. 0 ) ) ;
674
+ self . table . reserve ( additional, make_hasher ( hash_builder) ) ;
669
675
}
670
676
671
677
/// Tries to reserve capacity for at least `additional` more elements to be inserted
@@ -688,7 +694,7 @@ where
688
694
pub fn try_reserve ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
689
695
let hash_builder = & self . hash_builder ;
690
696
self . table
691
- . try_reserve ( additional, |x| make_hash ( hash_builder, & x . 0 ) )
697
+ . try_reserve ( additional, make_hasher ( hash_builder) )
692
698
}
693
699
694
700
/// Shrinks the capacity of the map as much as possible. It will drop
@@ -710,7 +716,7 @@ where
710
716
#[ cfg_attr( feature = "inline-more" , inline) ]
711
717
pub fn shrink_to_fit ( & mut self ) {
712
718
let hash_builder = & self . hash_builder ;
713
- self . table . shrink_to ( 0 , |x| make_hash ( hash_builder, & x . 0 ) ) ;
719
+ self . table . shrink_to ( 0 , make_hasher ( hash_builder) ) ;
714
720
}
715
721
716
722
/// Shrinks the capacity of the map with a lower limit. It will drop
@@ -740,7 +746,7 @@ where
740
746
pub fn shrink_to ( & mut self , min_capacity : usize ) {
741
747
let hash_builder = & self . hash_builder ;
742
748
self . table
743
- . shrink_to ( min_capacity, |x| make_hash ( hash_builder, & x . 0 ) ) ;
749
+ . shrink_to ( min_capacity, make_hasher ( hash_builder) ) ;
744
750
}
745
751
746
752
/// Gets the given key's corresponding entry in the map for in-place manipulation.
@@ -995,8 +1001,7 @@ where
995
1001
Some ( mem:: replace ( item, v) )
996
1002
} else {
997
1003
let hash_builder = & self . hash_builder ;
998
- self . table
999
- . insert ( hash, ( k, v) , |x| make_hash ( hash_builder, & x. 0 ) ) ;
1004
+ self . table . insert ( hash, ( k, v) , make_hasher ( hash_builder) ) ;
1000
1005
None
1001
1006
}
1002
1007
}
@@ -1946,7 +1951,10 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
1946
1951
S : BuildHasher ,
1947
1952
{
1948
1953
let hash_builder = self . hash_builder ;
1949
- self . insert_with_hasher ( hash, key, value, |k| make_hash ( hash_builder, k) )
1954
+ let & mut ( ref mut k, ref mut v) =
1955
+ self . table
1956
+ . insert_entry ( hash, ( key, value) , make_hasher ( hash_builder) ) ;
1957
+ ( k, v)
1950
1958
}
1951
1959
1952
1960
/// Set the value of an entry with a custom hasher function.
@@ -1977,9 +1985,9 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
1977
1985
let mut hasher = self . hash_builder . build_hasher ( ) ;
1978
1986
key. hash ( & mut hasher) ;
1979
1987
1980
- let elem = self . table . insert ( hasher . finish ( ) , ( key , value ) , |k| {
1981
- make_hash ( hash_builder , & k . 0 )
1982
- } ) ;
1988
+ let elem = self
1989
+ . table
1990
+ . insert ( hasher . finish ( ) , ( key , value ) , make_hasher ( hash_builder ) ) ;
1983
1991
RawOccupiedEntryMut {
1984
1992
elem,
1985
1993
table : self . table ,
@@ -2974,9 +2982,7 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
2974
2982
{
2975
2983
let hash_builder = & self . table . hash_builder ;
2976
2984
let table = & mut self . table . table ;
2977
- let entry = table. insert_entry ( self . hash , ( self . key , value) , |x| {
2978
- make_hash ( hash_builder, & x. 0 )
2979
- } ) ;
2985
+ let entry = table. insert_entry ( self . hash , ( self . key , value) , make_hasher ( hash_builder) ) ;
2980
2986
& mut entry. 1
2981
2987
}
2982
2988
@@ -2987,9 +2993,10 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
2987
2993
S : BuildHasher ,
2988
2994
{
2989
2995
let hash_builder = & self . table . hash_builder ;
2990
- let elem = self . table . table . insert ( self . hash , ( self . key , value) , |x| {
2991
- make_hash ( hash_builder, & x. 0 )
2992
- } ) ;
2996
+ let elem = self
2997
+ . table
2998
+ . table
2999
+ . insert ( self . hash , ( self . key , value) , make_hasher ( hash_builder) ) ;
2993
3000
OccupiedEntry {
2994
3001
hash : self . hash ,
2995
3002
key : None ,
@@ -4470,7 +4477,7 @@ mod test_map {
4470
4477
assert ! ( removed. contains( & ( i, 2 * i) ) , "{} not in {:?}" , i, removed) ;
4471
4478
let e = m
4472
4479
. table
4473
- . insert ( hash, ( i, 2 * i) , |x| super :: make_hash ( & hasher, & x . 0 ) ) ;
4480
+ . insert ( hash, ( i, 2 * i) , super :: make_hasher ( & hasher) ) ;
4474
4481
it. reflect_insert ( & e) ;
4475
4482
if let Some ( p) = removed. iter ( ) . position ( |e| e == & ( i, 2 * i) ) {
4476
4483
removed. swap_remove ( p) ;
0 commit comments