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

Tracking Issue for feature: "option_insert" #78271

Closed
4 tasks done
Canop opened this issue Oct 23, 2020 · 14 comments
Closed
4 tasks done

Tracking Issue for feature: "option_insert" #78271

Canop opened this issue Oct 23, 2020 · 14 comments
Labels
A-result-option Area: Result and Option combinators C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Canop
Copy link
Contributor

Canop commented Oct 23, 2020

This is a tracking issue for the feature "option_insert".
The feature gate for the issue is #[unstable(feature = "option_insert")].

This feature adds a insert method to any Option, setting its value and returning a mutable reference to this value.
This method removes a cause of unwrap and code complexity.
It allows replacing

option_value = Some(build());
option_value.as_mut().unwrap()

with

option_value.insert(build())

Steps / History

@Canop Canop added the C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. label Oct 23, 2020
@m-ou-se m-ou-se added A-result-option Area: Result and Option combinators T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 23, 2020
@KodrAus KodrAus added the Libs-Tracked Libs issues that are tracked on the team's project board. label Nov 6, 2020
@ThePuzzlemaker
Copy link
Contributor

ThePuzzlemaker commented Nov 15, 2020

I'd like to work on stabilizing this.

As for a name, I think that either insert_and_get_mut or replace_and_get_mut would be better than insert_and_get and replace_and_get as it implies that you are getting a mutable reference just from the name, without having to look at any signatures or documentation.

@ThePuzzlemaker ThePuzzlemaker removed their assignment Nov 15, 2020
@kupiakos
Copy link
Contributor

insert seems like the best name for this to me, as that matches the existing naming of get_or_insert and get_or_insert_with. insert is just the unconditional version of get_or_insert.

@m-ou-se
Copy link
Member

m-ou-se commented Mar 31, 2021

@rfcbot merge

@rfcbot
Copy link

rfcbot commented Mar 31, 2021

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.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Mar 31, 2021
@rfcbot
Copy link

rfcbot commented Mar 31, 2021

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

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Mar 31, 2021
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Apr 10, 2021
@rfcbot
Copy link

rfcbot commented Apr 10, 2021

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@et342
Copy link
Contributor

et342 commented Apr 14, 2021

Should this be called get_replace?

Say, if we have a box and if we put a thing into it, then what we get is a box filled with the thing, and not the thing itself. But, we do get the thing here, so prefix get_ seems appropriate. But, get_insert does not seems appropriate, because if box is filled − then we cannot insert, we can only replace and replacement is what is what's happening here, so get_replace.

@Amanieu
Copy link
Member

Amanieu commented Apr 14, 2021

I feel that insert is the least confusing way to describe the operation that is happening here. We're inserting a value and replacing the old one (just like HashMap::insert). Returning a reference to the inserted element makes sense: that's the only reason you'd use this method instead of just doing foo = Some(bar).

@et342
Copy link
Contributor

et342 commented Apr 14, 2021

I'm not really against it. insert is stretchable enough and kind of makes sense − but also not exactly?

Returning a reference to the inserted element makes sense: that's the only reason you'd use this method instead of just doing foo = Some(bar).

Yes, that's exactly the point. The reference you get is the primary reason to call this method, not the fact that the value gets replaced − that's secondary, because if it's a replacement action that you want, then just perform an assignment. So maybe this should be reflected in the name, like with the other get_ methods of Option.

HashMap::insert

I don't think HashMap::insert is particularly an example in favor of insert here, because in case of HashMap, new value is pushed into the slot, and the old one is pushed out. And it makes sense if we imagine it like we're inserting something somewhere, and what was in there already − falls out from the other side. But in case of insert here − we're not getting the old thing back, it just disappears into nowhere.

Perhaps, get_inserted? We're geting a reference to an unconditionally inserted value.

@m-ou-se
Copy link
Member

m-ou-se commented Apr 15, 2021

We discussed this yesterday in the library team meeting, and we agreed to continue with our original decision: insert.

bors added a commit to rust-lang-ci/rust that referenced this issue Apr 15, 2021
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Apr 15, 2021
@m-ou-se m-ou-se closed this as completed Apr 16, 2021
@tmandry
Copy link
Member

tmandry commented Apr 22, 2021

Semantics aside, I find it confusing that Option::insert returns a reference to the new value, while HashMap::insert returns the old value. That inconsistency makes it harder to remember what both methods do.

(Edit: Sorry for commenting on this after FCP, but since no one brought up this specific point I feel like it's still worth commenting. I'm thinking that this issue didn't get as much attention because there were a lot of FCPs (18!) in the TWiR edition where it was listed.)

@m-ou-se
Copy link
Member

m-ou-se commented Apr 24, 2021

There's already Option::replace that returns the old value. I'd argue that HashMap::insert should've been called HashMap::replace (and HashMap::try_insert just HashMap::insert), but that ship has sailed. :(

@WaffleLapkin
Copy link
Member

I wonder if it would be useful to have a method on Option which would both return the old value and a reference to the new one. I.e.

fn insert_replace(self, new: T) -> (Option<T>, &mut T) {...}

🤔

@Amanieu
Copy link
Member

Amanieu commented Apr 24, 2021

This seems overly complicated when you can just do:

let old = option.take();
let new_ref = option.insert(new);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-result-option Area: Result and Option combinators C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests