bind_err/rotate_out
This commit is contained in:
parent
9890684dcb
commit
bd361833b5
19
src/func.rs
19
src/func.rs
@ -247,6 +247,25 @@ pub trait MonadFailAny {
|
||||
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a;
|
||||
|
||||
fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>(
|
||||
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
|
||||
f: impl 'a + FnOnce(E0) -> <Self::W<E1> as WeakFunctor>::F<'a, A>,
|
||||
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a;
|
||||
|
||||
fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>(
|
||||
wa: <Self::W<E0> as WeakFunctor>::F<'a, Result<A, E1>>,
|
||||
) -> <Self::W<Result<E1, E0>> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a,
|
||||
{
|
||||
<Self::W<Result<E1, E0>> as Monad>::bind(Self::map_err(wa, Err), |fa| match fa {
|
||||
Ok(a) => <Self::W<Result<E1, E0>> as Pure>::pure(a),
|
||||
Err(e) => <Self::W<Result<E1, E0>> as MonadFail<Result<E1, E0>>>::fail(Ok(e)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a (collection of) [Monad]\(s),
|
||||
|
@ -161,6 +161,19 @@ impl<E> MonadFail<E> for ResultClass<E> {
|
||||
|
||||
pub struct ResultFailAny;
|
||||
|
||||
trait ResultExt<'a, A: 'a, E0: 'a>: 'a {
|
||||
fn bind_err<E1: 'a>(self, f: impl 'a + FnOnce(E0) -> Result<A, E1>) -> Result<A, E1>;
|
||||
}
|
||||
|
||||
impl<'a, A: 'a, E0: 'a> ResultExt<'a, A, E0> for Result<A, E0> {
|
||||
fn bind_err<E1: 'a>(self, f: impl 'a + FnOnce(E0) -> Result<A, E1>) -> Result<A, E1> {
|
||||
match self {
|
||||
Ok(a) => Ok(a),
|
||||
Err(e) => f(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MonadFailAny for ResultFailAny {
|
||||
type W<E> = ResultClass<E>;
|
||||
|
||||
@ -173,6 +186,29 @@ impl MonadFailAny for ResultFailAny {
|
||||
{
|
||||
wa.map_err(f)
|
||||
}
|
||||
|
||||
fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>(
|
||||
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
|
||||
f: impl 'a + FnOnce(E0) -> <Self::W<E1> as WeakFunctor>::F<'a, A>,
|
||||
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a,
|
||||
{
|
||||
wa.bind_err(f)
|
||||
}
|
||||
|
||||
fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>(
|
||||
wa: <Self::W<E0> as WeakFunctor>::F<'a, Result<A, E1>>,
|
||||
) -> <Self::W<Result<E1, E0>> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a,
|
||||
{
|
||||
match wa {
|
||||
Ok(Ok(a)) => Ok(a),
|
||||
Ok(Err(e)) => Err(Ok(e)),
|
||||
Err(e) => Err(Err(e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MonadFailOver<classes::solo::SoloClass> for ResultFailAny {
|
||||
@ -211,6 +247,28 @@ impl<T: Monad> MonadFailAny for ResultFailOver<T> {
|
||||
{
|
||||
T::fmap(|a| a.map_err(f), wa)
|
||||
}
|
||||
|
||||
fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>(
|
||||
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
|
||||
f: impl 'a + FnOnce(E0) -> <Self::W<E1> as WeakFunctor>::F<'a, A>,
|
||||
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a,
|
||||
{
|
||||
T::bind(wa, |a| match a {
|
||||
Ok(a) => T::pure(Ok(a)),
|
||||
Err(e) => f(e),
|
||||
})
|
||||
}
|
||||
|
||||
fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>(
|
||||
wa: <Self::W<E0> as WeakFunctor>::F<'a, Result<A, E1>>,
|
||||
) -> <Self::W<Result<E1, E0>> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a,
|
||||
{
|
||||
T::fmap(<ResultFailAny as MonadFailAny>::rotate_out, wa)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Monad> MonadFailOver<T> for ResultFailOver<T> {
|
||||
|
Loading…
Reference in New Issue
Block a user