Add reinsert to RawOccupiedEntryMut #450
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It is sometimes useful to modify the key associated with an entry without changing the value. For example, when building a cache where keys can change.
It is currently possible to implement this using hashbrown::HashMap by removing the entry and adding a new one with the same value. However, to prevent conflicts with the new key, the code has to:
This is not possible with the raw entry API. A RawOccupiedEntryMut mutably borrows the map so only one can exist at a time. A workaround is to compute the hash of the new key, do the lookup, destroy the raw entry and rebuild one with the hash later, but this is not ideal as it still does two lookups with the hash when one is sufficient.
RawOccupiedEntryMut already has insert_key that allows changing the key associated with an entry, but it does not actually reinserts the entry into the map, so if the new key has a different hash then the entry will no longer be accessible.
Add a reinsert method to reinsert the entry into the map after its key has been changed.