Skip to content
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

Addressed issues raised in #44286. (OccupiedEntry::replace_entry) #45152

Merged
merged 3 commits into from
Nov 11, 2017

Conversation

Binero
Copy link
Contributor

@Binero Binero commented Oct 9, 2017

This commit renames the replace function to replace_entry, and
creates a seperate replace_key function for OccupiedEntry. The
original replace function did not solve the use-case where the
key needed to be replaced, but not the value. Documentation and
naming has also been updated to better reflect what the original
replace function does.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (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.

@Binero Binero changed the title Addressed issues raised in #44286. Addressed issues raised in #44286. (OccupiedEntry::replace_entry) Oct 9, 2017
#[unstable(feature = "map_entry_replace", issue = "44286")]
pub fn replace_key(mut self) -> K {
let (old_key, _) = self.elem.read_mut();
mem::replace(old_key, self.key.unwrap())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the tabs into spaces.

[00:04:04] tidy error: /checkout/src/libstd/collections/hash/map.rs:2289: tab character
[00:04:04] tidy error: /checkout/src/libstd/collections/hash/map.rs:2290: tab character

@aidanhs aidanhs added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 12, 2017
@aidanhs
Copy link
Member

aidanhs commented Oct 19, 2017

Ping @dtolnay for review! (also pinged on IRC)

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reminder!

I am on board with the idea, and the implementation looks good. But could you change the usage example to illustrate a more realistic use case? Replacing "poneyland".to_string() with "poneyland".to_string() is not meaningful.

In general, usage examples are often not about how to use a particular API. If the user got far enough with Rust to be looking at OccupiedEntry documentation, we can assume they understand how to match on an enum and invoke functions. Here and elsewhere, the role of the example should be about why someone would want this functionality.

Either here or in a following PR, please also update the replace_entry documentation with a why example.

@dtolnay dtolnay added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 22, 2017
@carols10cents
Copy link
Member

Triage ping @Binero! Does @dtolnay's request about the documentation example make sense?

@Binero
Copy link
Contributor Author

Binero commented Oct 30, 2017

@dtolnay @carols10cents Oh, whoops. I missed this one. It sure does.

As for replace_key, I am not sure what the use case for that would be, @stepancheg requested it. replace_entry however is the expected behaviour when you use the entry API to replace a value, as otherwise the new key is lost, or the old key does not match the new value. It plugs the API hole resulting from the Entry taking ownership.

Discussion: #44286

@stepancheg
Copy link
Contributor

It can be used for example to reclaim memory used by keys. E. g.

struct RcString { ... }

Vec<RcString> known_strings;

fn reclaim_memory_used_by_keys(map: &mut HashMap<RcString, Something>) {
    for s in &known_strings {
        if let Entry::Occupied(e) = Entrymap.entry(s) {
            e.replace_key();
        }
    }
}

@shepmaster
Copy link
Member

Triage ping @Binero! It looks like you got some feedback on the rationale for replace_key — will you have time to work that back into the documentation?

This commit renames the `replace` function to `replace_entry`, and
creates a seperate `replace_key` function for `OccupiedEntry`. The
original `replace` function did not solve the use-case where the
key needed to be replaced, but not the value. Documentation and
naming has also been updated to better reflect what the original
replace function does.
@Binero
Copy link
Contributor Author

Binero commented Nov 11, 2017

Added better examples, and rebased them all.

@shepmaster shepmaster added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 11, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 11, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 11, 2017

📌 Commit 3ba2631 has been approved by dtolnay

@bors
Copy link
Contributor

bors commented Nov 11, 2017

⌛ Testing commit 3ba26319996e3d1d8a6594a80e3ab0acb89ade66 with merge 5120f57e5210955682db123cfb4a23d12143f80e...

@bors
Copy link
Contributor

bors commented Nov 11, 2017

💔 Test failed - status-appveyor

@dtolnay
Copy link
Member

dtolnay commented Nov 11, 2017

tidy error: C:\projects\rust\src\libstd\collections\hash\map.rs:2256: trailing whitespace
tidy error: C:\projects\rust\src\libstd\collections\hash\map.rs:2284: trailing whitespace
tidy error: C:\projects\rust\src\libstd\collections\hash\map.rs:2286: trailing whitespace
tidy error: C:\projects\rust\src\libstd\collections\hash\map.rs:2288: trailing whitespace
some tidy checks failed

The current examples should be more realistic.
@Binero
Copy link
Contributor Author

Binero commented Nov 11, 2017

Amended the style-changes to the last commit.

@dtolnay
Copy link
Member

dtolnay commented Nov 11, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 11, 2017

📌 Commit 0fb37fc has been approved by dtolnay

@bors
Copy link
Contributor

bors commented Nov 11, 2017

⌛ Testing commit 0fb37fc with merge 45caff8...

bors added a commit that referenced this pull request Nov 11, 2017
Addressed issues raised in #44286. (`OccupiedEntry::replace_entry`)

This commit renames the `replace` function to `replace_entry`, and
creates a seperate `replace_key` function for `OccupiedEntry`. The
original `replace` function did not solve the use-case where the
key needed to be replaced, but not the value. Documentation and
naming has also been updated to better reflect what the original
replace function does.
@bors
Copy link
Contributor

bors commented Nov 11, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: dtolnay
Pushing 45caff8 to master...

@bors bors merged commit 0fb37fc into rust-lang:master Nov 11, 2017
topecongiro added a commit to topecongiro/rust that referenced this pull request Dec 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants