Skip to content

Add named lifetimes to entry API methods - #756

Closed
MikkelPaulson wants to merge 1 commit into
rust-lang:mainfrom
MikkelPaulson:add-lifetimes-to-entry-methods
Closed

MikkelPaulson wants to merge 1 commit into
rust-lang:mainfrom
MikkelPaulson:add-lifetimes-to-entry-methods

Conversation

@MikkelPaulson

Copy link
Copy Markdown
Contributor

Added lifetimes where possible referring to the parent HashMap/ HashSet rather than the entry itself. This is not possible for VacantEntry or by extension the Entry itself, but OccupiedEntry already opaquely returns references from the parent.

Affected methods:

  • fn map::OccupiedEntry::key() -> &'a K;
  • fn map::OccupiedEntry::get() -> &'a V;
  • fn map::OccupiedEntry::get_mut() -> &'a mut V;
  • fn set::OccupiedEntry::get() -> &'a V;

Also rewrote tests to inherently validate that the lifetimes are adequate (tests fail to compile against current main), and added some rudimentary coverage for the HashSet entry API.

Added lifetimes where possible referring to the parent `HashMap`/
`HashSet` rather than the entry itself. This is not possible for
`VacantEntry` or by extension the `Entry` itself, but `OccupiedEntry`
already opaquely returns references from the parent.

Affected methods:

* `fn map::OccupiedEntry::key() -> &'a K;`
* `fn map::OccupiedEntry::get() -> &'a V;`
* `fn map::OccupiedEntry::get_mut() -> &'a mut V;`
* `fn set::OccupiedEntry::get() -> &'a V;`

Also rewrote tests to inherently validate that the lifetimes are
adequate (tests fail to compile against current main), and added some
rudimentary coverage for the `HashSet` entry API.
@cuviper

cuviper commented Oct 29, 2026

Copy link
Copy Markdown
Member

No, this is unsound -- e.g. it would allow multiple calls to get_mut() for aliased references, or for any of these references to become dangling by calling remove afterward. This is why into_entry and into_mut both consume the entry.

@cuviper cuviper closed this Oct 29, 2026
@MikkelPaulson

Copy link
Copy Markdown
Contributor Author

Oh, good point. Would you be open to adding hash_set::OccupiedEntry::into_ref(self) -> &'a T?

@cuviper

cuviper commented Oct 29, 2026

Copy link
Copy Markdown
Member

Hmm, right, there's no way to get the full lifetime from the set entry otherwise. Adding into_ref sounds ok to me.

@MikkelPaulson
MikkelPaulson deleted the add-lifetimes-to-entry-methods branch October 30, 2026 00:44
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.

2 participants