make FromResidual #[fundamental]
Proposal
Problem statement
Motivating examples or use cases
#![feature(try_trait_v2)]
use std::{convert, ops::FromResidual};
// Already support
fn try_trait_v2_with_result<T: Default, E>() -> Result<T, E> {
let val = Ok(T::default())?;
Ok(T::default())
}
// Already support
fn try_trait_v2_with_option<T: Default>() -> Option<T> {
let val = Some(T::default())?;
Some(T::default())
}
// Now we want
fn we_want_try_trait_v2_with_result_and_option<T: Default, E>() -> Result<T, E> {
let val = Ok(T::default())?;
let val = Some(T::default())?;
Ok(T::default())
}
// ERROR: only traits defined in the current crate can be implemented for types defined outside of the crate
impl<T, E> FromResidual<Option<convert::Infallible>> for Result<T, E> {
fn from_residual(residual: Option<convert::Infallible>) -> Self {
todo!()
}
}
Solution sketch
Alternatives
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Paging @scottmcm
I don't understand the motivating example. As there is not an E: Default bound, what does (None::<T>)? even mean.
My lang intuition says that we shouldn't make any more traits #[fundamental] -- I'd argue that anything wanting that should instead be done via better coherence features instead. (Like integrating explicit negative impls into coherence, say.)
As for FromResidual specifically, there's a good question about whether it should be implemented on the residual type or on the try type, but I don't think that's a reason to make it #[fundamental].
I don't understand the motivating example. As there is not an
E: Defaultbound, what does(None::<T>)?even mean.
My movitvation is let try_trait_v2 feature can use with both Option<T> and Result<T> in the same funtion block. For example:
fn we_want_try_trait_v2_with_result_and_option<T: Default, E>() -> Result<T, E> {
let val = Ok(T::default())?; // ? with Result<T> already supported
let val = Some(T::default())?; // ? with Option<T> currently invaild, because the function return type is Result<T,E>
Ok(T::default())
}
So according to the try_trait_v2 rfc, if we can impl FromResidual<Option<convert::Infallible>> for Result<T, E>, we will able to use ? with both Result<T,E> and Option<T> in the function which return Result<T,E>.
In this case, ? with Option<T> return a Error, not a None::<T>, because try_trait_v2 feature implicit invoke from_residual
My lang intuition says that we shouldn't make any more traits
#[fundamental]-- I'd argue that anything wanting that should instead be done via better coherence features instead. (Like integrating explicit negative impls into coherence, say.)As for
FromResidualspecifically, there's a good question about whether it should be implemented on the residual type or on the try type, but I don't think that's a reason to make it#[fundamental].
Yes, #[fundamental] trraits should be the last method if we can achive our purpose in other way. But the rust orphan rule don't allow us impl FromResidual<Option<T>> for Result<T,E> out of std crate. Is it worth to add a bridge trait to reach the goal what we want? I can try to send a PR if worthy.
It's my fault, Just found document here. https://rust-lang.github.io/rfcs/3058-try-trait-v2.html#compatibility-with-accidental-interconversions-if-needed
Closing this based on @scottmcm's recommendation as author of Try and FromResidual, and on the basis that we're pretty sure we don't want the implementer of T to be the one writing implementations of FromResidual for wrappers of T.
For the specific case of Option, the general pattern to use is .ok_or or .ok_or_else, or from the ecosystem, methods like .ok_or_eyre from eyre.