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

Stabilize or_insert_with_key #78083

Open
wants to merge 4 commits into
base: master
from
Open

Stabilize or_insert_with_key #78083

wants to merge 4 commits into from

Conversation

@ChaiTRex
Copy link
Contributor

@ChaiTRex ChaiTRex commented Oct 18, 2020

Stabilizes the or_insert_with_key feature from #71024. This allows inserting key-derived values when a HashMap/BTreeMap entry is vacant.

The difference between this and .or_insert_with(|| ... ) is that this provides a reference to the key to the closure after it is moved with .entry(key_being_moved), avoiding the need to copy or clone the key.

@rust-highfive
Copy link
Collaborator

@rust-highfive rust-highfive commented Oct 18, 2020

r? @varkor

(rust_highfive has picked a reviewer for you, use r? to override)

@varkor
Copy link
Member

@varkor varkor commented Oct 21, 2020

This will needs a libs team sign-off.

r? @SimonSapin

@m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Nov 25, 2020

@rfcbot merge

@rfcbot
Copy link

@rfcbot rfcbot commented Nov 25, 2020

Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Nov 25, 2020

Sorry for the delay! Unfortunately this missed 1.49. Can you update the stable versions to 1.50?

@ChaiTRex
Copy link
Contributor Author

@ChaiTRex ChaiTRex commented Dec 1, 2020

@m-ou-se It is updated in the latest commit.

@BurntSushi
Copy link
Member

@BurntSushi BurntSushi commented Dec 1, 2020

The difference between this and .or_insert_with(|| ... ) is that this provides a reference to the key to the closure after it is moved with .entry(key_being_moved), avoiding the need to copy or clone the key.

This seems like a useful bit of information that would be good to add to the docs.

@ChaiTRex
Copy link
Contributor Author

@ChaiTRex ChaiTRex commented Dec 7, 2020

@BurntSushi How about this?

Ensures a value is in the entry by inserting, if empty, the result of the default function. This method allows for generating key-derived values for insertion by providing the default function a reference to the key that was moved during the .entry(key) method call.

The reference to the moved key is provided so that cloning or copying the key is unnecessary, unlike with .or_insert_with(|| ... ).

@BurntSushi
Copy link
Member

@BurntSushi BurntSushi commented Dec 8, 2020

@ChaiTRex Thank you, that looks great to me!

ChaiTRex added 2 commits Dec 8, 2020
@rfcbot
Copy link

@rfcbot rfcbot commented Dec 8, 2020

🔔 This is now entering its final comment period, as per the review above. 🔔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

9 participants
You can’t perform that action at this time.