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

add Result::{value, into_value} #79315

Closed
wants to merge 1 commit into from
Closed

Conversation

llogiq
Copy link
Contributor

@llogiq llogiq commented Nov 22, 2020

Recently using binary_search, I found that I had to manually match the Result to get the value of the insertion point (and in my use case I didn't need to care about whether it was Ok or Err). So I thought this method was missing from Result.

The implementation might be possible to optimize further, but this is a safe initial version.

@rust-highfive
Copy link
Collaborator

r? @sfackler

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 22, 2020
@the8472
Copy link
Member

the8472 commented Nov 22, 2020

The question is whether the name value is more valuable (heh) for this use-case or to extract it from Result<T, !> at some point in the future.

@llogiq
Copy link
Contributor Author

llogiq commented Nov 22, 2020

Those use cases don't need to clash: We could implement value for Result<T, !>, too (at least I think so). We already have into_ok for Result<T, impl Into<!>>, though, which has a different meaning than throwing away the discriminant.

@llogiq llogiq force-pushed the result-value branch 3 times, most recently from c537aee to b5e8289 Compare November 24, 2020 21:13
@m-ou-se
Copy link
Member

m-ou-se commented Nov 27, 2020

Maybe there's a name that's more descriptive of what it does? value could mean a lot of different things. I wasn't able to guess from the PR title what these new functions would do. (Maybe ok_or_err() or into_either() or something in that direction?)

Do you have any examples where this would be useful other than binary_search? If that's the only motivating example, I'm not sure whether I'd consider this a problem with Result or with binary_search. If you're interested in the insertion point and not in finding a specific item, a function like binary_search that doesn't use Ord but just bool (a truly binary search) would fit the use case better, although Rust doesn't have that (yet?). (See also some thoughts here: #74024 (comment))

@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 Nov 27, 2020
@camsteffen
Copy link
Contributor

There's let Ok(n) | Err(n) = result but it's feature-gated (or_patterns).

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 18, 2020
@m-ou-se
Copy link
Member

m-ou-se commented Dec 29, 2020

any examples where this would be useful other than binary_search

This could be another use case: #80486

@crlf0710
Copy link
Member

crlf0710 commented Jan 15, 2021

Triage: What's next steps here?

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. labels Feb 1, 2021
@Dylan-DPC-zz
Copy link

r? @m-ou-se

@rust-highfive rust-highfive assigned m-ou-se and unassigned sfackler Feb 17, 2021
@thomcc
Copy link
Member

thomcc commented Feb 17, 2021

Ah, it looks like #80572 was a duplicate of this.

@bors
Copy link
Contributor

bors commented Feb 18, 2021

☔ The latest upstream changes (presumably #82241) made this pull request unmergeable. Please resolve the merge conflicts.

@m-ou-se
Copy link
Member

m-ou-se commented Mar 3, 2021

Thanks again for your PR. I'm closing this, as #80572 was merged, which adds the same function (although with a different name).

The reference version was not added by that PR, but that option was added as an open question to the tracking issue. Feel free to add your comments there.

@m-ou-se m-ou-se closed this Mar 3, 2021
@llogiq llogiq deleted the result-value branch March 3, 2021 19:01
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 S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.