#[allow(clippy::missing_errors_doc)]
pub trait ToResult {
#[inline]
fn ok<Err>(self) -> Result<Self, Err>
where
Self: Sized,
{
Ok(self)
}
#[inline]
fn err<Any>(self) -> Result<Any, Self>
where
Self: Sized,
{
Err(self)
}
#[inline]
fn some(self) -> Option<Self>
where
Self: Sized,
{
Some(self)
}
}
impl<T> ToResult for T {}
pub trait ResConv<T, E> {
fn into_option(self) -> Option<T>;
fn into_result(self) -> Result<T, E>;
}
impl<Input, Output, Err> ResConv<Output, Err> for Result<Input, Err>
where
Input: Into<Output>,
{
#[inline]
fn into_option(self) -> Option<Output> {
self.map(Into::into).ok()
}
#[inline]
fn into_result(self) -> Result<Output, Err> {
self.map(Into::into)
}
}
impl<Input, Output, Err: Default> ResConv<Output, Err> for Option<Input>
where
Input: Into<Output>,
{
#[inline]
fn into_option(self) -> Option<Output> {
self.map(Into::into)
}
#[inline]
fn into_result(self) -> Result<Output, Err> {
self.map(Into::into).ok_or_else(Err::default)
}
}