mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 00:34:06 +00:00
Rollup merge of #78083 - ChaiTRex:master, r=m-ou-se
Stabilize or_insert_with_key Stabilizes the `or_insert_with_key` feature from https://github.com/rust-lang/rust/issues/71024. This allows inserting key-derived values when a `HashMap`/`BTreeMap` entry is vacant. The difference between this and `.or_insert_with(|| ... )` is that this provides a reference to the key to the closure after it is moved with `.entry(key_being_moved)`, avoiding the need to copy or clone the key.
This commit is contained in:
commit
0765536c0b
@ -40,7 +40,7 @@ const SSO_ARRAY_SIZE: usize = 8;
|
||||
// into_keys/into_values (unstable)
|
||||
// all raw_entry-related
|
||||
// PartialEq/Eq (requires sorting the array)
|
||||
// Entry::or_insert_with_key (unstable)
|
||||
// Entry::or_insert_with_key
|
||||
// Vacant/Occupied entries and related
|
||||
//
|
||||
// FIXME: In HashMap most methods accepting key reference
|
||||
|
@ -116,15 +116,16 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "or_insert_with_key", issue = "71024")]
|
||||
/// Ensures a value is in the entry by inserting, if empty, the result of the default function,
|
||||
/// which takes the key as its argument, and returns a mutable reference to the value in the
|
||||
/// entry.
|
||||
/// Ensures a value is in the entry by inserting, if empty, the result of the default function.
|
||||
/// This method allows for generating key-derived values for insertion by providing the default
|
||||
/// function a reference to the key that was moved during the `.entry(key)` method call.
|
||||
///
|
||||
/// The reference to the moved key is provided so that cloning or copying the key is
|
||||
/// unnecessary, unlike with `.or_insert_with(|| ... )`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(or_insert_with_key)]
|
||||
/// use std::collections::BTreeMap;
|
||||
///
|
||||
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
|
||||
@ -134,6 +135,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
|
||||
/// assert_eq!(map["poneyland"], 9);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "or_insert_with_key", since = "1.50.0")]
|
||||
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
|
||||
match self {
|
||||
Occupied(entry) => entry.into_mut(),
|
||||
|
@ -2219,14 +2219,16 @@ impl<'a, K, V> Entry<'a, K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensures a value is in the entry by inserting, if empty, the result of the default function,
|
||||
/// which takes the key as its argument, and returns a mutable reference to the value in the
|
||||
/// entry.
|
||||
/// Ensures a value is in the entry by inserting, if empty, the result of the default function.
|
||||
/// This method allows for generating key-derived values for insertion by providing the default
|
||||
/// function a reference to the key that was moved during the `.entry(key)` method call.
|
||||
///
|
||||
/// The reference to the moved key is provided so that cloning or copying the key is
|
||||
/// unnecessary, unlike with `.or_insert_with(|| ... )`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(or_insert_with_key)]
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let mut map: HashMap<&str, usize> = HashMap::new();
|
||||
@ -2236,7 +2238,7 @@ impl<'a, K, V> Entry<'a, K, V> {
|
||||
/// assert_eq!(map["poneyland"], 9);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "or_insert_with_key", issue = "71024")]
|
||||
#[stable(feature = "or_insert_with_key", since = "1.50.0")]
|
||||
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
|
||||
match self {
|
||||
Occupied(entry) => entry.into_mut(),
|
||||
|
Loading…
Reference in New Issue
Block a user