Skip to content

Commit 27240fe

Browse files
authored
Rollup merge of rust-lang#64656 - passcod:map-entry-insert, r=Amanieu
Implement (HashMap) Entry::insert as per rust-lang#60142 Implementation of `Entry::insert` as per @SimonSapin's comment on rust-lang#60142. This requires a patch to hashbrown: ```diff diff --git a/src/rustc_entry.rs b/src/rustc_entry.rs index fefa5c3..7de8300 100644 --- a/src/rustc_entry.rs +++ b/src/rustc_entry.rs @@ -546,6 +546,32 @@ impl<'a, K, V> RustcVacantEntry<'a, K, V> { let bucket = self.table.insert_no_grow(self.hash, (self.key, value)); unsafe { &mut bucket.as_mut().1 } } + + /// Sets the value of the entry with the RustcVacantEntry's key, + /// and returns a RustcOccupiedEntry. + /// + /// # Examples + /// + /// ``` + /// use hashbrown::HashMap; + /// use hashbrown::hash_map::RustcEntry; + /// + /// let mut map: HashMap<&str, u32> = HashMap::new(); + /// + /// if let RustcEntry::Vacant(v) = map.rustc_entry("poneyland") { + /// let o = v.insert_and_return(37); + /// assert_eq!(o.get(), &37); + /// } + /// ``` + #[inline] + pub fn insert_and_return(self, value: V) -> RustcOccupiedEntry<'a, K, V> { + let bucket = self.table.insert_no_grow(self.hash, (self.key, value)); + RustcOccupiedEntry { + key: None, + elem: bucket, + table: self.table + } + } } impl<K, V> IterMut<'_, K, V> { ``` This is also only an implementation for HashMap. I tried implementing for BTreeMap, but I don't really understand BTreeMap's internals and require more guidance on implementing the equivalent `VacantEntry::insert_and_return` such that it returns an `OccupiedEntry`. Notably, following the original PR's modifications I end up needing a `Handle<NodeRef<marker::Mut<'_>, _, _, marker::LeafOrInternal>, _>` while I only have a `Handle<NodeRef<marker::Mut<'_>, _, _, marker::Internal>, _>` and don't know how to proceed. (To be clear, I'm not asking for guidance right now; I'd be happy getting only the HashMap implementation — the subject of this PR — reviewed and ready, and leave the BTreeMap implementation for a latter PR.)
2 parents b5bd31e + bdcc21c commit 27240fe

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"
@@ -1269,7 +1275,7 @@ version = "2.0.1"
12691275
source = "registry+https://github.com/rust-lang/crates.io-index"
12701276
checksum = "df044dd42cdb7e32f28557b661406fc0f2494be75199779998810dbc35030e0d"
12711277
dependencies = [
1272-
"hashbrown",
1278+
"hashbrown 0.5.0",
12731279
"lazy_static 1.3.0",
12741280
"log",
12751281
"pest",
@@ -1286,10 +1292,19 @@ version = "0.5.0"
12861292
source = "registry+https://github.com/rust-lang/crates.io-index"
12871293
checksum = "e1de41fb8dba9714efd92241565cdff73f78508c95697dd56787d3cba27e2353"
12881294
dependencies = [
1295+
"serde",
1296+
]
1297+
1298+
[[package]]
1299+
name = "hashbrown"
1300+
version = "0.6.1"
1301+
source = "registry+https://github.com/rust-lang/crates.io-index"
1302+
checksum = "6587d09be37fb98a11cb08b9000a3f592451c1b1b613ca69d949160e313a430a"
1303+
dependencies = [
1304+
"autocfg",
12891305
"compiler_builtins",
12901306
"rustc-std-workspace-alloc",
12911307
"rustc-std-workspace-core",
1292-
"serde",
12931308
]
12941309

12951310
[[package]]
@@ -4109,7 +4124,7 @@ dependencies = [
41094124
"core",
41104125
"dlmalloc",
41114126
"fortanix-sgx-abi",
4112-
"hashbrown",
4127+
"hashbrown 0.6.1",
41134128
"libc",
41144129
"panic_abort",
41154130
"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)