@@ -206,6 +206,37 @@ impl<K: Clone, V: Clone, S: Clone> Clone for HashMap<K, V, S> {
206
206
}
207
207
}
208
208
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
211
+ #[ cfg_attr( feature = "inline-more" , inline) ]
212
+ pub ( crate ) fn make_hasher < K : Hash , V > (
213
+ hash_builder : & impl BuildHasher ,
214
+ ) -> impl Fn ( & ( K , V ) ) -> u64 + ' _ {
215
+ move |val| make_hash ( hash_builder, & val. 0 )
216
+ }
217
+
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) ]
221
+ fn equivalent_key < Q , K , V > ( k : & Q ) -> impl Fn ( & ( K , V ) ) -> bool + ' _
222
+ where
223
+ K : Borrow < Q > ,
224
+ Q : ?Sized + Eq ,
225
+ {
226
+ move |x| k. eq ( x. 0 . borrow ( ) )
227
+ }
228
+
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) ]
232
+ fn equivalent < Q , K > ( k : & Q ) -> impl Fn ( & K ) -> bool + ' _
233
+ where
234
+ K : Borrow < Q > ,
235
+ Q : ?Sized + Eq ,
236
+ {
237
+ move |x| k. eq ( x. borrow ( ) )
238
+ }
239
+
209
240
#[ cfg_attr( feature = "inline-more" , inline) ]
210
241
pub ( crate ) fn make_hash < K : Hash + ?Sized > ( hash_builder : & impl BuildHasher , val : & K ) -> u64 {
211
242
let mut state = hash_builder. build_hasher ( ) ;
@@ -663,9 +694,8 @@ where
663
694
/// ```
664
695
#[ cfg_attr( feature = "inline-more" , inline) ]
665
696
pub fn reserve ( & mut self , additional : usize ) {
666
- let hash_builder = & self . hash_builder ;
667
697
self . table
668
- . reserve ( additional, |x| make_hash ( hash_builder , & x . 0 ) ) ;
698
+ . reserve ( additional, make_hasher ( & self . hash_builder ) ) ;
669
699
}
670
700
671
701
/// Tries to reserve capacity for at least `additional` more elements to be inserted
@@ -686,9 +716,8 @@ where
686
716
/// ```
687
717
#[ cfg_attr( feature = "inline-more" , inline) ]
688
718
pub fn try_reserve ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
689
- let hash_builder = & self . hash_builder ;
690
719
self . table
691
- . try_reserve ( additional, |x| make_hash ( hash_builder , & x . 0 ) )
720
+ . try_reserve ( additional, make_hasher ( & self . hash_builder ) )
692
721
}
693
722
694
723
/// Shrinks the capacity of the map as much as possible. It will drop
@@ -709,8 +738,7 @@ where
709
738
/// ```
710
739
#[ cfg_attr( feature = "inline-more" , inline) ]
711
740
pub fn shrink_to_fit ( & mut self ) {
712
- let hash_builder = & self . hash_builder ;
713
- self . table . shrink_to ( 0 , |x| make_hash ( hash_builder, & x. 0 ) ) ;
741
+ self . table . shrink_to ( 0 , make_hasher ( & self . hash_builder ) ) ;
714
742
}
715
743
716
744
/// Shrinks the capacity of the map with a lower limit. It will drop
@@ -738,9 +766,8 @@ where
738
766
/// ```
739
767
#[ cfg_attr( feature = "inline-more" , inline) ]
740
768
pub fn shrink_to ( & mut self , min_capacity : usize ) {
741
- let hash_builder = & self . hash_builder ;
742
769
self . table
743
- . shrink_to ( min_capacity, |x| make_hash ( hash_builder , & x . 0 ) ) ;
770
+ . shrink_to ( min_capacity, make_hasher ( & self . hash_builder ) ) ;
744
771
}
745
772
746
773
/// Gets the given key's corresponding entry in the map for in-place manipulation.
@@ -765,7 +792,7 @@ where
765
792
#[ cfg_attr( feature = "inline-more" , inline) ]
766
793
pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V , S > {
767
794
let hash = make_hash ( & self . hash_builder , & key) ;
768
- if let Some ( elem) = self . table . find ( hash, |q| q . 0 . eq ( & key) ) {
795
+ if let Some ( elem) = self . table . find ( hash, equivalent_key ( & key) ) {
769
796
Entry :: Occupied ( OccupiedEntry {
770
797
hash,
771
798
key : Some ( key) ,
@@ -852,7 +879,7 @@ where
852
879
Q : Hash + Eq ,
853
880
{
854
881
let hash = make_hash ( & self . hash_builder , k) ;
855
- self . table . get ( hash, |x| k . eq ( x . 0 . borrow ( ) ) )
882
+ self . table . get ( hash, equivalent_key ( k ) )
856
883
}
857
884
858
885
/// Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.
@@ -960,7 +987,7 @@ where
960
987
Q : Hash + Eq ,
961
988
{
962
989
let hash = make_hash ( & self . hash_builder , k) ;
963
- self . table . get_mut ( hash, |x| k . eq ( x . 0 . borrow ( ) ) )
990
+ self . table . get_mut ( hash, equivalent_key ( k ) )
964
991
}
965
992
966
993
/// Inserts a key-value pair into the map.
@@ -991,12 +1018,11 @@ where
991
1018
#[ cfg_attr( feature = "inline-more" , inline) ]
992
1019
pub fn insert ( & mut self , k : K , v : V ) -> Option < V > {
993
1020
let hash = make_hash ( & self . hash_builder , & k) ;
994
- if let Some ( ( _, item) ) = self . table . get_mut ( hash, |x| k . eq ( & x . 0 ) ) {
1021
+ if let Some ( ( _, item) ) = self . table . get_mut ( hash, equivalent_key ( & k ) ) {
995
1022
Some ( mem:: replace ( item, v) )
996
1023
} else {
997
- let hash_builder = & self . hash_builder ;
998
1024
self . table
999
- . insert ( hash, ( k, v) , |x| make_hash ( hash_builder , & x . 0 ) ) ;
1025
+ . insert ( hash, ( k, v) , make_hasher ( & self . hash_builder ) ) ;
1000
1026
None
1001
1027
}
1002
1028
}
@@ -1061,7 +1087,7 @@ where
1061
1087
Q : Hash + Eq ,
1062
1088
{
1063
1089
let hash = make_hash ( & self . hash_builder , & k) ;
1064
- self . table . remove_entry ( hash, |x| k . eq ( x . 0 . borrow ( ) ) )
1090
+ self . table . remove_entry ( hash, equivalent_key ( k ) )
1065
1091
}
1066
1092
}
1067
1093
@@ -1528,7 +1554,7 @@ impl<'a, K, V, S> RawEntryBuilderMut<'a, K, V, S> {
1528
1554
K : Borrow < Q > ,
1529
1555
Q : Eq ,
1530
1556
{
1531
- self . from_hash ( hash, |q| q . borrow ( ) . eq ( k) )
1557
+ self . from_hash ( hash, equivalent ( k) )
1532
1558
}
1533
1559
}
1534
1560
@@ -1585,7 +1611,7 @@ impl<'a, K, V, S> RawEntryBuilder<'a, K, V, S> {
1585
1611
K : Borrow < Q > ,
1586
1612
Q : Eq ,
1587
1613
{
1588
- self . from_hash ( hash, |q| q . borrow ( ) . eq ( k) )
1614
+ self . from_hash ( hash, equivalent ( k) )
1589
1615
}
1590
1616
1591
1617
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -1945,8 +1971,10 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
1945
1971
K : Hash ,
1946
1972
S : BuildHasher ,
1947
1973
{
1948
- let hash_builder = self . hash_builder ;
1949
- self . insert_with_hasher ( hash, key, value, |k| make_hash ( hash_builder, k) )
1974
+ let & mut ( ref mut k, ref mut v) =
1975
+ self . table
1976
+ . insert_entry ( hash, ( key, value) , make_hasher ( self . hash_builder ) ) ;
1977
+ ( k, v)
1950
1978
}
1951
1979
1952
1980
/// Set the value of an entry with a custom hasher function.
@@ -1973,13 +2001,14 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
1973
2001
K : Hash ,
1974
2002
S : BuildHasher ,
1975
2003
{
1976
- let hash_builder = self . hash_builder ;
1977
2004
let mut hasher = self . hash_builder . build_hasher ( ) ;
1978
2005
key. hash ( & mut hasher) ;
1979
2006
1980
- let elem = self . table . insert ( hasher. finish ( ) , ( key, value) , |k| {
1981
- make_hash ( hash_builder, & k. 0 )
1982
- } ) ;
2007
+ let elem = self . table . insert (
2008
+ hasher. finish ( ) ,
2009
+ ( key, value) ,
2010
+ make_hasher ( self . hash_builder ) ,
2011
+ ) ;
1983
2012
RawOccupiedEntryMut {
1984
2013
elem,
1985
2014
table : self . table ,
@@ -2972,11 +3001,12 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
2972
3001
K : Hash ,
2973
3002
S : BuildHasher ,
2974
3003
{
2975
- let hash_builder = & self . table . hash_builder ;
2976
3004
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
- } ) ;
3005
+ let entry = table. insert_entry (
3006
+ self . hash ,
3007
+ ( self . key , value) ,
3008
+ make_hasher ( & self . table . hash_builder ) ,
3009
+ ) ;
2980
3010
& mut entry. 1
2981
3011
}
2982
3012
@@ -2986,10 +3016,11 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
2986
3016
K : Hash ,
2987
3017
S : BuildHasher ,
2988
3018
{
2989
- 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
- } ) ;
3019
+ let elem = self . table . table . insert (
3020
+ self . hash ,
3021
+ ( self . key , value) ,
3022
+ make_hasher ( & self . table . hash_builder ) ,
3023
+ ) ;
2993
3024
OccupiedEntry {
2994
3025
hash : self . hash ,
2995
3026
key : None ,
@@ -4470,7 +4501,7 @@ mod test_map {
4470
4501
assert ! ( removed. contains( & ( i, 2 * i) ) , "{} not in {:?}" , i, removed) ;
4471
4502
let e = m
4472
4503
. table
4473
- . insert ( hash, ( i, 2 * i) , |x| super :: make_hash ( & hasher, & x . 0 ) ) ;
4504
+ . insert ( hash, ( i, 2 * i) , super :: make_hasher ( & hasher) ) ;
4474
4505
it. reflect_insert ( & e) ;
4475
4506
if let Some ( p) = removed. iter ( ) . position ( |e| e == & ( i, 2 * i) ) {
4476
4507
removed. swap_remove ( p) ;
0 commit comments