-
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
Implement and_modify
on Entry
#44734
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
34450fd
to
2582a2e
Compare
What about BTreeMap? |
Sorry I had overlooked |
@mchlrhw Thanks for submitting this! I'd like to get @rust-lang/libs's opinion on adding methods like this to Also, I'm not exactly sure if the name |
I would like to match |
🚲🏠 map.entry("poneyland")
.and_modify(|e| *e.new = false)
.or_insert(Foo { new: true }); |
|
@mchlrhw mind elaborating on the rationale here as well a bit? The doc examples here aren't super convincing because that should work today as |
Ah yes, most of my rationale was in a pre-RFC, which I haven't linked to yet, so let me fix that: https://internals.rust-lang.org/t/pre-rfc-and-then-method-on-std-collections-hash-map-entry/5918/2. |
The main purpose is to be able to modify existing entries before you attempt an insert. If your use case allows insert first and then modify then sure, you can use the current API, but if you need to track new state then |
Ok thanks! I'm not 100% sold on the API but don't mind landing it as unstable, the name |
Cool, thanks! I'll change the name now and wait for more feedback on the API. I was worried it was a bit of a niche use case, but I found it useful for something I'm working on and hopefully someone else will find it useful too 😄. Do you think it's worth changing the doctests to use the |
@mchlrhw All righty, this seems fine to me then. Should this get an entry in the unstable book before merging? |
That's great, thanks! That question probably wasn't aimed at me, but if it turns out you do need an entry I'd be happy to write one. |
@mchlrhw Could you change the title of this PR and the tracking issue to mention |
@mchlrhw I think we're trying to make sure there's an entry in the unstable book for every feature, and I think this PR introduces a new feature, so I think it would be good to add it in this PR! cc @steveklabnik |
@mchlrhw Ping. Could you address @BurntSushi's comment? The unstable book page can be written to |
@kennytm Sorry for the delay. I was waiting to see what @steveklabnik's thoughts were. I've had a go at documenting it, but I'm not sure what sort of tone to use, or tense for that matter 😄. Also, I could do with some help with regards to the code snippets and the use of |
@mchlrhw Instead of ```rust
#![feature(entry_and_modify)]
# fn main() {
use std::collections::HashMap;
struct Foo {
new: bool,
}
let mut map: HashMap<&str, Foo> = HashMap::new();
map.entry("quux")
.and_modify(|e| *e.new = false)
.or_insert(Foo { new: true });
# }
``` |
Ah, ok. I'll try and fix that |
Should I squash fix-up commits, or is that considered bad practice once a PR review has started? |
@mchlrhw This PR is small enough that I'm fine with just squashing this down! |
@mchlrhw The docs look great. Thank you! |
Thanks! Do I squash, or is that something that bors does as part of merging? |
@mchlrhw bors won't squash. |
8ac3e9d
to
9e36111
Compare
@bors r+ |
📌 Commit 9e36111 has been approved by |
Implement `and_modify` on `Entry` ## Motivation `Entry`s are useful for allowing access to existing values in a map while also allowing default values to be inserted for absent keys. The existing API is similar to that of `Option`, where `or` and `or_with` can be used if the option variant is `None`. The `Entry` API is, however, missing an equivalent of `Option`'s `and_then` method. If it were present it would be possible to modify an existing entry before calling `or_insert` without resorting to matching on the entry variant. Tracking issue: #44733.
☀️ Test successful - status-appveyor, status-travis |
Thanks everyone! |
…-entry_and_modify, r=alexcrichton Stabilize 'entry_and_modify' feature Stabilize `entry_and_modify` feature introduced by rust-lang#44734. Closes rust-lang#44733
This commit tweaks a few stable APIs in the `beta` branch before they hit stable. The `str::is_whitespace` and `str::is_alphanumeric` functions were deleted (added in rust-lang#49381, issue at rust-lang#49657). The `and_modify` APIs added in rust-lang#44734 were altered to take a `FnOnce` closure rather than a `FnMut` closure. Closes rust-lang#49581 Closes rust-lang#49657
This commit tweaks a few stable APIs in the `beta` branch before they hit stable. The `str::is_whitespace` and `str::is_alphanumeric` functions were deleted (added in rust-lang#49381, issue at rust-lang#49657). The `and_modify` APIs added in rust-lang#44734 were altered to take a `FnOnce` closure rather than a `FnMut` closure. Closes rust-lang#49581 Closes rust-lang#49657
Tweak some stabilizations in libstd This commit tweaks a few stable APIs in the `beta` branch before they hit stable. The `str::is_whitespace` and `str::is_alphanumeric` functions were deleted (added in #49381, issue at #49657). The `and_modify` APIs added in #44734 were altered to take a `FnOnce` closure rather than a `FnMut` closure. Closes #49581 Closes #49657
Motivation
Entry
s are useful for allowing access to existing values in a map while also allowing default values to be inserted for absent keys. The existing API is similar to that ofOption
, whereor
andor_with
can be used if the option variant isNone
.The
Entry
API is, however, missing an equivalent ofOption
'sand_then
method. If it were present it would be possible to modify an existing entry before callingor_insert
without resorting to matching on the entry variant.Tracking issue: #44733.