Skip to content

Commit 0765536

Browse files
authored
Rollup merge of #78083 - ChaiTRex:master, r=m-ou-se
Stabilize or_insert_with_key Stabilizes the `or_insert_with_key` feature from #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.
2 parents d1741e5 + f115be9 commit 0765536

File tree

3 files changed

+15
-11
lines changed
  • compiler/rustc_data_structures/src/sso
  • library

3 files changed

+15
-11
lines changed

compiler/rustc_data_structures/src/sso/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const SSO_ARRAY_SIZE: usize = 8;
4040
// into_keys/into_values (unstable)
4141
// all raw_entry-related
4242
// PartialEq/Eq (requires sorting the array)
43-
// Entry::or_insert_with_key (unstable)
43+
// Entry::or_insert_with_key
4444
// Vacant/Occupied entries and related
4545
//
4646
// FIXME: In HashMap most methods accepting key reference

library/alloc/src/collections/btree/map/entry.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,16 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
116116
}
117117
}
118118

119-
#[unstable(feature = "or_insert_with_key", issue = "71024")]
120-
/// Ensures a value is in the entry by inserting, if empty, the result of the default function,
121-
/// which takes the key as its argument, and returns a mutable reference to the value in the
122-
/// entry.
119+
/// Ensures a value is in the entry by inserting, if empty, the result of the default function.
120+
/// This method allows for generating key-derived values for insertion by providing the default
121+
/// function a reference to the key that was moved during the `.entry(key)` method call.
122+
///
123+
/// The reference to the moved key is provided so that cloning or copying the key is
124+
/// unnecessary, unlike with `.or_insert_with(|| ... )`.
123125
///
124126
/// # Examples
125127
///
126128
/// ```
127-
/// #![feature(or_insert_with_key)]
128129
/// use std::collections::BTreeMap;
129130
///
130131
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
@@ -134,6 +135,7 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
134135
/// assert_eq!(map["poneyland"], 9);
135136
/// ```
136137
#[inline]
138+
#[stable(feature = "or_insert_with_key", since = "1.50.0")]
137139
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
138140
match self {
139141
Occupied(entry) => entry.into_mut(),

library/std/src/collections/hash/map.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -2219,14 +2219,16 @@ impl<'a, K, V> Entry<'a, K, V> {
22192219
}
22202220
}
22212221

2222-
/// Ensures a value is in the entry by inserting, if empty, the result of the default function,
2223-
/// which takes the key as its argument, and returns a mutable reference to the value in the
2224-
/// entry.
2222+
/// Ensures a value is in the entry by inserting, if empty, the result of the default function.
2223+
/// This method allows for generating key-derived values for insertion by providing the default
2224+
/// function a reference to the key that was moved during the `.entry(key)` method call.
2225+
///
2226+
/// The reference to the moved key is provided so that cloning or copying the key is
2227+
/// unnecessary, unlike with `.or_insert_with(|| ... )`.
22252228
///
22262229
/// # Examples
22272230
///
22282231
/// ```
2229-
/// #![feature(or_insert_with_key)]
22302232
/// use std::collections::HashMap;
22312233
///
22322234
/// let mut map: HashMap<&str, usize> = HashMap::new();
@@ -2236,7 +2238,7 @@ impl<'a, K, V> Entry<'a, K, V> {
22362238
/// assert_eq!(map["poneyland"], 9);
22372239
/// ```
22382240
#[inline]
2239-
#[unstable(feature = "or_insert_with_key", issue = "71024")]
2241+
#[stable(feature = "or_insert_with_key", since = "1.50.0")]
22402242
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
22412243
match self {
22422244
Occupied(entry) => entry.into_mut(),

0 commit comments

Comments
 (0)