Uh oh!
There was an error while loading. Please reload this page.
Implement and_modify on Entry - #44734
Conversation
rust-highfive
commented
Sep 21, 2017
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
2582a2eComparekennytm
commented
Sep 21, 2017
What about BTreeMap? |
Sorry I had overlooked |
BurntSushi
commented
Sep 21, 2017
@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 |
mchlrhw
commented
Sep 21, 2017
I would like to match |
🚲🏠 map.entry("poneyland").and_modify(|e| *e.new = false).or_insert(Foo{new:true}); |
mchlrhw
commented
Sep 21, 2017
|
alexcrichton
commented
Sep 21, 2017
@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 |
mchlrhw
commented
Sep 21, 2017
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 |
alexcrichton
commented
Sep 21, 2017
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 |
BurntSushi
commented
Sep 25, 2017
@mchlrhw All righty, this seems fine to me then. Should this get an entry in the unstable book before merging? |
mchlrhw
commented
Sep 26, 2017
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. |
kennytm
commented
Sep 26, 2017
@mchlrhw Could you change the title of this PR and the tracking issue to mention |
BurntSushi
commented
Sep 26, 2017
@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 |
kennytm
commented
Oct 5, 2017
@mchlrhw Ping. Could you address @BurntSushi's comment? The unstable book page can be written to |
mchlrhw
commented
Oct 5, 2017
@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 |
kennytm
commented
Oct 5, 2017
@mchlrhw Instead of ```rust
#![feature(entry_and_modify)]
# fn main(){use std::collections::HashMap;structFoo{new:bool,}letmut map:HashMap<&str,Foo> = HashMap::new();
map.entry("quux").and_modify(|e| *e.new = false).or_insert(Foo{new:true});
# }
``` |
mchlrhw
commented
Oct 5, 2017
Ah, ok. I'll try and fix that |
mchlrhw
commented
Oct 5, 2017
Should I squash fix-up commits, or is that considered bad practice once a PR review has started? |
BurntSushi
commented
Oct 5, 2017
@mchlrhw This PR is small enough that I'm fine with just squashing this down! |
BurntSushi
commented
Oct 5, 2017
@mchlrhw The docs look great. Thank you! |
mchlrhw
commented
Oct 5, 2017
Thanks! Do I squash, or is that something that bors does as part of merging? |
kennytm
commented
Oct 5, 2017
@mchlrhw bors won't squash. |
8ac3e9d to
9e36111CompareBurntSushi
commented
Oct 6, 2017
@bors r+ |
bors
commented
Oct 6, 2017
📌 Commit 9e36111 has been approved by |
bors
commented
Oct 6, 2017
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.
bors
commented
Oct 6, 2017
☀️ Test successful - status-appveyor, status-travis |
mchlrhw
commented
Oct 6, 2017
Thanks everyone! |
…-entry_and_modify, r=alexcrichton Stabilize 'entry_and_modify' feature Stabilize `entry_and_modify` feature introduced by rust-lang#44734. Closesrust-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. Closesrust-lang#49581Closesrust-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. Closesrust-lang#49581Closesrust-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#49581Closes#49657
Motivation
Entrys 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, whereorandor_withcan be used if the option variant isNone.The
EntryAPI is, however, missing an equivalent ofOption'sand_thenmethod. If it were present it would be possible to modify an existing entry before callingor_insertwithout resorting to matching on the entry variant.Tracking issue: #44733.