Comment why get_or_insert returns &T

This commit is contained in:
Josh Stone
2019-05-16 16:21:31 -07:00
parent 5e2c9d38e9
commit 9161a4dbef
+4
View File
@@ -637,6 +637,8 @@ pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
#[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 @@ pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
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
}