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.
///
}
}
-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> {
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>;
-}
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!)
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`.
/// ```
#[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
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.