@@ -1215,18 +1215,17 @@ where
12151215 #[ cfg_attr( feature = "inline-more" , inline) ]
12161216 pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V , S , A > {
12171217 let hash = make_hash :: < K , S > ( & self . hash_builder , & key) ;
1218- if let Some ( elem) = self . table . raw . find ( hash, equivalent_key ( & key) ) {
1219- Entry :: Occupied ( OccupiedEntry {
1220- hash,
1221- elem,
1222- table : self ,
1223- } )
1224- } else {
1225- Entry :: Vacant ( VacantEntry {
1226- hash,
1218+ let hasher = make_hasher ( & self . hash_builder ) ;
1219+ match self . table . entry ( hash, equivalent_key ( & key) , hasher) {
1220+ hash_table:: Entry :: Occupied ( inner) => Entry :: Occupied ( OccupiedEntry {
1221+ inner,
1222+ marker : PhantomData ,
1223+ } ) ,
1224+ hash_table:: Entry :: Vacant ( inner) => Entry :: Vacant ( VacantEntry {
1225+ inner,
12271226 key,
1228- table : self ,
1229- } )
1227+ marker : PhantomData ,
1228+ } ) ,
12301229 }
12311230 }
12321231
@@ -1253,18 +1252,17 @@ where
12531252 Q : Hash + Equivalent < K > + ?Sized ,
12541253 {
12551254 let hash = make_hash :: < Q , S > ( & self . hash_builder , key) ;
1256- if let Some ( elem) = self . table . raw . find ( hash, equivalent_key ( key) ) {
1257- EntryRef :: Occupied ( OccupiedEntry {
1258- hash,
1259- elem,
1260- table : self ,
1261- } )
1262- } else {
1263- EntryRef :: Vacant ( VacantEntryRef {
1264- hash,
1255+ let hasher = make_hasher ( & self . hash_builder ) ;
1256+ match self . table . entry ( hash, equivalent_key ( key) , hasher) {
1257+ hash_table:: Entry :: Occupied ( inner) => EntryRef :: Occupied ( OccupiedEntry {
1258+ inner,
1259+ marker : PhantomData ,
1260+ } ) ,
1261+ hash_table:: Entry :: Vacant ( inner) => EntryRef :: Vacant ( VacantEntryRef {
1262+ inner,
12651263 key,
1266- table : self ,
1267- } )
1264+ marker : PhantomData ,
1265+ } ) ,
12681266 }
12691267 }
12701268
@@ -1826,13 +1824,10 @@ where
18261824 /// ```
18271825 #[ cfg_attr( feature = "inline-more" , inline) ]
18281826 pub fn insert ( & mut self , k : K , v : V ) -> Option < V > {
1829- let hash = make_hash :: < K , S > ( & self . hash_builder , & k) ;
1830- match self . find_or_find_insert_index ( hash, & k) {
1831- Ok ( bucket) => Some ( mem:: replace ( unsafe { & mut bucket. as_mut ( ) . 1 } , v) ) ,
1832- Err ( index) => {
1833- unsafe {
1834- self . table . raw . insert_at_index ( hash, index, ( k, v) ) ;
1835- }
1827+ match self . entry ( k) {
1828+ Entry :: Occupied ( mut entry) => Some ( entry. insert ( v) ) ,
1829+ Entry :: Vacant ( entry) => {
1830+ entry. insert ( v) ;
18361831 None
18371832 }
18381833 }
@@ -1914,11 +1909,10 @@ where
19141909 #[ cfg_attr( feature = "inline-more" , inline) ]
19151910 pub unsafe fn insert_unique_unchecked ( & mut self , k : K , v : V ) -> ( & K , & mut V ) {
19161911 let hash = make_hash :: < K , S > ( & self . hash_builder , & k) ;
1917- let bucket =
1912+ let entry =
19181913 self . table
1919- . raw
1920- . insert ( hash, ( k, v) , make_hasher :: < _ , V , S > ( & self . hash_builder ) ) ;
1921- let ( k_ref, v_ref) = unsafe { bucket. as_mut ( ) } ;
1914+ . insert_unique ( hash, ( k, v) , make_hasher :: < _ , V , S > ( & self . hash_builder ) ) ;
1915+ let ( k_ref, v_ref) = entry. into_mut ( ) ;
19221916 ( k_ref, v_ref)
19231917 }
19241918
@@ -2812,9 +2806,8 @@ impl<K: Debug, V: Debug, S, A: Allocator> Debug for Entry<'_, K, V, S, A> {
28122806/// assert_eq!(map.len(), 2);
28132807/// ```
28142808pub struct OccupiedEntry < ' a , K , V , S = DefaultHashBuilder , A : Allocator = Global > {
2815- hash : u64 ,
2816- elem : Bucket < ( K , V ) > ,
2817- table : & ' a mut HashMap < K , V , S , A > ,
2809+ inner : hash_table:: OccupiedEntry < ' a , ( K , V ) , A > ,
2810+ marker : PhantomData < & ' a mut S > ,
28182811}
28192812
28202813unsafe impl < K , V , S , A > Send for OccupiedEntry < ' _ , K , V , S , A >
@@ -2874,9 +2867,9 @@ impl<K: Debug, V: Debug, S, A: Allocator> Debug for OccupiedEntry<'_, K, V, S, A
28742867/// assert!(map[&"b"] == 20 && map.len() == 2);
28752868/// ```
28762869pub struct VacantEntry < ' a , K , V , S = DefaultHashBuilder , A : Allocator = Global > {
2877- hash : u64 ,
2870+ inner : hash_table :: VacantEntry < ' a , ( K , V ) , A > ,
28782871 key : K ,
2879- table : & ' a mut HashMap < K , V , S , A > ,
2872+ marker : PhantomData < & ' a mut S > ,
28802873}
28812874
28822875impl < K : Debug , V , S , A : Allocator > Debug for VacantEntry < ' _ , K , V , S , A > {
@@ -3018,9 +3011,9 @@ where
30183011/// assert!(map["b"] == 20 && map.len() == 2);
30193012/// ```
30203013pub struct VacantEntryRef < ' map , ' key , K , Q : ?Sized , V , S , A : Allocator = Global > {
3021- hash : u64 ,
3014+ inner : hash_table :: VacantEntry < ' map , ( K , V ) , A > ,
30223015 key : & ' key Q ,
3023- table : & ' map mut HashMap < K , V , S , A > ,
3016+ marker : PhantomData < & ' map mut S > ,
30243017}
30253018
30263019impl < K , Q , V , S , A > Debug for VacantEntryRef < ' _ , ' _ , K , Q , V , S , A >
@@ -3817,7 +3810,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
38173810 /// ```
38183811 #[ cfg_attr( feature = "inline-more" , inline) ]
38193812 pub fn key ( & self ) -> & K {
3820- unsafe { & self . elem . as_ref ( ) . 0 }
3813+ & self . inner . get ( ) . 0
38213814 }
38223815
38233816 /// Take the ownership of the key and value from the map.
@@ -3846,7 +3839,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
38463839 /// ```
38473840 #[ cfg_attr( feature = "inline-more" , inline) ]
38483841 pub fn remove_entry ( self ) -> ( K , V ) {
3849- unsafe { self . table . table . raw . remove ( self . elem ) . 0 }
3842+ self . inner . remove ( ) . 0
38503843 }
38513844
38523845 /// Gets a reference to the value in the entry.
@@ -3867,7 +3860,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
38673860 /// ```
38683861 #[ cfg_attr( feature = "inline-more" , inline) ]
38693862 pub fn get ( & self ) -> & V {
3870- unsafe { & self . elem . as_ref ( ) . 1 }
3863+ & self . inner . get ( ) . 1
38713864 }
38723865
38733866 /// Gets a mutable reference to the value in the entry.
@@ -3899,7 +3892,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
38993892 /// ```
39003893 #[ cfg_attr( feature = "inline-more" , inline) ]
39013894 pub fn get_mut ( & mut self ) -> & mut V {
3902- unsafe { & mut self . elem . as_mut ( ) . 1 }
3895+ & mut self . inner . get_mut ( ) . 1
39033896 }
39043897
39053898 /// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
@@ -3930,7 +3923,7 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
39303923 /// ```
39313924 #[ cfg_attr( feature = "inline-more" , inline) ]
39323925 pub fn into_mut ( self ) -> & ' a mut V {
3933- unsafe { & mut self . elem . as_mut ( ) . 1 }
3926+ & mut self . inner . into_mut ( ) . 1
39343927 }
39353928
39363929 /// Sets the value of the entry, and returns the entry's old value.
@@ -4036,30 +4029,25 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> {
40364029 where
40374030 F : FnOnce ( & K , V ) -> Option < V > ,
40384031 {
4039- unsafe {
4040- let mut spare_key = None ;
4041-
4042- self . table
4043- . table
4044- . raw
4045- . replace_bucket_with ( self . elem . clone ( ) , |( key, value) | {
4046- if let Some ( new_value) = f ( & key, value) {
4047- Some ( ( key, new_value) )
4048- } else {
4049- spare_key = Some ( key) ;
4050- None
4051- }
4052- } ) ;
4032+ let mut spare_key = None ;
40534033
4054- if let Some ( key) = spare_key {
4055- Entry :: Vacant ( VacantEntry {
4056- hash : self . hash ,
4057- key,
4058- table : self . table ,
4059- } )
4034+ match self . inner . replace_entry_with ( |( key, value) | {
4035+ if let Some ( new_value) = f ( & key, value) {
4036+ Some ( ( key, new_value) )
40604037 } else {
4061- Entry :: Occupied ( self )
4038+ spare_key = Some ( key) ;
4039+ None
40624040 }
4041+ } ) {
4042+ hash_table:: Entry :: Vacant ( inner) => Entry :: Vacant ( VacantEntry {
4043+ inner,
4044+ key : unsafe { spare_key. unwrap_unchecked ( ) } ,
4045+ marker : PhantomData ,
4046+ } ) ,
4047+ hash_table:: Entry :: Occupied ( inner) => Entry :: Occupied ( OccupiedEntry {
4048+ inner,
4049+ marker : PhantomData ,
4050+ } ) ,
40634051 }
40644052 }
40654053}
@@ -4122,13 +4110,7 @@ impl<'a, K, V, S, A: Allocator> VacantEntry<'a, K, V, S, A> {
41224110 K : Hash ,
41234111 S : BuildHasher ,
41244112 {
4125- let table = & mut self . table . table . raw ;
4126- let entry = table. insert_entry (
4127- self . hash ,
4128- ( self . key , value) ,
4129- make_hasher :: < _ , V , S > ( & self . table . hash_builder ) ,
4130- ) ;
4131- & mut entry. 1
4113+ & mut self . inner . insert ( ( self . key , value) ) . into_mut ( ) . 1
41324114 }
41334115
41344116 /// Sets the value of the entry with the [`VacantEntry`]'s key,
@@ -4153,15 +4135,9 @@ impl<'a, K, V, S, A: Allocator> VacantEntry<'a, K, V, S, A> {
41534135 K : Hash ,
41544136 S : BuildHasher ,
41554137 {
4156- let elem = self . table . table . raw . insert (
4157- self . hash ,
4158- ( self . key , value) ,
4159- make_hasher :: < _ , V , S > ( & self . table . hash_builder ) ,
4160- ) ;
41614138 OccupiedEntry {
4162- hash : self . hash ,
4163- elem,
4164- table : self . table ,
4139+ inner : self . inner . insert ( ( self . key , value) ) ,
4140+ marker : PhantomData ,
41654141 }
41664142 }
41674143}
@@ -4461,13 +4437,7 @@ impl<'map, 'key, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'map, 'key, K,
44614437 & ' key Q : Into < K > ,
44624438 S : BuildHasher ,
44634439 {
4464- let table = & mut self . table . table . raw ;
4465- let entry = table. insert_entry (
4466- self . hash ,
4467- ( self . key . into ( ) , value) ,
4468- make_hasher :: < _ , V , S > ( & self . table . hash_builder ) ,
4469- ) ;
4470- & mut entry. 1
4440+ & mut self . inner . insert ( ( self . key . into ( ) , value) ) . into_mut ( ) . 1
44714441 }
44724442
44734443 /// Sets the key and value of the entry and returns a mutable reference to
@@ -4506,17 +4476,11 @@ impl<'map, 'key, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'map, 'key, K,
45064476 Q : Equivalent < K > ,
45074477 S : BuildHasher ,
45084478 {
4509- let table = & mut self . table . table . raw ;
45104479 assert ! (
45114480 ( self . key) . equivalent( & key) ,
45124481 "key used for Entry creation is not equivalent to the one used for insertion"
45134482 ) ;
4514- let entry = table. insert_entry (
4515- self . hash ,
4516- ( key, value) ,
4517- make_hasher :: < _ , V , S > ( & self . table . hash_builder ) ,
4518- ) ;
4519- & mut entry. 1
4483+ & mut self . inner . insert ( ( key, value) ) . into_mut ( ) . 1
45204484 }
45214485
45224486 /// Sets the value of the entry with the [`VacantEntryRef`]'s key,
@@ -4542,15 +4506,9 @@ impl<'map, 'key, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'map, 'key, K,
45424506 & ' key Q : Into < K > ,
45434507 S : BuildHasher ,
45444508 {
4545- let elem = self . table . table . raw . insert (
4546- self . hash ,
4547- ( self . key . into ( ) , value) ,
4548- make_hasher :: < _ , V , S > ( & self . table . hash_builder ) ,
4549- ) ;
45504509 OccupiedEntry {
4551- hash : self . hash ,
4552- elem,
4553- table : self . table ,
4510+ inner : self . inner . insert ( ( self . key . into ( ) , value) ) ,
4511+ marker : PhantomData ,
45544512 }
45554513 }
45564514}
0 commit comments