From e9f01bcf68b7f01c4b05422068adb3872f68cbaf Mon Sep 17 00:00:00 2001 From: Jeroen Bollen Date: Sat, 2 Sep 2017 23:44:21 +0200 Subject: [PATCH 1/3] Added a way to retrieve the key out of a HashMap when it's being replaced. --- src/libstd/collections/hash/map.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 16b0c70998616..6eb6f892f80fe 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2161,6 +2161,36 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { fn take_key(&mut self) -> Option { self.key.take() } + + /// Replaces the entry, returning the old key and value. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashMap; + /// use std::collections::hash_map::Entry; + /// + /// let mut map: HashMap = HashMap::new(); + /// map.insert(String::from("poneyland"), 15); + /// + /// if let Entry::Occupied(entry) = map.entry(String::from("poneyland")) { + /// let (old_key, old_value): (String, u32) = entry.replace(16); + /// assert_eq!(old_key, "poneyland"); + /// assert_eq!(old_value, 15); + /// } + /// + /// assert_eq!(map.get("poneyland"), Some(&16)); + /// + /// ``` + #[stable(feature = "rust1", since = "1.20.0")] + pub fn replace(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) + } } impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> { From a312b4769808f69607377c8e00a5436c31edf67d Mon Sep 17 00:00:00 2001 From: Jeroen Bollen Date: Sun, 3 Sep 2017 20:16:20 +0200 Subject: [PATCH 2/3] Marked `Entry::replace` as unstable. --- src/libstd/collections/hash/map.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 6eb6f892f80fe..81d79493f0808 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2167,6 +2167,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// # Examples /// /// ``` + /// # #![feature(map_entry_replace)] /// use std::collections::HashMap; /// use std::collections::hash_map::Entry; /// @@ -2182,7 +2183,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// assert_eq!(map.get("poneyland"), Some(&16)); /// /// ``` - #[stable(feature = "rust1", since = "1.20.0")] + #[unstable(feature = "map_entry_replace", issue = "44286")] pub fn replace(mut self, value: V) -> (K, V) { let (old_key, old_value) = self.elem.read_mut(); From d3de465dc85638a6a77298638ebbbfab04b1844d Mon Sep 17 00:00:00 2001 From: Jeroen Bollen Date: Tue, 12 Sep 2017 15:32:10 +0200 Subject: [PATCH 3/3] Addressed @BurntSuchi's remarks regarding Entry::replace --- src/libstd/collections/hash/map.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 81d79493f0808..48dad8bff5d70 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2167,21 +2167,20 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { /// # Examples /// /// ``` - /// # #![feature(map_entry_replace)] + /// #![feature(map_entry_replace)] /// use std::collections::HashMap; /// use std::collections::hash_map::Entry; /// /// let mut map: HashMap = HashMap::new(); - /// map.insert(String::from("poneyland"), 15); + /// map.insert("poneyland".to_string(), 15); /// - /// if let Entry::Occupied(entry) = map.entry(String::from("poneyland")) { + /// 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); /// } /// /// assert_eq!(map.get("poneyland"), Some(&16)); - /// /// ``` #[unstable(feature = "map_entry_replace", issue = "44286")] pub fn replace(mut self, value: V) -> (K, V) {