-
Notifications
You must be signed in to change notification settings - Fork 13k
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 #![feature(entry_insert)]
#65225
Comments
This does not appear to be compatible with #44286, as the following code will panic. #![feature(map_entry_replace, entry_insert)]
use std::collections::HashMap;
fn main() {
HashMap::new().entry(0).insert(0).replace_key();
} It seems to me that the two APIs are fundamentally incompatible, or at least they are currently incompatible without panicking. The API in #44286 assumes that an |
It seems to me that it is a bit silly to use an I could imagine this would also be useful as a stepping stone towards implementing something like rust-lang/rfcs#1769. |
if this API was not supported.
I'm a bit rusty with this PR, but am I correct in saying that the entry_insert API is impossible to replicate from user code, while the map_entry_replace one is, as per the original PR? (Albeit not with the same efficiency)
Of course I'd much rather a solution that supports both uses is found
|
@passcod I believe you mostly are correct on both parts of your statement. I do not think the The However I do not think it is possible to implement it as a function in safe rust. You would need to pass both the |
Why isn't the signature of this method |
The motivation is to be able to reference the key (or the entry, more generally) even after inserting a value. |
The current implementation is kind of limited, you cannot do something like this: fn insert(&mut self, msg: Message) -> &Message {
self.map.entry(msg).insert(0).key() error[E0515]: cannot return value referencing temporary value It would be convenient if you could implement this in a way which says "insert the key and return a reference of the key from within the HashMap". Currently, you'd have to do this ugly work-around: fn insert(&mut self, msg: Message) -> &Message {
let msg2 = msg.clone();
self.map.entry(msg).insert(0); // or just `.or_insert()`
self.map.get_key_value(&msg2).unwrap().0 |
@lamafab I think you want a function like impl<'a, K, V> OccupiedEntry<'a, K, V> {
pub fn into_key(self) -> &'a K { /* ... */ }
} |
We reviewed this in today's @rust-lang/libs-api meeting. We'd like to see a similar method added to We feel like this should be named Could you rename this method to Could you also consider adding an |
Heyho! Awesome 🦀 I'll try to get to it sometime this month. Edit: still on my radar, probably end of October. |
I recently needed something similar to lamafab where I wanted a reference to the key in the map after inserting
|
Stabilise entry_insert This stabilises `HashMap:Entry::insert_entry` etc. Tracking issue rust-lang#65225. It will need an FCP. This was implemented in rust-lang#64656 two years ago. This PR includes the rename and change discussed in rust-lang#65225 (comment), happy to split if needed.
Suggestion: replace This covers most uses (anything not needing to access the key), and since the only motivation for this method is convenience, there's no need to worry about that case. This then removes the conflict with #44286. |
Didn't want to jump the horse during the unstabilisation moment but I'm unfortunately not terribly interested in this feature anymore, so am unsubbing from it all. This has nothing to do with anyone involved and only to do with me/time, and I generally/superficially agree with the concerns raised for the stabilisation revert. Thanks for all the fish 🐬 |
We briefly discussed this issue last week in the Library API meeting. We'd like to see a more complete proposal for an alternative API, such as the |
One very odd part of this api is that This could be useful in situations where you remove an item, but may or may not need to replace it. In this case you can use Useful additions to this api would be On the topic of key ownership, I think the most consistent approach is to have the
one final note: I am in general not a fan of any method with a |
I've created a mockup for what I think this api could be: https://github.com/maboesanman/InPlace curious what people's thoughts are. |
Am I reading correct there is no movement on this since March? This seems like an unusual omission from the stable API (right now Entry can conditionally insert but not unconditionally insert). I understand the intent of the API is for inserting when key is not present, but there are reasonable purposes for unconditional insert as well, for example, checking the value then inserting if a condition is met:
It seems like the holdup here is not whether the function should go in, but rather disagreement over the return value / naming; but it's been a long time without that disagreement being resolved. (I personally agree with nokel that |
@passcod In the top-comment, the checked checkbox writing that insert_entry was stabilized in 1.59 should be unchecked: it was destabilized in #94105 (just writing that so other people don’t feel compelled to try it out on play.rlo because the docs and this tracking issue currently have different opinions on whether this is stable yet) |
Team member @joshtriplett has proposed to merge 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 merge, 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. This will be merged soon. |
I'm not sure that a new stabilization PR has been done for this? Sorry if this is normal process, I just got pinged on the release note issue and was confused. |
I think there is no stabilization PR yet. |
A stabilization PR needs to be submitted for this, anyone can do it. |
…ChrisDenton Stabilize entry_insert This stabilises `HashMap::Entry::insert_entry`, following the FCP in tracking issue rust-lang#65225. This was implemented in rust-lang#64656 five years ago.
Rollup merge of rust-lang#130290 - passcod:stabilise-entry-insert, r=ChrisDenton Stabilize entry_insert This stabilises `HashMap::Entry::insert_entry`, following the FCP in tracking issue rust-lang#65225. This was implemented in rust-lang#64656 five years ago.
This matches the recently-stabilized methods on `HashMap` entries. I've reused tracking issue rust-lang#65225 for now, but we may want to split it.
This matches the recently-stabilized methods on `HashMap` entries. I've reused tracking issue rust-lang#65225 for now, but we may want to split it.
…manieu btree: add `{Entry,VacantEntry}::insert_entry` This matches the recently-stabilized methods on `HashMap` entries. I've reused tracking issue rust-lang#65225 for now, but we may want to split it.
…manieu btree: add `{Entry,VacantEntry}::insert_entry` This matches the recently-stabilized methods on `HashMap` entries. I've reused tracking issue rust-lang#65225 for now, but we may want to split it.
…manieu btree: add `{Entry,VacantEntry}::insert_entry` This matches the recently-stabilized methods on `HashMap` entries. I've reused tracking issue rust-lang#65225 for now, but we may want to split it.
Rollup merge of rust-lang#133042 - cuviper:btreemap-insert_entry, r=Amanieu btree: add `{Entry,VacantEntry}::insert_entry` This matches the recently-stabilized methods on `HashMap` entries. I've reused tracking issue rust-lang#65225 for now, but we may want to split it.
This is a tracking issue for the
Entry::insert_entry
method on HashMap and BTreeMap introduced in #60142 (comment).Stabilised in 1.59: Stabilise entry_insert #90345Re-stabilised in 1.83: Stabilize entry_insert #130290De-stabilized in Destabilise entry_insert #94105{Entry,VacantEntry}::insert_entry
#133042The text was updated successfully, but these errors were encountered: