how.rs/src/into.rs

53 lines
990 B
Rust

use crate::How;
mod private {
use crate::How;
#[doc(hidden)]
pub trait IntoResultHow: Sized {
type T;
fn into_result_how(self) -> Result<Self::T, How>;
}
}
pub(crate) use private::IntoResultHow;
impl<T, E> IntoResultHow for Result<T, E>
where
E: std::error::Error,
{
type T = T;
#[inline(always)]
fn into_result_how(self) -> Result<Self::T, How> {
#[inline(never)]
fn into<E: std::error::Error>(e: E) -> How {
How::new(e.to_string())
}
self.map_err(into)
}
}
impl<T> IntoResultHow for Result<T, How> {
type T = T;
#[inline(always)]
fn into_result_how(self) -> Result<Self::T, How> {
self
}
}
impl<T> IntoResultHow for Option<T> {
type T = T;
#[inline(always)]
fn into_result_how(self) -> Result<Self::T, How> {
#[inline(never)]
fn into() -> How {
How::new("Option::None")
}
self.ok_or_else(into)
}
}