-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 Result::into_ok
#61695
Comments
How is this different from the never ( |
@Lonami I'm not sure I understand the question in relation to the proposal?
I'm proposing an unwrapping |
Oh, sorry, I should've checked first, I was unaware of that ^^ |
No problem! Should've maybe added some background information, |
Looks like this might be rust-lang/rfcs#1723? |
@scottmcm Ah, yes. I didn't go back to 2016, and didn't search in the RFCs :) |
Hijacking this as a tracking issue for rust-lang/rfcs#2799 |
Result::into_ok
Add method Result::into_ok Implementation of rust-lang/rfcs#2799 Tracking issue #61695
Add method Result::into_ok Implementation of rust-lang/rfcs#2799 Tracking issue rust-lang#61695
Is there anything else required to begin the process of stabilizing the feature? |
|
@mexus You can always make a stabilization PR and see what libs thinks 🙃 Unfortunately |
I published a PR for adding the corresponding I think the The standard library simply has the return type |
Is there anything blocking this being stabilized? This can already be used without If this feature is stabilized. The following would work on stable: fn infallible_op() -> Result<String, bad::Never> {
Ok("hello".into())
}
fn main() {
let x: String = infallible_op().into_ok();
println!("Great results: {}", x);
} |
Add Result::into_err where the Ok variant is the never type Equivalent of rust-lang#66045 but for the inverse situation where `T: Into<!>` rather than `E: Into<!>`. I'm using the same feature gate name. I can't see why one of these methods would be OK to stabilize but not the other. Tracking issue: rust-lang#61695
I posted PR #83493 in order to implement |
What about the more general trait bound, impl<T, E: Into<T>> Result<T, E> {
pub fn into_ok(self) -> T {
match self {
Ok(x) => x,
Err(e) => e.into(),
}
}
}
/// and a corresponding `into_err`
This could make using stuff like |
This bound does not generalize
I feel that this case is too niche to justify adding a special method on |
Oh, right. Can't implement
Fair enough Edit: what about |
What's needed for stabilization of this? |
My understanding here is that this was blocked on what to do about type Infallible = !; which would resolve that concern. Once #35121 stabilizes, the path to stabilizing this should, I think, be fairly straightforward and uncontroversial. |
Since there will be |
This issue adds the infallible impl<T> Result<T, T> {
fn into_inner(self) -> T {
let (Ok(r) | Err(r)) = self;
r
}
} |
I'm not sure I see the usefulness of It also looks like this was already discussed in #82223, and ultimately rejected because it's unidiomatic; It incentives using |
@superatomic An example where this comes up is when you do a I think rejecting it outright as unidiomatic is too presumptuous. If the Rust standard library has interfaces that return |
Honestly, I don't have an opinion either way. I think it's a reasonable method to add, especially for use with |
Convenience methods for |
With Rust 1.82 we got the omitting empty types in pattern matching feature. Is the fn compute_data_infallible() -> Result<Data, Infallible> {...}
fn run_until_error() -> Result<Infallible, Error> {...}
fn main() {
let Ok(data) = compute_data_infallible();
let Err(error) = run_until_error();
} This does not allow using the computation involving the At the very least, I think the documentation for |
I would like to propose adding a
unwrap_infallible
associated function tocore::result::Result
. The purpose is to convertResult<T, core::convert::Infallible>
to aT
, as the error case is impossible.The implementation would basically be:
An example use-case I have is a wrapper type with generic value verification, like
Handle<T:Verify>
. Some verifications can fail, but some can not. When verification is infallible, aResult<Handle<T>, Infallible>
will be returned.Since some can fail, the
Handle
type implementsTryFrom
and notFrom
. Because of the blanket implementation ofTryFrom
for all types implementingFrom
, I can't additionally add aFrom
conversion for the infallible cases. This blanket implementation makes sense, as it allows an API to take aT: TryFrom
and handle all possible conversions, even infallible ones.But for the API consumer it would be beneficial to be able to go from an infallible
Result<T, Infallible>
to a plainT
without having to manually match or useexpect
. The latter is shorter and chainable, but has the disadvantage that it will still compile when the types are changed and the conversion is no longer infallible.It might be that there is a different solution to infallible conversions via
TryFrom
in the future, for example via specialization. I believe that having anunwrap_infallible
would still be beneficial in many other cases where generically fallible actions can be infallible in certain situations. One example is when working with a library that is based on fallible operations with a user supplied error type, but where the specific implementation from the user is infallible.I'd be happy to work on a PR for this if it's acceptable to add, though I might require some guidance with regards to stability attributes, feature flags and so on. It's a bit hard to find information on that, and experimentation is costly as it takes me a while to complete a
./x.py test src/libcore
:)The text was updated successfully, but these errors were encountered: