about summary refs log tree commit diff
path: root/src/libstd/collections
diff options
context:
space:
mode:
authorPiotr Czarnecki <pioczarn@gmail.com>2016-03-17 23:11:22 +0100
committerPiotr Czarnecki <pioczarn@gmail.com>2016-03-17 23:11:22 +0100
commit6a1ccf8a86a0753cfbec3ad659b4f3ed58a95867 (patch)
tree2ef67295fb9d1a9d6c503f8f6df2a95e08e4d318 /src/libstd/collections
parentef874310f2c11671e96fd700aceb9dcbc44db983 (diff)
downloadrust-6a1ccf8a86a0753cfbec3ad659b4f3ed58a95867.tar.gz
rust-6a1ccf8a86a0753cfbec3ad659b4f3ed58a95867.zip
fixup Cleaner Recover::replace
Diffstat (limited to 'src/libstd/collections')
-rw-r--r--src/libstd/collections/hash/map.rs26
1 files changed, 15 insertions, 11 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs
index 8fb703e8fb8..e149a5131fe 100644
--- a/src/libstd/collections/hash/map.rs
+++ b/src/libstd/collections/hash/map.rs
@@ -1313,6 +1313,7 @@ impl<'a, K, V> InternalEntry<K, V, &'a mut RawTable<K, V>> {
         match self {
             InternalEntry::Occupied { elem } => {
                 Some(Occupied(OccupiedEntry {
+                    key: Some(key),
                     elem: elem
                 }))
             }
@@ -1347,6 +1348,7 @@ pub enum Entry<'a, K: 'a, V: 'a> {
 /// A view into a single occupied location in a HashMap.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
+    key: Option<K>,
     elem: FullBucket<K, V, &'a mut RawTable<K, V>>,
 }
 
@@ -1552,6 +1554,12 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
     pub fn remove(self) -> V {
         pop_internal(self.elem).1
     }
+    /// Returns a key that was used for search.
+    ///
+    /// The key was retained for further use.
+    fn take_key(&mut self) -> Option<K> {
+        self.key.take()
+    }
 }
 
 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>
     }
 
     fn replace(&mut self, key: K) -> Option<K> {
-        let hash = self.make_hash(&key);
         self.reserve(1);
 
-        match search_hashed(&mut self.table, hash, |k| *k == key) {
-            InternalEntry::Occupied { mut elem } => {
-                Some(mem::replace(elem.read_mut().0, key))
+        match self.entry(key) {
+            Occupied(mut occupied) => {
+                let key = occupied.take_key().unwrap();
+                Some(mem::replace(occupied.elem.read_mut().0, key))
             }
-            other => {
-                if let Some(Vacant(vacant)) = other.into_entry(key) {
-                    vacant.insert(());
-                    None
-                } else {
-                    unreachable!()
-                }
+            Vacant(vacant) => {
+                vacant.insert(());
+                None
             }
         }
     }