]> git.lizzy.rs Git - rust.git/commitdiff
Remove the Recover trait for HashSet
authorAmanieu d'Antras <amanieu@gmail.com>
Thu, 7 Feb 2019 11:08:37 +0000 (12:08 +0100)
committerAmanieu d'Antras <amanieu@gmail.com>
Tue, 23 Apr 2019 22:54:14 +0000 (06:54 +0800)
src/libstd/collections/hash/map.rs
src/libstd/collections/hash/mod.rs
src/libstd/collections/hash/set.rs

index ac3cfde47b5202033e8be90378258ed6a7183043..e24a824e38807186d09caa310a1a194d19d90d69 100644 (file)
@@ -2937,13 +2937,6 @@ 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()
-    }
-
     /// Replaces the entry, returning the old key and value. The new key in the hash map will be
     /// the key used to create this entry.
     ///
@@ -3262,39 +3255,6 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     }
 }
 
-impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
-    where K: Eq + Hash + Borrow<Q>,
-          S: BuildHasher,
-          Q: Eq + Hash
-{
-    type Key = K;
-
-    #[inline]
-    fn get(&self, key: &Q) -> Option<&K> {
-        self.search(key).map(|bucket| bucket.into_refs().0)
-    }
-
-    fn take(&mut self, key: &Q) -> Option<K> {
-        self.search_mut(key).map(|bucket| pop_internal(bucket).0)
-    }
-
-    #[inline]
-    fn replace(&mut self, key: K) -> Option<K> {
-        self.reserve(1);
-
-        match self.entry(key) {
-            Occupied(mut occupied) => {
-                let key = occupied.take_key().unwrap();
-                Some(mem::replace(occupied.elem.read_mut().0, key))
-            }
-            Vacant(vacant) => {
-                vacant.insert(());
-                None
-            }
-        }
-    }
-}
-
 #[allow(dead_code)]
 fn assert_covariance() {
     fn map_key<'new>(v: HashMap<&'static str, u8>) -> HashMap<&'new str, u8> {
index 0d1bbb590db9ed7163b5144565fd9929785e77e8..56585477f1c1735f677cd7760c84e962c3537626 100644 (file)
@@ -4,11 +4,3 @@
 mod table;
 pub mod map;
 pub mod set;
-
-trait Recover<Q: ?Sized> {
-    type Key;
-
-    fn get(&self, key: &Q) -> Option<&Self::Key>;
-    fn take(&mut self, key: &Q) -> Option<Self::Key>;
-    fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
-}
index b9fcc2365fa7c87efc0297779c656c8422fa82a3..0fbf374fa496a6fc6dfa38d481207a262bcad812 100644 (file)
@@ -4,7 +4,6 @@
 use crate::iter::{Chain, FromIterator, FusedIterator};
 use crate::ops::{BitOr, BitAnd, BitXor, Sub};
 
-use super::Recover;
 use super::map::{self, HashMap, Keys, RandomState};
 
 // Future Optimization (FIXME!)
@@ -579,7 +578,7 @@ pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
         where T: Borrow<Q>,
               Q: Hash + Eq
     {
-        Recover::get(&self.map, value)
+        self.map.get_key_value(value).map(|(k, _)| k)
     }
 
     /// Returns `true` if `self` has no elements in common with `other`.
@@ -699,7 +698,13 @@ pub fn insert(&mut self, value: T) -> bool {
     /// ```
     #[stable(feature = "set_recovery", since = "1.9.0")]
     pub fn replace(&mut self, value: T) -> Option<T> {
-        Recover::replace(&mut self.map, value)
+        match self.map.entry(value) {
+            map::Entry::Occupied(occupied) => Some(occupied.replace_key()),
+            map::Entry::Vacant(vacant) => {
+                vacant.insert(());
+                None
+            }
+        }
     }
 
     /// Removes a value from the set. Returns whether the value was
@@ -754,7 +759,7 @@ pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
         where T: Borrow<Q>,
               Q: Hash + Eq
     {
-        Recover::take(&mut self.map, value)
+        self.map.remove_entry(value).map(|(k, _)| k)
     }
 
     /// Retains only the elements specified by the predicate.