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§
Required Methods§
Sourcefn into_result(self) -> Result<T, Self::Error>
fn into_result(self) -> Result<T, Self::Error>
Converts the value into a Result
.