-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking issue for HashMap OccupiedEntry::{replace_key, replace_entry}
#44286
Comments
HashMap
and BTreeMap
entry to be replaced.HashMap
and BTreeMap
entry to be replaced.
While it is indeed a missing API, there are several issues with this new API implemented in PR #44278:
I think
And probably that's it. If we need to replace key for some reason, |
|
Oh, it is not public API, thanks.
But this function does not allow to replace key without replacing value. BTW, there's already a function to replace value, it's called insert. So if we had
Please ignore that. |
I will take a look at |
@stepancheg I think it's probably better to have |
This commit renames the `replace` function to `replace_entry`, and creates a seperate `replace_key` function for `OccupiedEntry`. The original `replace` function did not solve the use-case where the key needed to be replaced, but not the value. Documentation and naming has also been updated to better reflect what the original replace function does.
Addressed issues raised in #44286. (`OccupiedEntry::replace_entry`) This commit renames the `replace` function to `replace_entry`, and creates a seperate `replace_key` function for `OccupiedEntry`. The original `replace` function did not solve the use-case where the key needed to be replaced, but not the value. Documentation and naming has also been updated to better reflect what the original replace function does.
This has been in Nightly for long enough that I’m inclined to stabilize, but I don’t quite understand the use case. @Binero, can you explain some more why it can be important to recover the old key rather than the new one (passed to |
It is quite a niche function, so it's certainly not instantly obvious where this would be useful. That said, it has some uses when you want to for example lower the memory footprint of a If either |
Not sure if this was the original intent, but I have a use case for match map.entry(key) {
Entry::Occupied(entry) =>
entry.into_mut().do_thing_which_can_fail().map_err(|_| entry.replace_key()),
Entry::Vacant(entry) => ...,
} Without |
HashMap
and BTreeMap
entry to be replaced.OccupiedEntry::{replace_key, replace_entry}
I may be off-base here, but I feel like there is a requirement for these methods that the key continue to have the same hash? If so, that should almost certainly be stated in the documentation. |
That is correct. |
Ah, reading it again, I guess the reason this is okay is because you can only get an |
It's not super easy to spot from this issue alone, but the |
Would somebody mind providing a short status update on what's left before stabilization? |
Maybe it should at least return My ideal API would be: impl OccupiedEntry {
fn swap_keys(&mut self);
// +
fn into_key(self) -> K;
} It allows the most flexibility and I don't see an immediate drawback. fn into_inner(self) -> (K, &mut V);
Basically, I really want my |
These methods will currently edit: that's #65225, as mentioned already in #44286 (comment) |
This is actually really wild. |
I believe that part of hashbrown was just copied from the standard library when it was first implemented. |
The |
I think we should remove this feature because it requires While there are valid use cases for modifying the key of an existing entry in a @rfcbot fcp close |
Team member @Amanieu has proposed to close this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to close, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. |
Update hashbrown to 0.15 and adjust some methods This PR updates `hashbrown` to 0.15 in the standard library and adjust some methods as well as removing some as they no longer exists in Hashbrown it-self. - `HashMap::get_many_mut` change API to return array-of-Option - `HashMap::{replace_entry, replace_key}` are removed, FCP close [already finished](rust-lang#44286 (comment)) - `HashSet::get_or_insert_owned` is removed as it no longer exists in hashbrown Closes rust-lang#44286 r? `@Amanieu`
Update hashbrown to 0.15 and adjust some methods This PR updates `hashbrown` to 0.15 in the standard library and adjust some methods as well as removing some as they no longer exists in Hashbrown it-self. - `HashMap::get_many_mut` change API to return array-of-Option - `HashMap::{replace_entry, replace_key}` are removed, FCP close [already finished](rust-lang/rust#44286 (comment)) - `HashSet::get_or_insert_owned` is removed as it no longer exists in hashbrown Closes rust-lang/rust#44286 r? `@Amanieu`
Note for anyone hit by the removal of these functions: the same result can be achieved with match map.raw_entry_mut().from_key(&key) {
hash_map::RawEntryMut::Occupied(mut occupied) => occupied.insert_key(key),
hash_map::RawEntryMut::Vacant(_) => {}
} |
The raw entry API is also slated for removal. Can you have a look at hashbrown's |
@Amanieu Oh, I wasn't aware. In my particular case I also make use of other more "exotic" features of raw entries (supplying hashes, custom comparison functions), so I will likely need to go for |
@Amanieu hashbrown's |
@constituent If you need to mess with keys in ways where "equivalent" keys actually carry additional data then the correct tool to use is hashbrown's |
At the moment the only way to replace a map entry with a new one, swallows the old key. In cases the user wants to keep the old key, the only way to do so is to first
remove_entry
and to theninsert
a new one. This requires two map look-ups.As the Entry API aims to make operations that would otherwise require two look-ups into operations that require only one, the replacing of an entry is an obvious hole in the API.
OccupiedEntry::replace_entry
) #45152The text was updated successfully, but these errors were encountered: