From 185ed988d240e380c63c7ff809622a270f670637 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Thu, 7 Feb 2019 12:08:37 +0100 Subject: [PATCH] Remove the Recover trait for HashSet --- src/libstd/collections/hash/map.rs | 40 ------------------------------ src/libstd/collections/hash/mod.rs | 8 ------ src/libstd/collections/hash/set.rs | 13 +++++++--- 3 files changed, 9 insertions(+), 52 deletions(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index ac3cfde47b5..e24a824e388 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -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 { - 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 super::Recover for HashMap - where K: Eq + Hash + Borrow, - 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 { - self.search_mut(key).map(|bucket| pop_internal(bucket).0) - } - - #[inline] - fn replace(&mut self, key: K) -> Option { - 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> { diff --git a/src/libstd/collections/hash/mod.rs b/src/libstd/collections/hash/mod.rs index 0d1bbb590db..56585477f1c 100644 --- a/src/libstd/collections/hash/mod.rs +++ b/src/libstd/collections/hash/mod.rs @@ -4,11 +4,3 @@ mod table; pub mod map; pub mod set; - -trait Recover { - type Key; - - fn get(&self, key: &Q) -> Option<&Self::Key>; - fn take(&mut self, key: &Q) -> Option; - fn replace(&mut self, key: Self::Key) -> Option; -} diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index b9fcc2365fa..0fbf374fa49 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -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(&self, value: &Q) -> Option<&T> where T: Borrow, 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 { - 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(&mut self, value: &Q) -> Option where T: Borrow, 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. -- 2.44.0