how.rs/src/into.rs

63 lines
1.2 KiB
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)]
#[track_caller]
fn into_result_how(self) -> Result<Self::T, How> {
#[inline(never)]
#[track_caller]
fn into<E: std::error::Error>(e: E) -> How {
How::new(e.to_string())
}
match self {
Ok(t) => Ok(t),
Err(e) => Err(into(e))
}
}
}
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)]
#[track_caller]
fn into_result_how(self) -> Result<Self::T, How> {
#[inline(never)]
#[track_caller]
fn into() -> How {
How::new("Option::None")
}
match self {
Some(t) => Ok(t),
None => Err(into())
}
}
}