From 5f938342ce72816bb4e8bc6f6144ffaa0ca08b05 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 16 May 2019 15:10:52 -0700 Subject: [PATCH 1/3] Add entry-like methods to HashSet * `HashSet::get_or_insert` * `HashSet::get_or_insert_with` These provide a simplification of the `Entry` API for `HashSet`, with names chosen to match the similar methods on `Option`. --- src/libstd/collections/hash/set.rs | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index b56a27c80bc..ca694d2135e 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -618,6 +618,58 @@ impl HashSet self.map.get_key_value(value).map(|(k, _)| k) } + /// Inserts the given `value` into the set if it is not present, then + /// returns a reference to the value in the set. + /// + /// # Examples + /// + /// ``` + /// #![feature(hash_set_entry)] + /// + /// use std::collections::HashSet; + /// + /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// assert_eq!(set.len(), 3); + /// assert_eq!(set.get_or_insert(2), &2); + /// assert_eq!(set.get_or_insert(100), &100); + /// assert_eq!(set.len(), 4); // 100 was inserted + /// ``` + #[inline] + #[unstable(feature = "hash_set_entry", issue = "0")] + pub fn get_or_insert(&mut self, value: T) -> &T { + self.map.raw_entry_mut().from_key(&value).or_insert(value, ()).0 + } + + /// Inserts a value computed from `f` into the set if the given `value` is + /// not present, then returns a reference to the value in the set. + /// + /// # Examples + /// + /// ``` + /// #![feature(hash_set_entry)] + /// + /// use std::collections::HashSet; + /// + /// let mut set: HashSet = ["cat", "dog", "horse"] + /// .iter().map(|&pet| pet.to_owned()).collect(); + /// + /// assert_eq!(set.len(), 3); + /// for &pet in &["cat", "dog", "fish"] { + /// let value = set.get_or_insert_with(pet, str::to_owned); + /// assert_eq!(value, pet); + /// } + /// assert_eq!(set.len(), 4); // a new "fish" was inserted + /// ``` + #[inline] + #[unstable(feature = "hash_set_entry", issue = "0")] + pub fn get_or_insert_with(&mut self, value: &Q, f: F) -> &T + where T: Borrow, + Q: Hash + Eq, + F: FnOnce(&Q) -> T + { + self.map.raw_entry_mut().from_key(value).or_insert_with(|| (f(value), ())).0 + } + /// Returns `true` if `self` has no elements in common with `other`. /// This is equivalent to checking for an empty intersection. /// From 5e2c9d38e9300239ebd1127cc0ed7424e9af3867 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 16 May 2019 15:37:01 -0700 Subject: [PATCH 2/3] Add a hash_set_entry tracking issue --- src/libstd/collections/hash/set.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index ca694d2135e..c3903806127 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -635,7 +635,7 @@ impl HashSet /// assert_eq!(set.len(), 4); // 100 was inserted /// ``` #[inline] - #[unstable(feature = "hash_set_entry", issue = "0")] + #[unstable(feature = "hash_set_entry", issue = "60896")] pub fn get_or_insert(&mut self, value: T) -> &T { self.map.raw_entry_mut().from_key(&value).or_insert(value, ()).0 } @@ -661,7 +661,7 @@ impl HashSet /// assert_eq!(set.len(), 4); // a new "fish" was inserted /// ``` #[inline] - #[unstable(feature = "hash_set_entry", issue = "0")] + #[unstable(feature = "hash_set_entry", issue = "60896")] pub fn get_or_insert_with(&mut self, value: &Q, f: F) -> &T where T: Borrow, Q: Hash + Eq, From 9161a4dbefd613fd86fab4bfea88ad93c55fb4da Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 16 May 2019 16:21:31 -0700 Subject: [PATCH 3/3] Comment why get_or_insert returns &T --- src/libstd/collections/hash/set.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index c3903806127..403914c0707 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -637,6 +637,8 @@ impl HashSet #[inline] #[unstable(feature = "hash_set_entry", issue = "60896")] pub fn get_or_insert(&mut self, value: T) -> &T { + // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with + // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`. self.map.raw_entry_mut().from_key(&value).or_insert(value, ()).0 } @@ -667,6 +669,8 @@ impl HashSet Q: Hash + Eq, F: FnOnce(&Q) -> T { + // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with + // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`. self.map.raw_entry_mut().from_key(value).or_insert_with(|| (f(value), ())).0 }