From 0a1720419e67d6cd851d19fc513535597dbca359 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 16 Feb 2026 10:59:13 -0800 Subject: [PATCH] Add entry `into_map` methods This adds methods to get the original `&'a mut HashMap` reference from `hash_map::{Entry, EntryRef, OccupiedEntry, VacantEntry, VacantEntryRef}` to help with current borrow-checker limitations in returning lifetimes from different branches. ```rust /// Converts the `Entry` into a mutable reference to the underlying map. pub fn into_map(self) -> &'a mut HashMap; ``` There are already similar `into_table` methods on `hash_table` occupied and vacant entries, but this also adds that to `hash_table::Entry` for consistency. This idea was raised in an [internals thread][1] for the standard library, but it's not possible there since those entries don't have any reference to return, especially since they aren't even parameterized by the `S` hasher at all -- they are wrapping `RustcEntry` here. [1]: https://internals.rust-lang.org/t/get-mut-map-back-from-entry-api/24003 --- src/map.rs | 31 +++++++++++++++++++++++++++++++ src/table.rs | 8 ++++++++ 2 files changed, 39 insertions(+) diff --git a/src/map.rs b/src/map.rs index 2154ec3e18..062c340c83 100644 --- a/src/map.rs +++ b/src/map.rs @@ -3712,6 +3712,14 @@ impl<'a, K, V, S, A: Allocator> Entry<'a, K, V, S, A> { Entry::Vacant(_) => self, } } + + /// Converts the `Entry` into a mutable reference to the underlying map. + pub fn into_map(self) -> &'a mut HashMap { + match self { + Entry::Occupied(entry) => entry.table, + Entry::Vacant(entry) => entry.table, + } + } } impl<'a, K, V: Default, S, A: Allocator> Entry<'a, K, V, S, A> { @@ -4136,6 +4144,11 @@ impl<'a, K, V, S, A: Allocator> OccupiedEntry<'a, K, V, S, A> { } } } + + /// Converts the `OccupiedEntry` into a mutable reference to the underlying map. + pub fn into_map(self) -> &'a mut HashMap { + self.table + } } impl<'a, K, V, S, A: Allocator> VacantEntry<'a, K, V, S, A> { @@ -4238,6 +4251,11 @@ impl<'a, K, V, S, A: Allocator> VacantEntry<'a, K, V, S, A> { table: self.table, } } + + /// Converts the `VacantEntry` into a mutable reference to the underlying map. + pub fn into_map(self) -> &'a mut HashMap { + self.table + } } impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> { @@ -4424,6 +4442,14 @@ impl<'a, 'b, K, Q: ?Sized, V, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> { EntryRef::Vacant(entry) => EntryRef::Vacant(entry), } } + + /// Converts the `EntryRef` into a mutable reference to the underlying map. + pub fn into_map(self) -> &'a mut HashMap { + match self { + EntryRef::Occupied(entry) => entry.table, + EntryRef::Vacant(entry) => entry.table, + } + } } impl<'a, 'b, K, Q: ?Sized, V: Default, S, A: Allocator> EntryRef<'a, 'b, K, Q, V, S, A> { @@ -4761,6 +4787,11 @@ impl<'map, 'key, K, Q: ?Sized, V, S, A: Allocator> VacantEntryRef<'map, 'key, K, table: self.table, } } + + /// Converts the `VacantEntryRef` into a mutable reference to the underlying map. + pub fn into_map(self) -> &'map mut HashMap { + self.table + } } impl FromIterator<(K, V)> for HashMap diff --git a/src/table.rs b/src/table.rs index 094350442b..833eef4e58 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1945,6 +1945,14 @@ where Entry::Vacant(entry) => Entry::Vacant(entry), } } + + /// Converts the `Entry` into a mutable reference to the underlying table. + pub fn into_table(self) -> &'a mut HashTable { + match self { + Entry::Occupied(entry) => entry.table, + Entry::Vacant(entry) => entry.table, + } + } } /// A view into an occupied entry in a `HashTable`.