Skip to content

Commit bdcc21c

Browse files
committed
Implement (HashMap) Entry::insert as per #60142
1 parent 7870050 commit bdcc21c

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

Cargo.lock

+18-3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ dependencies = [
107107
"winapi 0.3.6",
108108
]
109109

110+
[[package]]
111+
name = "autocfg"
112+
version = "0.1.6"
113+
source = "registry+https://github.com/rust-lang/crates.io-index"
114+
checksum = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875"
115+
110116
[[package]]
111117
name = "backtrace"
112118
version = "0.3.37"
@@ -1259,7 +1265,7 @@ version = "2.0.1"
12591265
source = "registry+https://github.com/rust-lang/crates.io-index"
12601266
checksum = "df044dd42cdb7e32f28557b661406fc0f2494be75199779998810dbc35030e0d"
12611267
dependencies = [
1262-
"hashbrown",
1268+
"hashbrown 0.5.0",
12631269
"lazy_static 1.3.0",
12641270
"log",
12651271
"pest",
@@ -1276,10 +1282,19 @@ version = "0.5.0"
12761282
source = "registry+https://github.com/rust-lang/crates.io-index"
12771283
checksum = "e1de41fb8dba9714efd92241565cdff73f78508c95697dd56787d3cba27e2353"
12781284
dependencies = [
1285+
"serde",
1286+
]
1287+
1288+
[[package]]
1289+
name = "hashbrown"
1290+
version = "0.6.1"
1291+
source = "registry+https://github.com/rust-lang/crates.io-index"
1292+
checksum = "6587d09be37fb98a11cb08b9000a3f592451c1b1b613ca69d949160e313a430a"
1293+
dependencies = [
1294+
"autocfg",
12791295
"compiler_builtins",
12801296
"rustc-std-workspace-alloc",
12811297
"rustc-std-workspace-core",
1282-
"serde",
12831298
]
12841299

12851300
[[package]]
@@ -4073,7 +4088,7 @@ dependencies = [
40734088
"core",
40744089
"dlmalloc",
40754090
"fortanix-sgx-abi",
4076-
"hashbrown",
4091+
"hashbrown 0.6.1",
40774092
"libc",
40784093
"panic_abort",
40794094
"panic_unwind",

src/libstd/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ libc = { version = "0.2.51", default-features = false, features = ['rustc-dep-of
2323
compiler_builtins = { version = "0.1.16" }
2424
profiler_builtins = { path = "../libprofiler_builtins", optional = true }
2525
unwind = { path = "../libunwind" }
26-
hashbrown = { version = "0.5.0", features = ['rustc-dep-of-std'] }
26+
hashbrown = { version = "0.6.1", default-features = false, features = ['rustc-dep-of-std'] }
2727

2828
[dependencies.backtrace_rs]
2929
package = "backtrace"

src/libstd/collections/hash/map.rs

+47
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,31 @@ impl<'a, K, V> Entry<'a, K, V> {
20302030
Vacant(entry) => Vacant(entry),
20312031
}
20322032
}
2033+
2034+
/// Sets the value of the entry, and returns an OccupiedEntry.
2035+
///
2036+
/// # Examples
2037+
///
2038+
/// ```
2039+
/// #![feature(entry_insert)]
2040+
/// use std::collections::HashMap;
2041+
///
2042+
/// let mut map: HashMap<&str, String> = HashMap::new();
2043+
/// let entry = map.entry("poneyland").insert("hoho".to_string());
2044+
///
2045+
/// assert_eq!(entry.key(), &"poneyland");
2046+
/// ```
2047+
#[inline]
2048+
#[unstable(feature = "entry_insert", issue = "65225")]
2049+
pub fn insert(self, value: V) -> OccupiedEntry<'a, K, V> {
2050+
match self {
2051+
Occupied(mut entry) => {
2052+
entry.insert(value);
2053+
entry
2054+
},
2055+
Vacant(entry) => entry.insert_entry(value),
2056+
}
2057+
}
20332058
}
20342059

20352060
impl<'a, K, V: Default> Entry<'a, K, V> {
@@ -2347,6 +2372,28 @@ impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
23472372
pub fn insert(self, value: V) -> &'a mut V {
23482373
self.base.insert(value)
23492374
}
2375+
2376+
/// Sets the value of the entry with the VacantEntry's key,
2377+
/// and returns an OccupiedEntry.
2378+
///
2379+
/// # Examples
2380+
///
2381+
/// ```
2382+
/// use std::collections::HashMap;
2383+
/// use std::collections::hash_map::Entry;
2384+
///
2385+
/// let mut map: HashMap<&str, u32> = HashMap::new();
2386+
///
2387+
/// if let Entry::Vacant(o) = map.entry("poneyland") {
2388+
/// o.insert(37);
2389+
/// }
2390+
/// assert_eq!(map["poneyland"], 37);
2391+
/// ```
2392+
#[inline]
2393+
fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
2394+
let base = self.base.insert_entry(value);
2395+
OccupiedEntry { base }
2396+
}
23502397
}
23512398

23522399
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)