1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
use core::convert::Infallible;
/// 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
///
/// ```
/// # use nvim_oxi_luajit::IntoResult;
/// fn double<F: Fn() -> R, R: IntoResult<usize>>(
/// f: F,
/// ) -> Result<usize, R::Error> {
/// f().into_result().map(|x| x * 2)
/// }
///
/// # fn main() {
/// // `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));
/// # }
/// ```
pub trait IntoResult<T> {
/// The error type in the returned `Result`.
type Error;
/// Converts the value into a `Result`.
fn into_result(self) -> Result<T, Self::Error>;
}
impl<T> IntoResult<T> for T {
type Error = Infallible;
#[inline]
fn into_result(self) -> Result<T, Self::Error> {
Ok(self)
}
}
impl<T, E> IntoResult<T> for Result<T, E> {
type Error = E;
#[inline]
fn into_result(self) -> Result<T, Self::Error> {
self
}
}