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