@@ -1313,6 +1313,7 @@ impl<'a, K, V> InternalEntry<K, V, &'a mut RawTable<K, V>> {
1313
1313
match self {
1314
1314
InternalEntry :: Occupied { elem } => {
1315
1315
Some ( Occupied ( OccupiedEntry {
1316
+ key : Some ( key) ,
1316
1317
elem : elem
1317
1318
} ) )
1318
1319
}
@@ -1347,6 +1348,7 @@ pub enum Entry<'a, K: 'a, V: 'a> {
1347
1348
/// A view into a single occupied location in a HashMap.
1348
1349
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1349
1350
pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
1351
+ key : Option < K > ,
1350
1352
elem : FullBucket < K , V , & ' a mut RawTable < K , V > > ,
1351
1353
}
1352
1354
@@ -1552,6 +1554,12 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
1552
1554
pub fn remove ( self ) -> V {
1553
1555
pop_internal ( self . elem ) . 1
1554
1556
}
1557
+ /// Returns a key that was used for search.
1558
+ ///
1559
+ /// The key was retained for further use.
1560
+ fn take_key ( & mut self ) -> Option < K > {
1561
+ self . key . take ( )
1562
+ }
1555
1563
}
1556
1564
1557
1565
impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
@@ -1661,20 +1669,16 @@ impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
1661
1669
}
1662
1670
1663
1671
fn replace ( & mut self , key : K ) -> Option < K > {
1664
- let hash = self . make_hash ( & key) ;
1665
1672
self . reserve ( 1 ) ;
1666
1673
1667
- match search_hashed ( & mut self . table , hash, |k| * k == key) {
1668
- InternalEntry :: Occupied { mut elem } => {
1669
- Some ( mem:: replace ( elem. read_mut ( ) . 0 , key) )
1674
+ match self . entry ( key) {
1675
+ Occupied ( mut occupied) => {
1676
+ let key = occupied. take_key ( ) . unwrap ( ) ;
1677
+ Some ( mem:: replace ( occupied. elem . read_mut ( ) . 0 , key) )
1670
1678
}
1671
- other => {
1672
- if let Some ( Vacant ( vacant) ) = other. into_entry ( key) {
1673
- vacant. insert ( ( ) ) ;
1674
- None
1675
- } else {
1676
- unreachable ! ( )
1677
- }
1679
+ Vacant ( vacant) => {
1680
+ vacant. insert ( ( ) ) ;
1681
+ None
1678
1682
}
1679
1683
}
1680
1684
}
0 commit comments