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

API: Add err_or and err_or_else to Option #80190

Closed
LunarLambda opened this issue Dec 19, 2020 · 4 comments
Closed

API: Add err_or and err_or_else to Option #80190

LunarLambda opened this issue Dec 19, 2020 · 4 comments
Labels
A-error-handling Area: Error handling A-result-option Area: Result and Option combinators C-feature-request Category: A feature request, i.e: not implemented / a PR. PG-error-handling Project group: Error handling (https://github.com/rust-lang/project-error-handling) T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@LunarLambda
Copy link

sometimes, particularly when interfacing with C libraries which provide their own errno-like API (GetLastError, SDL_GetError, etc), Rust bindings may opt to return Option<ErrorType> to model the absence of an error.

This could then be 'upgraded' to a Result<T, E>, in a manner similar to ok_or:

fn example() -> Result<(), SomeError> {
    get_some_error().err_or(())
}

pros

cons

  • name looks like error, might cause confusion
  • not extremely common use case? (haven't checked discussion on unwrap_none yet)
@jonas-schievink jonas-schievink added A-error-handling Area: Error handling C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 19, 2020
@camelid camelid added the A-result-option Area: Result and Option combinators label Dec 21, 2020
@joshtriplett
Copy link
Member

What's the advantage of returning Option<ErrorType> rather than Result<(), ErrorType>? Semantically, the latter feels more accurate. And both are capable of handling niches in the same way.

@yaahc yaahc added the I-needs-decision Issue: In need of a decision. label Aug 4, 2021
@m-ou-se m-ou-se removed the I-needs-decision Issue: In need of a decision. label Aug 11, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Aug 11, 2021

We discussed this briefly in the library api team meeting last week, and we'd like to see some concrete use cases. Do you have examples from some Rust crates where this Option to Result::Err conversion would be used?

@yaahc yaahc added the PG-error-handling Project group: Error handling (https://github.com/rust-lang/project-error-handling) label Sep 27, 2021
@yaahc
Copy link
Member

yaahc commented Sep 27, 2021

Closing this for now due to lack of responses with concrete use cases. Feel free to reopen in the future if you have a use case where you feel this API would be justified.

@yaahc yaahc closed this as completed Sep 27, 2021
@NathanielB123
Copy link

NathanielB123 commented Jan 2, 2023

I think I have a concrete use-case for this. I am using the insert function in HashMap<K, V> which returns Option<V> if there is already an item in the hash map. In my code, and I would argue probably most of the time, an item already being present in the hash map implies some error has occurred, and so I want to return Result<SuccessType, V>. err_or_else would be perfect for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-error-handling Area: Error handling A-result-option Area: Result and Option combinators C-feature-request Category: A feature request, i.e: not implemented / a PR. PG-error-handling Project group: Error handling (https://github.com/rust-lang/project-error-handling) 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

7 participants