DeriveFailAny
This commit is contained in:
parent
0c7f29060e
commit
f22509c199
@ -194,3 +194,89 @@ impl<T: Monad, O: DeriveMonad> Monad for OverloadClass<T, O> {
|
||||
T::join(ffa)
|
||||
}
|
||||
}
|
||||
|
||||
struct DeriveFail<Ex>(Ex);
|
||||
|
||||
impl<Ex> DeriveMonad for DeriveFail<Ex> {}
|
||||
|
||||
impl<E, Ex, T: MonadFail<Result<E, Ex>>> Fail<E> for OverloadClass<T, DeriveFail<Ex>> {
|
||||
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
|
||||
where
|
||||
Self: 'a,
|
||||
E: 'a,
|
||||
{
|
||||
T::fail(Ok(e))
|
||||
}
|
||||
}
|
||||
|
||||
struct DeriveFailAny<Ex, Fallible>(Ex, Fallible);
|
||||
|
||||
impl<Ex, Fallible: MonadFailAny> MonadFailAny for DeriveFailAny<Ex, Fallible> {
|
||||
type W<E> = OverloadClass<Fallible::W<Result<E, Ex>>, DeriveFail<Ex>>;
|
||||
|
||||
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
|
||||
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
|
||||
f: impl 'a + FnOnce(E0) -> E1,
|
||||
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a,
|
||||
{
|
||||
Fallible::map_err(wa, |err| err.map(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,
|
||||
{
|
||||
Fallible::bind_err(wa, |err| match err {
|
||||
Ok(e0) => f(e0),
|
||||
Err(ex) => Fallible::fail(Err(ex)),
|
||||
})
|
||||
}
|
||||
|
||||
fn bind<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>(
|
||||
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
|
||||
f: impl 'a + FnOnce(Result<A, E0>) -> <Self::W<E1> as WeakFunctor>::F<'a, B>,
|
||||
) -> <Self::W<E1> as WeakFunctor>::F<'a, B>
|
||||
where
|
||||
Self: 'a,
|
||||
{
|
||||
Fallible::bind(wa, |result| match result {
|
||||
Ok(a) => f(Ok(a)),
|
||||
Err(Ok(e0)) => f(Err(e0)),
|
||||
Err(Err(ex)) => Fallible::fail(Err(ex)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ex, Fallible: MonadFailAny> MonadFailOver<Fallible::W<Ex>> for DeriveFailAny<Ex, Fallible> {
|
||||
fn unstuff<'a, A: 'a, E: 'a>(
|
||||
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
|
||||
) -> <Fallible::W<Ex> as WeakFunctor>::F<'a, Result<A, E>>
|
||||
where
|
||||
Self: 'a,
|
||||
Fallible::W<Ex>: 'a,
|
||||
{
|
||||
Fallible::bind_err(<Self::W<E> as Functor>::fmap(Ok, wa), |err| match err {
|
||||
Ok(e) => Fallible::pure(Err(e)),
|
||||
Err(ex) => Fallible::fail(ex),
|
||||
})
|
||||
}
|
||||
|
||||
fn stuff<'a, A: 'a, E: 'a>(
|
||||
fa: <Fallible::W<Ex> as WeakFunctor>::F<'a, Result<A, E>>,
|
||||
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
|
||||
where
|
||||
Self: 'a,
|
||||
Fallible::W<Ex>: 'a,
|
||||
{
|
||||
Fallible::bind(fa, |result| match result {
|
||||
Ok(Ok(a)) => Fallible::pure(a),
|
||||
Ok(Err(e)) => Fallible::fail(Ok(e)),
|
||||
Err(ex) => Fallible::fail(Err(ex)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user