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

Projects
None yet
9 participants
@Binero
Contributor

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

This comment has been minimized.

Show comment
Hide comment
@rust-highfive

rust-highfive Oct 9, 2017

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.

Collaborator

rust-highfive commented Oct 9, 2017

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 from Addressed issues raised in #44286. to Addressed issues raised in #44286. (`OccupiedEntry::replace_entry`) Oct 9, 2017

Show outdated Hide outdated src/libstd/collections/hash/map.rs Outdated
@aidanhs

This comment has been minimized.

Show comment
Hide comment
@aidanhs

aidanhs Oct 19, 2017

Member

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

Member

aidanhs commented Oct 19, 2017

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

@dtolnay

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.

@carols10cents

This comment has been minimized.

Show comment
Hide comment
@carols10cents

carols10cents Oct 30, 2017

Member

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

Member

carols10cents commented Oct 30, 2017

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

@Binero

This comment has been minimized.

Show comment
Hide comment
@Binero

Binero Oct 30, 2017

Contributor

@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

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@stepancheg

stepancheg Oct 30, 2017

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();
        }
    }
}
Contributor

stepancheg commented Oct 30, 2017

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

This comment has been minimized.

Show comment
Hide comment
@shepmaster

shepmaster Nov 10, 2017

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?

Member

shepmaster commented Nov 10, 2017

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?

Binero added some commits Oct 9, 2017

Addressed issues raised in #44286.
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

This comment has been minimized.

Show comment
Hide comment
@Binero

Binero Nov 11, 2017

Contributor

Added better examples, and rebased them all.

Contributor

Binero commented Nov 11, 2017

Added better examples, and rebased them all.

@dtolnay

This comment has been minimized.

Show comment
Hide comment
@dtolnay
Member

dtolnay commented Nov 11, 2017

@bors r+

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 11, 2017

Contributor

📌 Commit 3ba2631 has been approved by dtolnay

Contributor

bors commented Nov 11, 2017

📌 Commit 3ba2631 has been approved by dtolnay

bors added a commit that referenced this pull request Nov 11, 2017

Auto merge of #45152 - Binero:master, r=dtolnay
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

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 11, 2017

Contributor

⌛️ Testing commit 3ba2631 with merge 5120f57...

Contributor

bors commented Nov 11, 2017

⌛️ Testing commit 3ba2631 with merge 5120f57...

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 11, 2017

Contributor

💔 Test failed - status-appveyor

Contributor

bors commented Nov 11, 2017

💔 Test failed - status-appveyor

@dtolnay

This comment has been minimized.

Show comment
Hide comment
@dtolnay

dtolnay Nov 11, 2017

Member
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
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
Improvided map_entry_replace examples
The current examples should be more realistic.
@Binero

This comment has been minimized.

Show comment
Hide comment
@Binero

Binero Nov 11, 2017

Contributor

Amended the style-changes to the last commit.

Contributor

Binero commented Nov 11, 2017

Amended the style-changes to the last commit.

@dtolnay

This comment has been minimized.

Show comment
Hide comment
@dtolnay
Member

dtolnay commented Nov 11, 2017

@bors r+

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 11, 2017

Contributor

📌 Commit 0fb37fc has been approved by dtolnay

Contributor

bors commented Nov 11, 2017

📌 Commit 0fb37fc has been approved by dtolnay

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 11, 2017

Contributor

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

Contributor

bors commented Nov 11, 2017

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

bors added a commit that referenced this pull request Nov 11, 2017

Auto merge of #45152 - Binero:master, r=dtolnay
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

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 11, 2017

Contributor

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

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

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details

@Binero Binero referenced this pull request Nov 18, 2017

Open

Allow a `HashMap` and `BTreeMap` entry to be replaced. #44286

2 of 2 tasks complete

topecongiro added a commit to topecongiro/rust that referenced this pull request Dec 26, 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