From 0df7f00a0430df0d9c14e4c51919062f44aadaad Mon Sep 17 00:00:00 2001 From: Jeroen Bollen Date: Mon, 9 Oct 2017 21:06:20 +0200 Subject: [PATCH 1/3] Addressed issues raised in #44286. 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. --- src/libstd/collections/hash/map.rs | 32 +++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 1771e9244143b..cac6d5ab76714 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2239,20 +2239,20 @@ 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 = 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); /// } @@ -2260,7 +2260,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// 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()); @@ -2268,6 +2268,28 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { (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 = 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> { From 0f8ee171b2d78acaee2fb3c7774af531510d945b Mon Sep 17 00:00:00 2001 From: Jeroen Bollen Date: Thu, 12 Oct 2017 11:18:16 +0200 Subject: [PATCH 2/3] Changed tabs back into spaces to fix formatting. --- src/libstd/collections/hash/map.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index cac6d5ab76714..a7b02fb2b7665 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2287,8 +2287,8 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// ``` #[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()) + let (old_key, _) = self.elem.read_mut(); + mem::replace(old_key, self.key.unwrap()) } } From 0fb37fc67d0d0278022f355bed2aac0c2b772835 Mon Sep 17 00:00:00 2001 From: Jeroen Bollen Date: Sat, 11 Nov 2017 16:21:49 +0100 Subject: [PATCH 3/3] Improvided map_entry_replace examples The current examples should be more realistic. --- src/libstd/collections/hash/map.rs | 34 +++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index a7b02fb2b7665..7a79a472d58d9 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2247,17 +2247,18 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// ``` /// #![feature(map_entry_replace)] /// use std::collections::hash_map::{Entry, HashMap}; + /// use std::rc::Rc; /// - /// let mut map: HashMap = HashMap::new(); - /// map.insert("poneyland".to_string(), 15); + /// let mut map: HashMap, u32> = HashMap::new(); + /// map.insert(Rc::new("Stringthing".to_string()), 15); /// - /// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) { - /// let (old_key, old_value): (String, u32) = entry.replace_entry(16); - /// assert_eq!(old_key, "poneyland"); - /// assert_eq!(old_value, 15); + /// let my_key = Rc::new("Stringthing".to_string()); + /// + /// 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, u32) = entry.replace_entry(16); /// } /// - /// assert_eq!(map.get("poneyland"), Some(&16)); /// ``` #[unstable(feature = "map_entry_replace", issue = "44286")] pub fn replace_entry(mut self, value: V) -> (K, V) { @@ -2276,13 +2277,22 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// ``` /// #![feature(map_entry_replace)] /// use std::collections::hash_map::{Entry, HashMap}; + /// use std::rc::Rc; + /// + /// let mut map: HashMap, u32> = HashMap::new(); + /// let mut known_strings: Vec> = Vec::new(); + /// + /// // Initialise known strings, run program, etc. /// - /// let mut map: HashMap = HashMap::new(); - /// map.insert("poneyland".to_string(), 15); + /// reclaim_memory(&mut map, &known_strings); /// - /// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) { - /// let old_key = entry.replace_key(); - /// assert_eq!(old_key, "poneyland"); + /// fn reclaim_memory(map: &mut HashMap, u32>, known_strings: &[Rc] ) { + /// 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(); + /// } + /// } /// } /// ``` #[unstable(feature = "map_entry_replace", issue = "44286")]