Trait IntoResult

Source
pub trait IntoResult<T> {
    type Error;

    // Required method
    fn into_result(self) -> Result<T, Self::Error>;
}
Expand description

A trait for types that can be converted into a Result.

This trait can be used to be generic over functions that can return either a T or a Result<T, E>.

§Examples

fn double<F: Fn() -> R, R: IntoResult<usize>>(
    f: F,
) -> Result<usize, R::Error> {
    f().into_result().map(|x| x * 2)
}

// `double` takes a closure whose return type is generic over `IntoResult`,
// so we don't have to return `Ok(21)`.
assert_eq!(double(|| 21), Ok(42));

Required Associated Types§

Source

type Error

The error type in the returned Result.

Required Methods§

Source

fn into_result(self) -> Result<T, Self::Error>

Converts the value into a Result.

Implementations on Foreign Types§

Source§

impl<T, E> IntoResult<T> for Result<T, E>

Source§

type Error = E

Source§

fn into_result(self) -> Result<T, <Result<T, E> as IntoResult<T>>::Error>

Implementors§