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: Provide Entry-like API for Option #39288

Closed
shahn opened this issue Jan 25, 2017 · 17 comments
Closed

Tracking issue: Provide Entry-like API for Option #39288

shahn opened this issue Jan 25, 2017 · 17 comments
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@shahn
Copy link
Contributor

shahn commented Jan 25, 2017

Often I have code where Option is used to indicate whether a value has been initialized or not. In these instances I often have code that either wants to use the value directly or initialize it and then use the value. So what I'm looking for is similar to as_ref()/as_mut() except they should return &T/&mut T respectively, and take either a value or a closure.

I think I would call the functions get{,_mut}_or_insert{,with}() marrying the get functions (for example in hashmaps) for getting references with the or_insert Entry like functions.

@Thinkofname
Copy link

Thinkofname commented Jan 25, 2017

Isn't this just as_ref/mut().unwrap_or(val) and as_ref/mut().unwrap_or_else(|| val) ?

Edit: These don't modify the original Option which I guess it what you wanted

@shahn
Copy link
Contributor Author

shahn commented Jan 25, 2017

indeed

@alexcrichton alexcrichton added B-unstable Blocker: Implemented in the nightly compiler and unstable. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Feb 4, 2017
shahn added a commit to shahn/rust that referenced this issue Feb 4, 2017
This implements rust-lang#39288.

Thanks to @steveklabnik and @oli-obk for their review comments :)
frewsxcv added a commit to frewsxcv/rust that referenced this issue Feb 5, 2017
Provide Entry-like API for Option

This implements rust-lang#39288.

I am wondering whether to use std::intrinsics::unreachable!() here. Both seems fine to me (the second match optimizes away in release mode).
@tbu-
Copy link
Contributor

tbu- commented Feb 6, 2017

See also #25149 and #29203. Also, this seems fixed by #39289.

@shahn
Copy link
Contributor Author

shahn commented Feb 6, 2017

AIUI, this is not fixed by #39289, because this issue is referenced as the tracking issue for the implementation and should stay open until it's decided whether this is to be stabilized or backed out.

@tbu-
Copy link
Contributor

tbu- commented Feb 6, 2017

Ah, right. Maybe you could put "Tracking issue" in the title.

@shahn shahn changed the title Provide Entry-like API for Option Tracking issue: Provide Entry-like API for Option Feb 6, 2017
@oberien
Copy link
Contributor

oberien commented May 22, 2017

As this issue is quite old already (over 3 months), would it be possible to stabilize these two functions soon?

@brson
Copy link
Contributor

brson commented May 23, 2017

@rfcbot merge

@brson
Copy link
Contributor

brson commented May 23, 2017

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented May 23, 2017

Team member @brson has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once these reviewers reach consensus, 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.

@alexcrichton
Copy link
Member

To be clear, the APIs proposed here for stabilization are:

impl<T> Option<T> {
    fn get_or_insert(&mut self, v: T) -> &mut T;
    fn get_or_insert_with<F: FnOnce() -> T>(&mut self, f: F) -> &mut T;
}

@rfcbot
Copy link

rfcbot commented Jun 6, 2017

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

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jun 6, 2017
@rfcbot
Copy link

rfcbot commented Jun 16, 2017

The final comment period is now complete.

@dhardy
Copy link
Contributor

dhardy commented Jun 26, 2017

This isn't Entry-like. If it was, it would be possible to do the following:

match opt_value.entry() {
    VacantEntry(entry) => entry.insert(x),
    OccupiedEntry(entry) => { assert_eq!(entry.get(), x); }
}

@SimonSapin
Copy link
Contributor

@dhardy That particular example can be written as:

match opt_value {
    ref mut entry @ None => *entry = Some(x),
    Some(ref entry) => assert_eq!(entry, x),
}

@Mark-Simulacrum Mark-Simulacrum added the C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. label Jul 22, 2017
@bors bors closed this as completed in 64c1b23 Jul 27, 2017
alexcrichton added a commit to alexcrichton/rust that referenced this issue Aug 12, 2017
Stabilized:

* `Option::get_or_insert`
* `Option::get_or_insert_with`

Closes rust-lang#39288
@raphlinus
Copy link
Contributor

To save time for people researching this, the feature landed in stable at Rust 1.20 (source: 64c1b23).

@shepmaster
Copy link
Member

shepmaster commented Nov 1, 2017

@raphlinus I think that commit is wrong :-) The function in question doesn't show up in the 1.20.0 docs but does in the 1.21.0 docs. Nope, never mind; I just forgot how to search, sigh.

GitHub does say that commit is only in the 1.21.0 tag though...

@shepmaster
Copy link
Member

Ah, the 1.20.0 commit was a backport, thus why it's a different tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. 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