Skip to content

Commit a80a1d0

Browse files
authored
Rollup merge of #60894 - cuviper:hash_set_entry, r=cramertj,Centril
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`.
2 parents 70b38d1 + 9161a4d commit a80a1d0

File tree

1 file changed

+56
-0
lines changed
  • src/libstd/collections/hash

1 file changed

+56
-0
lines changed

src/libstd/collections/hash/set.rs

+56
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,62 @@ impl<T, S> HashSet<T, S>
618618
self.map.get_key_value(value).map(|(k, _)| k)
619619
}
620620

621+
/// Inserts the given `value` into the set if it is not present, then
622+
/// returns a reference to the value in the set.
623+
///
624+
/// # Examples
625+
///
626+
/// ```
627+
/// #![feature(hash_set_entry)]
628+
///
629+
/// use std::collections::HashSet;
630+
///
631+
/// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
632+
/// assert_eq!(set.len(), 3);
633+
/// assert_eq!(set.get_or_insert(2), &2);
634+
/// assert_eq!(set.get_or_insert(100), &100);
635+
/// assert_eq!(set.len(), 4); // 100 was inserted
636+
/// ```
637+
#[inline]
638+
#[unstable(feature = "hash_set_entry", issue = "60896")]
639+
pub fn get_or_insert(&mut self, value: T) -> &T {
640+
// Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
641+
// `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
642+
self.map.raw_entry_mut().from_key(&value).or_insert(value, ()).0
643+
}
644+
645+
/// Inserts a value computed from `f` into the set if the given `value` is
646+
/// not present, then returns a reference to the value in the set.
647+
///
648+
/// # Examples
649+
///
650+
/// ```
651+
/// #![feature(hash_set_entry)]
652+
///
653+
/// use std::collections::HashSet;
654+
///
655+
/// let mut set: HashSet<String> = ["cat", "dog", "horse"]
656+
/// .iter().map(|&pet| pet.to_owned()).collect();
657+
///
658+
/// assert_eq!(set.len(), 3);
659+
/// for &pet in &["cat", "dog", "fish"] {
660+
/// let value = set.get_or_insert_with(pet, str::to_owned);
661+
/// assert_eq!(value, pet);
662+
/// }
663+
/// assert_eq!(set.len(), 4); // a new "fish" was inserted
664+
/// ```
665+
#[inline]
666+
#[unstable(feature = "hash_set_entry", issue = "60896")]
667+
pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
668+
where T: Borrow<Q>,
669+
Q: Hash + Eq,
670+
F: FnOnce(&Q) -> T
671+
{
672+
// Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
673+
// `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
674+
self.map.raw_entry_mut().from_key(value).or_insert_with(|| (f(value), ())).0
675+
}
676+
621677
/// Returns `true` if `self` has no elements in common with `other`.
622678
/// This is equivalent to checking for an empty intersection.
623679
///

0 commit comments

Comments
 (0)