Skip to content

Add entry into_map methods#686

Merged
Amanieu merged 1 commit intorust-lang:masterfrom
cuviper:into_map
Feb 27, 2026
Merged

Add entry into_map methods#686
Amanieu merged 1 commit intorust-lang:masterfrom
cuviper:into_map

Conversation

@cuviper
Copy link
Member

@cuviper cuviper commented Feb 16, 2026

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.

    /// Converts the `Entry` into a mutable reference to the underlying map.
    pub fn into_map(self) -> &'a mut HashMap<K, V, S, A>;

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 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.

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<K, V, S, A>;
```

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
@cuviper
Copy link
Member Author

cuviper commented Feb 16, 2026

I also considered this for hash_set entries, i.e. fn into_set(self) -> &'a mut HashSet, but with the way they're wrapping the map entries, we would need to "upcast" the into_map reference to a set reference. That's possible, but I think we'd need to add #[repr(transparent)] to HashSet.

@clarfonthey
Copy link
Contributor

So, main concern here is the hasher, and the fact that the table doesn't have one. So, if we were to store a pointer to the table in its entries and want to make the map implemented based upon those, we'd have to probably either do repr(C) or do some nonsense with offset_of to avoid storing two pointers.

But like, it's not a dealbreaker, just something to consider.

@cuviper
Copy link
Member Author

cuviper commented Feb 16, 2026

Even if HashMap is built on HashTable (#666), these entries could keep &mut HashMap and a bucket index here, rather than wrapping the table entry types.

@clarfonthey
Copy link
Contributor

Fair! I guess that bucket indices are part of that API, now, so, it would work pretty well.

@Amanieu Amanieu added this pull request to the merge queue Feb 27, 2026
Merged via the queue into rust-lang:master with commit a1faeab Feb 27, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants