-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
entry API v3 #921
entry API v3 #921
Conversation
How about map.entry(key).or_insert(0) += 1
let val = map.entry(key).or_insert_with(|| expensive(big, data));
map_of_vecs.entry(key).or_insert_default().push(0); Drawback: |
It seems to me that we could get many of these benefits by replacing |
@reem that does something different (just returns the default value, ignoring the error, not inserting the default value into the data structure). |
# Alternatives | ||
|
||
Settle for Result chumpsville or abandon this sugar altogether. Truly, fates worse than death. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Pass more information into the
default_with
closure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's literally no information to pass for a vacantentry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A reference to the key?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gankro Was this addressed somewhere off-thread or there's a technical reason it can't be done?
It would be neat to save a clone if the 'expensive' case requires the key to generate the value data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API as a thin layer over Entry can't admit an &Key for the simple reason that Entry doesn't expose this. Otherwise it wasn't included in Entry simply because no one presented a compelling usecase (iirc every use of the old API I saw never used the key even though it was given). It's possible to add new methods that expose this functionality at a later date, though.
@huonw You're right. +1 to this RFC then, I think this is quite clean. |
I concur with @Stebalien, I find the |
Just as an interesting note, Python has something similar called |
} | ||
} | ||
|
||
#[unstable(feature = "collections", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you accidentally the tail end of this attr
This is nice, and hits all my uses of |
In the Java world, there are some libraries aiming to enhance collections usage, which have wildly different APIs, e.g. trove has adjustOrPutValue, while koloboke and Java 8 have compute, computeIfPresent, computeIfAbsent and getOrDefault. Python has a In this context, the suggested API feels right. It's certainly more orthogonal than any of the API designs of other languages. 👍 |
Really like the RFC. Bike-shedding names: here's another suggestion (not necessarily better):
|
I like the idea but I am not wild about the name. Certainly before reading the RFC I had no idea what |
That would be a great addition, as others already voiced I'm unsure about the naming. |
Well done, @gankro! I agree with what many have said about the naming (especially overlap with the |
👍 for or_insert as livable. |
👍 can't get clearer than |
``` | ||
/// Ensures a value is in the entry by inserting the default if empty, and returns | ||
/// a mutable reference to the value in the entry. | ||
pub fn default(self. default: V) -> &'a mut V { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(&mut self, default: V)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self for sure. Want to consume the entry.
Very nice API, I'd prefer the name |
On Tue, 3 Mar 2015 at 10:35 bluss notifications@github.com wrote:
|
This RFC has been merged with overwhelming consensus. The merge updates the text to the |
Replace Entry::get with Entry::default and Entry::default_with for better ergonomics and clearer code.
Rendered