Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
Merge #15
Browse files Browse the repository at this point in the history
15: Merge upstream changes r=cuviper a=cuviper
  • Loading branch information
bors[bot] committed Nov 19, 2017
2 parents a368d16 + 24590b2 commit 5dcf178
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions src/std_hash/map.rs
Expand Up @@ -2252,35 +2252,67 @@ 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 rayon_hash::HashMap;
/// use rayon_hash::hash_map::Entry;
/// use rayon_hash::hash_map::{Entry, HashMap};
/// use std::rc::Rc;
///
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
/// map.insert(Rc::new("Stringthing".to_string()), 15);
///
/// let mut map: HashMap<String, u32> = HashMap::new();
/// map.insert("poneyland".to_string(), 15);
/// let my_key = Rc::new("Stringthing".to_string());
///
/// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
/// let (old_key, old_value): (String, u32) = entry.replace(16);
/// assert_eq!(old_key, "poneyland");
/// assert_eq!(old_value, 15);
/// if let Entry::Occupied(entry) = map.entry(my_key) {
/// // Also replace the key with a handle to our other key.
/// let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
/// }
///
/// assert_eq!(map.get("poneyland"), Some(&16));
/// ```
#[cfg(rayon_hash_unstable)] // #[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 rayon_hash::hash_map::{Entry, HashMap};
/// use std::rc::Rc;
///
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
/// let mut known_strings: Vec<Rc<String>> = Vec::new();
///
/// // Initialise known strings, run program, etc.
///
/// reclaim_memory(&mut map, &known_strings);
///
/// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
/// for s in known_strings {
/// if let Entry::Occupied(entry) = map.entry(s.clone()) {
/// // Replaces the entry's key with our version of it in `known_strings`.
/// entry.replace_key();
/// }
/// }
/// }
/// ```
#[cfg(rayon_hash_unstable)] // #[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 5dcf178

Please sign in to comment.