Confusing lifetime on HashMap's Entry::get #49745
Labels
A-docs
Area: Documentation for any part of the project, including the compiler, standard library, and tools
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
The lifetime on the
get()
method's return value is bound to the lifetime ofEntry
. This is intended, as far as I can tell; however, theinto_mut
method allows theEntry
to be converted into a reference bound to theHashMap
's lifetime. But there is no way to get an immutable reference bound to theHashMap
's lifetime.This issue originally arose from this reddit thread. A minimal usecase example is:
Playground
From my understanding, it should be safe to add an
into_ref(self) -> &'a V
method to fill this hole. It would be implemented as:because the underlying structure
FullBucket
already supports aninto_refs()
method (with the same safety reasoning asinto_mut_refs()
.)It may also be useful to have an
into_key(self) -> &'a K
method. It should be just as safe (and would use basically the same implementation).This was previously opened as an issue (#39099) but was self-closed when the poster found the
into_mut()
workaround.I'd be happy to open a PR for this, but I want to make sure I'm not missing anything obvious here.
The text was updated successfully, but these errors were encountered: