-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Method for bool
to Result<(),E>
conversion
#3777
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
Comments
You're unlikely to get a lot of feedback here. If you just want to discuss it generally, you'd be better off opening a thread on Zulip or IRLO. If you want to make a proposal, you'd create a new issue and select "API Change Proposal" (i.e. ACP) here: |
Thanks a lot for the guidance and sorry for the unnecessary noise in the wrong place! |
For prior art, see macros like https://docs.rs/snafu/latest/snafu/macro.ensure.html |
For the record. The following crate has syntactic sugar. use then::Some;
assert_eq!(false.some(0), None);
assert_eq!(true.some_with(Default::default), Some(0)); |
https://docs.rs/boolinator/latest/boolinator/trait.Boolinator.html is another crate serving the same purpose |
Prior work
#2606 and #2757 was a really useful addition for writing combinator-style rust code. Thanks a lot for that!
The Motivation
After using the new methods for quiet some time, there have been situations here and there that still feel a little wonky, especially dealing with
bool
toResult
situations.Currently, we have the following options:
if
sthen
/then_some
While the latter works fine for most of the situations, I kind of always refrain from using it if I just want to get a
Result<(), E>
since it looks unnecessarily verbose and arcane:It's not only verbose but also reads very poorly in real world scenarios like early exit conditions and run condition checks:
The Proposal
I think it could be nice to support some method like
or_err
as follows:So effectively just handling the
Result<T, E>
whereT = ()
. This would "improve" the situation above to becomeThis would also mean we call only one method (
or_err
) instead of two methods (then_some
,ok_or
). I guess this has negligible impact though since the compiler most likely optimizes this to the same code (?) (not experimentally tested!).Last but not least, there would probably also be the need for a closure-taking variant
or_else_err
or something alike.Fair arguments against the change
bool
doesn't have to take place again. The only real question would be: How far are we going with things like this?T != ()
, then the following would be equivalentif
s, although I can't mention any off the top of my head. In the end some performance considerations could be added to the docs or clippy Lint's could be established for thatFeedback
Maybe this is just me being weird with my rust in the end. In that case, feel free to close this issue/RFC if there's no clear indication other people feel a need for this feature. Thanks for giving it a read anyways!
The text was updated successfully, but these errors were encountered: