@@ -807,8 +807,8 @@ where
807
807
Q : Hash + Eq ,
808
808
{
809
809
// Avoid `Option::map` because it bloats LLVM IR.
810
- match self . get_key_value ( k) {
811
- Some ( ( _, v) ) => Some ( v) ,
810
+ match self . get_inner ( k) {
811
+ Some ( & ( _, ref v) ) => Some ( v) ,
812
812
None => None ,
813
813
}
814
814
}
@@ -838,17 +838,23 @@ where
838
838
K : Borrow < Q > ,
839
839
Q : Hash + Eq ,
840
840
{
841
- let hash = make_hash ( & self . hash_builder , k) ;
842
841
// Avoid `Option::map` because it bloats LLVM IR.
843
- match self . table . find ( hash, |x| k. eq ( x. 0 . borrow ( ) ) ) {
844
- Some ( item) => unsafe {
845
- let & ( ref key, ref value) = item. as_ref ( ) ;
846
- Some ( ( key, value) )
847
- } ,
842
+ match self . get_inner ( k) {
843
+ Some ( & ( ref key, ref value) ) => Some ( ( key, value) ) ,
848
844
None => None ,
849
845
}
850
846
}
851
847
848
+ #[ inline]
849
+ fn get_inner < Q : ?Sized > ( & self , k : & Q ) -> Option < & ( K , V ) >
850
+ where
851
+ K : Borrow < Q > ,
852
+ Q : Hash + Eq ,
853
+ {
854
+ let hash = make_hash ( & self . hash_builder , k) ;
855
+ self . table . get ( hash, |x| k. eq ( x. 0 . borrow ( ) ) )
856
+ }
857
+
852
858
/// Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.
853
859
///
854
860
/// The supplied key may be any borrowed form of the map's key type, but
@@ -878,13 +884,9 @@ where
878
884
K : Borrow < Q > ,
879
885
Q : Hash + Eq ,
880
886
{
881
- let hash = make_hash ( & self . hash_builder , k) ;
882
887
// Avoid `Option::map` because it bloats LLVM IR.
883
- match self . table . find ( hash, |x| k. eq ( x. 0 . borrow ( ) ) ) {
884
- Some ( item) => unsafe {
885
- let & mut ( ref key, ref mut value) = item. as_mut ( ) ;
886
- Some ( ( key, value) )
887
- } ,
888
+ match self . get_inner_mut ( k) {
889
+ Some ( & mut ( ref key, ref mut value) ) => Some ( ( key, value) ) ,
888
890
None => None ,
889
891
}
890
892
}
@@ -914,7 +916,7 @@ where
914
916
K : Borrow < Q > ,
915
917
Q : Hash + Eq ,
916
918
{
917
- self . get ( k) . is_some ( )
919
+ self . get_inner ( k) . is_some ( )
918
920
}
919
921
920
922
/// Returns a mutable reference to the value corresponding to the key.
@@ -944,14 +946,23 @@ where
944
946
K : Borrow < Q > ,
945
947
Q : Hash + Eq ,
946
948
{
947
- let hash = make_hash ( & self . hash_builder , k) ;
948
949
// Avoid `Option::map` because it bloats LLVM IR.
949
- match self . table . find ( hash , |x| k . eq ( x . 0 . borrow ( ) ) ) {
950
- Some ( item ) => Some ( unsafe { & mut item . as_mut ( ) . 1 } ) ,
950
+ match self . get_inner_mut ( k ) {
951
+ Some ( & mut ( _ , ref mut v ) ) => Some ( v ) ,
951
952
None => None ,
952
953
}
953
954
}
954
955
956
+ #[ inline]
957
+ fn get_inner_mut < Q : ?Sized > ( & mut self , k : & Q ) -> Option < & mut ( K , V ) >
958
+ where
959
+ K : Borrow < Q > ,
960
+ Q : Hash + Eq ,
961
+ {
962
+ let hash = make_hash ( & self . hash_builder , k) ;
963
+ self . table . get_mut ( hash, |x| k. eq ( x. 0 . borrow ( ) ) )
964
+ }
965
+
955
966
/// Inserts a key-value pair into the map.
956
967
///
957
968
/// If the map did not have this key present, [`None`] is returned.
@@ -979,16 +990,14 @@ where
979
990
/// ```
980
991
#[ cfg_attr( feature = "inline-more" , inline) ]
981
992
pub fn insert ( & mut self , k : K , v : V ) -> Option < V > {
982
- unsafe {
983
- let hash = make_hash ( & self . hash_builder , & k) ;
984
- if let Some ( item) = self . table . find ( hash, |x| k. eq ( & x. 0 ) ) {
985
- Some ( mem:: replace ( & mut item. as_mut ( ) . 1 , v) )
986
- } else {
987
- let hash_builder = & self . hash_builder ;
988
- self . table
989
- . insert ( hash, ( k, v) , |x| make_hash ( hash_builder, & x. 0 ) ) ;
990
- None
991
- }
993
+ let hash = make_hash ( & self . hash_builder , & k) ;
994
+ if let Some ( ( _, item) ) = self . table . get_mut ( hash, |x| k. eq ( & x. 0 ) ) {
995
+ Some ( mem:: replace ( item, v) )
996
+ } else {
997
+ let hash_builder = & self . hash_builder ;
998
+ self . table
999
+ . insert ( hash, ( k, v) , |x| make_hash ( hash_builder, & x. 0 ) ) ;
1000
+ None
992
1001
}
993
1002
}
994
1003
@@ -1051,14 +1060,8 @@ where
1051
1060
K : Borrow < Q > ,
1052
1061
Q : Hash + Eq ,
1053
1062
{
1054
- unsafe {
1055
- let hash = make_hash ( & self . hash_builder , & k) ;
1056
- if let Some ( item) = self . table . find ( hash, |x| k. eq ( x. 0 . borrow ( ) ) ) {
1057
- Some ( self . table . remove ( item) )
1058
- } else {
1059
- None
1060
- }
1061
- }
1063
+ let hash = make_hash ( & self . hash_builder , & k) ;
1064
+ self . table . remove_entry ( hash, |x| k. eq ( x. 0 . borrow ( ) ) )
1062
1065
}
1063
1066
}
1064
1067
@@ -1590,11 +1593,8 @@ impl<'a, K, V, S> RawEntryBuilder<'a, K, V, S> {
1590
1593
where
1591
1594
F : FnMut ( & K ) -> bool ,
1592
1595
{
1593
- match self . map . table . find ( hash, |( k, _) | is_match ( k) ) {
1594
- Some ( item) => unsafe {
1595
- let & ( ref key, ref value) = item. as_ref ( ) ;
1596
- Some ( ( key, value) )
1597
- } ,
1596
+ match self . map . table . get ( hash, |( k, _) | is_match ( k) ) {
1597
+ Some ( & ( ref key, ref value) ) => Some ( ( key, value) ) ,
1598
1598
None => None ,
1599
1599
}
1600
1600
}
@@ -1961,11 +1961,10 @@ impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
1961
1961
where
1962
1962
H : Fn ( & K ) -> u64 ,
1963
1963
{
1964
- unsafe {
1965
- let elem = self . table . insert ( hash, ( key, value) , |x| hasher ( & x. 0 ) ) ;
1966
- let & mut ( ref mut k, ref mut v) = elem. as_mut ( ) ;
1967
- ( k, v)
1968
- }
1964
+ let & mut ( ref mut k, ref mut v) = self
1965
+ . table
1966
+ . insert_entry ( hash, ( key, value) , |x| hasher ( & x. 0 ) ) ;
1967
+ ( k, v)
1969
1968
}
1970
1969
1971
1970
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -2974,10 +2973,11 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
2974
2973
S : BuildHasher ,
2975
2974
{
2976
2975
let hash_builder = & self . table . hash_builder ;
2977
- let bucket = self . table . table . insert ( self . hash , ( self . key , value) , |x| {
2976
+ let table = & mut self . table . table ;
2977
+ let entry = table. insert_entry ( self . hash , ( self . key , value) , |x| {
2978
2978
make_hash ( hash_builder, & x. 0 )
2979
2979
} ) ;
2980
- unsafe { & mut bucket . as_mut ( ) . 1 }
2980
+ & mut entry . 1
2981
2981
}
2982
2982
2983
2983
#[ cfg_attr( feature = "inline-more" , inline) ]
0 commit comments