Skip to content

Commit

Permalink
Comment why get_or_insert returns &T
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed May 16, 2019
1 parent 5e2c9d3 commit 9161a4d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/libstd/collections/hash/set.rs
Expand Up @@ -637,6 +637,8 @@ impl<T, S> HashSet<T, S>
#[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
}

Expand Down Expand Up @@ -667,6 +669,8 @@ impl<T, S> HashSet<T, S>
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
}

Expand Down

0 comments on commit 9161a4d

Please sign in to comment.