Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement `and_modify` on `Entry` #44734
Conversation
rust-highfive
assigned
BurntSushi
Sep 21, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
mchlrhw
referenced this pull request
Sep 21, 2017
Closed
Tracking issue for `entry_and_modify` #44733
mchlrhw
force-pushed the
mchlrhw:wip/hashmap-entry-and-then
branch
from
34450fd
to
2582a2e
Sep 21, 2017
This comment has been minimized.
This comment has been minimized.
|
What about BTreeMap? |
This comment has been minimized.
This comment has been minimized.
|
Sorry I had overlooked |
This comment has been minimized.
This comment has been minimized.
|
@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 |
BurntSushi
added
the
T-libs
label
Sep 21, 2017
This comment has been minimized.
This comment has been minimized.
|
I would like to match |
This comment has been minimized.
This comment has been minimized.
|
map.entry("poneyland")
.and_modify(|e| *e.new = false)
.or_insert(Foo { new: true }); |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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
added
the
S-waiting-on-review
label
Sep 21, 2017
This comment has been minimized.
This comment has been minimized.
|
Ok thanks! I'm not 100% sold on the API but don't mind landing it as unstable, the name |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
@mchlrhw All righty, this seems fine to me then. Should this get an entry in the unstable book before merging? |
BurntSushi
added
S-waiting-on-author
and removed
S-waiting-on-review
labels
Sep 25, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@mchlrhw Could you change the title of this PR and the tracking issue to mention |
mchlrhw
changed the title
Implement `and_then` on `Entry`
Implement `and_modify` on `Entry`
Sep 26, 2017
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
@mchlrhw Ping. Could you address @BurntSushi's comment? The unstable book page can be written to |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
@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 });
# }
``` |
This comment has been minimized.
This comment has been minimized.
|
Ah, ok. I'll try and fix that |
This comment has been minimized.
This comment has been minimized.
|
Should I squash fix-up commits, or is that considered bad practice once a PR review has started? |
This comment has been minimized.
This comment has been minimized.
|
@mchlrhw This PR is small enough that I'm fine with just squashing this down! |
This comment has been minimized.
This comment has been minimized.
|
@mchlrhw The docs look great. Thank you! |
This comment has been minimized.
This comment has been minimized.
|
Thanks! Do I squash, or is that something that bors does as part of merging? |
This comment has been minimized.
This comment has been minimized.
|
@mchlrhw bors won't squash. |
mchlrhw
force-pushed the
mchlrhw:wip/hashmap-entry-and-then
branch
from
8ac3e9d
to
9e36111
Oct 6, 2017
This comment has been minimized.
This comment has been minimized.
|
@bors r+ |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Oct 6, 2017
This comment has been minimized.
This comment has been minimized.
|
|
bors
merged commit 9e36111
into
rust-lang:master
Oct 6, 2017
mchlrhw
deleted the
mchlrhw:wip/hashmap-entry-and-then
branch
Oct 6, 2017
This comment has been minimized.
This comment has been minimized.
|
Thanks everyone! |
mchlrhw commentedSep 21, 2017
•
edited
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.