rust-lang / libs-team

The home of the library team
Apache License 2.0
123 stars 19 forks source link

make FromResidual #[fundamental] #360

Open CodePeeker opened 6 months ago

CodePeeker commented 6 months ago

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):

Second, if there's a concrete solution: