Compare commits

...

2 Commits

Author SHA1 Message Date
9a8472e8fd merge MonadFailOver into FutureFailAny 2023-05-20 13:42:58 +00:00
f22509c199 DeriveFailAny 2023-05-20 13:15:04 +00:00
5 changed files with 112 additions and 38 deletions

View File

@ -42,7 +42,7 @@ pub trait Context {
/// Type to allow improved support for result evaluation.
/// This is important for async applications stopping early.
type Fallible: MonadFailOver<Self::T>;
type Fallible: MonadFailAny<T = Self::T>;
/// See [`Diagnostic`].
type D: Diagnostic<Self::T>;

View File

@ -278,6 +278,9 @@ pub trait MonadFailAny {
/// [`MonadFail`] for a specific error type.
type W<E>: MonadFail<E>;
/// Associated infallible [`Monad`].
type T: Monad;
/// Equivalent of [`Result::map_err`].
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
wa: <Self::W<E0> as WeakFunctor>::F<'a, A>,
@ -311,6 +314,17 @@ pub trait MonadFailAny {
Err(e) => Self::fail(Ok(e)),
})
}
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a;
fn stuff<'a, A: 'a, E: 'a>(
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a;
}
pub trait MonadFailAnyExt: MonadFailAny {
@ -330,25 +344,6 @@ pub trait MonadFailAnyExt: MonadFailAny {
impl<Fallible: ?Sized + MonadFailAny> MonadFailAnyExt for Fallible {}
/// Represents a (collection of) [Monad]\(s),
/// wrapped values of which are interchangeable with another [Monad]'s
/// wrapped [Result]s.
pub trait MonadFailOver<T: Monad>: MonadFailAny {
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a,
T: 'a;
fn stuff<'a, A: 'a, E: 'a>(
fa: <T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a,
T: 'a;
}
pub trait SharedFunctor: WeakFunctor {
type Shared<'a, A: 'a + Clone>: 'a + Clone
where

View File

@ -194,3 +194,88 @@ 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>>;
type T = Fallible::W<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)),
})
}
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)),
})
}
}

View File

@ -215,6 +215,8 @@ impl<'a, A: 'a, E0: 'a> ResultExt<'a, A, E0> for Result<A, E0> {
impl MonadFailAny for ResultFailAny {
type W<E> = ResultClass<E>;
type T = classes::solo::SoloClass;
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,
@ -257,25 +259,21 @@ impl MonadFailAny for ResultFailAny {
Err(e) => Err(Err(e)),
}
}
}
impl MonadFailOver<classes::solo::SoloClass> for ResultFailAny {
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <classes::solo::SoloClass as WeakFunctor>::F<'a, Result<A, E>>
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a,
classes::solo::SoloClass: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(
fa: <classes::solo::SoloClass as WeakFunctor>::F<'a, Result<A, E>>,
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a,
classes::solo::SoloClass: 'a,
{
fa
}
@ -286,6 +284,8 @@ pub struct ResultFailOver<T: Monad>(T);
impl<T: Monad> MonadFailAny for ResultFailOver<T> {
type W<E> = super::composition::CompositionClass<T, ResultClass<E>>;
type T = T;
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,
@ -327,25 +327,21 @@ impl<T: Monad> MonadFailAny for ResultFailOver<T> {
{
T::fmap(<ResultFailAny as MonadFailAny>::rotate_out, wa)
}
}
impl<T: Monad> MonadFailOver<T> for ResultFailOver<T> {
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <T as WeakFunctor>::F<'a, Result<A, E>>
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a,
T: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(
fa: <T as WeakFunctor>::F<'a, Result<A, E>>,
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a,
T: 'a,
{
fa
}

View File

@ -197,6 +197,8 @@ struct FutureFailAny;
impl MonadFailAny for FutureFailAny {
type W<E> = TryFutureClass<E>;
type T = classes::future::FutureClass;
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,
@ -246,25 +248,21 @@ impl MonadFailAny for FutureFailAny {
}
})
}
}
impl MonadFailOver<classes::future::FutureClass> for FutureFailAny {
fn unstuff<'a, A: 'a, E: 'a>(
wa: <Self::W<E> as WeakFunctor>::F<'a, A>,
) -> <classes::future::FutureClass as WeakFunctor>::F<'a, Result<A, E>>
) -> <Self::T as WeakFunctor>::F<'a, Result<A, E>>
where
Self: 'a,
classes::future::FutureClass: 'a,
{
wa
}
fn stuff<'a, A: 'a, E: 'a>(
fa: <classes::future::FutureClass as WeakFunctor>::F<'a, Result<A, E>>,
fa: <Self::T as WeakFunctor>::F<'a, Result<A, E>>,
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
where
Self: 'a,
classes::future::FutureClass: 'a,
{
fa
}