Skip to content

Commit

Permalink
Addressed issues raised in #44286.
Browse files Browse the repository at this point in the history
This commit renames the `replace` function to `replace_entry`, and
creates a seperate `replace_key` function for `OccupiedEntry`. The
original `replace` function did not solve the use-case where the
key needed to be replaced, but not the value. Documentation and
naming has also been updated to better reflect what the original
replace function does.
  • Loading branch information
Binero committed Nov 11, 2017
1 parent 69ee5a8 commit 0df7f00
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/libstd/collections/hash/map.rs
Expand Up @@ -2239,35 +2239,57 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
self.key.take()
}

/// Replaces the entry, returning the old key and value.
/// 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.
///
/// # Examples
///
/// ```
/// #![feature(map_entry_replace)]
/// use std::collections::HashMap;
/// use std::collections::hash_map::Entry;
/// use std::collections::hash_map::{Entry, HashMap};
///
/// let mut map: HashMap<String, u32> = HashMap::new();
/// map.insert("poneyland".to_string(), 15);
///
/// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
/// let (old_key, old_value): (String, u32) = entry.replace(16);
/// let (old_key, old_value): (String, u32) = entry.replace_entry(16);
/// assert_eq!(old_key, "poneyland");
/// assert_eq!(old_value, 15);
/// }
///
/// assert_eq!(map.get("poneyland"), Some(&16));
/// ```
#[unstable(feature = "map_entry_replace", issue = "44286")]
pub fn replace(mut self, value: V) -> (K, V) {
pub fn replace_entry(mut self, value: V) -> (K, V) {
let (old_key, old_value) = self.elem.read_mut();

let old_key = mem::replace(old_key, self.key.unwrap());
let old_value = mem::replace(old_value, value);

(old_key, old_value)
}

/// Replaces the key in the hash map with the key used to create this entry.
///
/// # Examples
///
/// ```
/// #![feature(map_entry_replace)]
/// use std::collections::hash_map::{Entry, HashMap};
///
/// let mut map: HashMap<String, u32> = HashMap::new();
/// map.insert("poneyland".to_string(), 15);
///
/// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
/// let old_key = entry.replace_key();
/// assert_eq!(old_key, "poneyland");
/// }
/// ```
#[unstable(feature = "map_entry_replace", issue = "44286")]
pub fn replace_key(mut self) -> K {
let (old_key, _) = self.elem.read_mut();
mem::replace(old_key, self.key.unwrap())
}
}

impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
Expand Down

0 comments on commit 0df7f00

Please sign in to comment.