Skip to content

Commit 45caff8

Browse files
committed
Auto merge of #45152 - Binero:master, r=dtolnay
Addressed issues raised in #44286. (`OccupiedEntry::replace_entry`) 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.
2 parents 24bb4d1 + 0fb37fc commit 45caff8

File tree

1 file changed

+43
-11
lines changed
  • src/libstd/collections/hash

1 file changed

+43
-11
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,35 +2239,67 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
22392239
self.key.take()
22402240
}
22412241

2242-
/// Replaces the entry, returning the old key and value.
2242+
/// Replaces the entry, returning the old key and value. The new key in the hash map will be
2243+
/// the key used to create this entry.
22432244
///
22442245
/// # Examples
22452246
///
22462247
/// ```
22472248
/// #![feature(map_entry_replace)]
2248-
/// use std::collections::HashMap;
2249-
/// use std::collections::hash_map::Entry;
2249+
/// use std::collections::hash_map::{Entry, HashMap};
2250+
/// use std::rc::Rc;
2251+
///
2252+
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
2253+
/// map.insert(Rc::new("Stringthing".to_string()), 15);
22502254
///
2251-
/// let mut map: HashMap<String, u32> = HashMap::new();
2252-
/// map.insert("poneyland".to_string(), 15);
2255+
/// let my_key = Rc::new("Stringthing".to_string());
22532256
///
2254-
/// if let Entry::Occupied(entry) = map.entry("poneyland".to_string()) {
2255-
/// let (old_key, old_value): (String, u32) = entry.replace(16);
2256-
/// assert_eq!(old_key, "poneyland");
2257-
/// assert_eq!(old_value, 15);
2257+
/// if let Entry::Occupied(entry) = map.entry(my_key) {
2258+
/// // Also replace the key with a handle to our other key.
2259+
/// let (old_key, old_value): (Rc<String>, u32) = entry.replace_entry(16);
22582260
/// }
22592261
///
2260-
/// assert_eq!(map.get("poneyland"), Some(&16));
22612262
/// ```
22622263
#[unstable(feature = "map_entry_replace", issue = "44286")]
2263-
pub fn replace(mut self, value: V) -> (K, V) {
2264+
pub fn replace_entry(mut self, value: V) -> (K, V) {
22642265
let (old_key, old_value) = self.elem.read_mut();
22652266

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

22692270
(old_key, old_value)
22702271
}
2272+
2273+
/// Replaces the key in the hash map with the key used to create this entry.
2274+
///
2275+
/// # Examples
2276+
///
2277+
/// ```
2278+
/// #![feature(map_entry_replace)]
2279+
/// use std::collections::hash_map::{Entry, HashMap};
2280+
/// use std::rc::Rc;
2281+
///
2282+
/// let mut map: HashMap<Rc<String>, u32> = HashMap::new();
2283+
/// let mut known_strings: Vec<Rc<String>> = Vec::new();
2284+
///
2285+
/// // Initialise known strings, run program, etc.
2286+
///
2287+
/// reclaim_memory(&mut map, &known_strings);
2288+
///
2289+
/// fn reclaim_memory(map: &mut HashMap<Rc<String>, u32>, known_strings: &[Rc<String>] ) {
2290+
/// for s in known_strings {
2291+
/// if let Entry::Occupied(entry) = map.entry(s.clone()) {
2292+
/// // Replaces the entry's key with our version of it in `known_strings`.
2293+
/// entry.replace_key();
2294+
/// }
2295+
/// }
2296+
/// }
2297+
/// ```
2298+
#[unstable(feature = "map_entry_replace", issue = "44286")]
2299+
pub fn replace_key(mut self) -> K {
2300+
let (old_key, _) = self.elem.read_mut();
2301+
mem::replace(old_key, self.key.unwrap())
2302+
}
22712303
}
22722304

22732305
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {

0 commit comments

Comments
 (0)