353 lines
8.4 KiB
Rust
353 lines
8.4 KiB
Rust
//! Implementation of [`MonadFail<E>`] for [`Result<A, E>`].
|
|
//!
|
|
//! If any of the input values are [`Err`], you can expect the output to be [`Err`] as well.
|
|
//! That includes
|
|
//! [`ResultClass::replace`] and [`ResultClass::discard_first`]/[`ResultClass::discard_second`],
|
|
//! even if the value of the option would be ignored.
|
|
//!
|
|
//! For [`Option<A>`] alternative see [`super::option`]
|
|
|
|
use crate::func::*;
|
|
|
|
#[derive(SharedFunctor, CovariantFunctor)]
|
|
pub struct ResultClass<E>(E);
|
|
|
|
impl<E> WeakFunctor for ResultClass<E> {
|
|
type F<'a, A: 'a> = Result<A, E> where Self: 'a;
|
|
}
|
|
|
|
impl<E> Functor for ResultClass<E> {
|
|
fn fmap<'a, A: 'a, B: 'a>(f: impl 'a + FnOnce(A) -> B, fa: Self::F<'a, A>) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa.map(f)
|
|
}
|
|
|
|
fn replace<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, b: B) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa?;
|
|
Self::pure(b)
|
|
}
|
|
|
|
fn void<'a, A: 'a>(fa: Self::F<'a, A>) -> Self::F<'a, ()>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa?;
|
|
Self::pure(())
|
|
}
|
|
}
|
|
|
|
impl<E> Pure for ResultClass<E> {
|
|
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Ok(a)
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeSeq for ResultClass<E> {
|
|
fn seq<'a, A: 'a, B: 'a>(
|
|
ff: Self::F<'a, impl 'a + FnOnce(A) -> B>,
|
|
fa: Self::F<'a, A>,
|
|
) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Self::pure(ff?(fa?))
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeLA2 for ResultClass<E> {
|
|
fn la2<'a, A: 'a, B: 'a, C: 'a>(
|
|
f: impl 'a + FnOnce(A, B) -> C,
|
|
fa: Self::F<'a, A>,
|
|
fb: Self::F<'a, B>,
|
|
) -> Self::F<'a, C>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Self::pure(f(fa?, fb?))
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeTuple for ResultClass<E> {
|
|
fn tuple<'a, A: 'a, B: 'a>((fa, fb): (Self::F<'a, A>, Self::F<'a, B>)) -> Self::F<'a, (A, B)>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Self::pure((fa?, fb?))
|
|
}
|
|
}
|
|
|
|
impl<E> Applicative for ResultClass<E> {
|
|
fn discard_first<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa?;
|
|
fb
|
|
}
|
|
|
|
fn discard_second<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fb?;
|
|
fa
|
|
}
|
|
}
|
|
|
|
impl<E> Monad for ResultClass<E> {
|
|
fn bind<'a, A: 'a, B: 'a>(
|
|
fa: Self::F<'a, A>,
|
|
f: impl 'a + FnOnce(A) -> Self::F<'a, B>,
|
|
) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
f(fa?)
|
|
}
|
|
|
|
fn iterate_mut<'a, A: 'a, B: 'a>(
|
|
mut a: A,
|
|
mut f: impl 'a + FnMut(A) -> Self::F<'a, ControlFlow<B, A>>,
|
|
) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
loop {
|
|
match f(a)? {
|
|
ControlFlow::Continue(next_a) => a = next_a,
|
|
ControlFlow::Break(b) => return Self::pure(b),
|
|
};
|
|
}
|
|
}
|
|
|
|
fn iterate_argument<'a, A: 'a, B: 'a>(
|
|
mut a: A,
|
|
mut f: impl AIterative<'a, T = Self, A = A, B = B>,
|
|
) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
loop {
|
|
match f.next(a)? {
|
|
ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f),
|
|
ControlFlow::Break(b) => return Self::pure(b),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
loop {
|
|
match f.next()? {
|
|
ControlFlow::Continue(next_f) => f = next_f,
|
|
ControlFlow::Break(b) => return Self::pure(b),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
ffa?
|
|
}
|
|
}
|
|
|
|
impl<E> LocalFunctor for ResultClass<E> {
|
|
fn unstuff<'a, A: 'a, B: 'a>(
|
|
state: Self::F<'a, ControlFlow<B, A>>,
|
|
) -> ControlFlow<Self::F<'a, B>, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
match state {
|
|
Ok(ControlFlow::Continue(a)) => ControlFlow::Continue(a),
|
|
Ok(ControlFlow::Break(b)) => ControlFlow::Break(Ok(b)),
|
|
Err(e) => ControlFlow::Break(Err(e)),
|
|
}
|
|
}
|
|
|
|
fn stuff<'a, A: 'a, T: 'a + Pure>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
match fa {
|
|
Ok(ua) => T::fmap(Ok, ua),
|
|
Err(e) => T::pure(Err(e)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<E> Fail<E> for ResultClass<E> {
|
|
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Err(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>;
|
|
|
|
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,
|
|
{
|
|
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 bind<'a, A: '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, A>,
|
|
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
f(wa)
|
|
}
|
|
|
|
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 {
|
|
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>>
|
|
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>>,
|
|
) -> <Self::W<E> as WeakFunctor>::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
classes::solo::SoloClass: 'a,
|
|
{
|
|
fa
|
|
}
|
|
}
|
|
|
|
pub struct ResultFailOver<T: Monad>(T);
|
|
|
|
impl<T: Monad> MonadFailAny for ResultFailOver<T> {
|
|
type W<E> = super::composition::CompositionClass<T, ResultClass<E>>;
|
|
|
|
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,
|
|
{
|
|
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 bind<'a, A: '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, A>,
|
|
) -> <Self::W<E1> as WeakFunctor>::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
T::bind(wa, 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,
|
|
{
|
|
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>>
|
|
where
|
|
Self: 'a,
|
|
T: 'a,
|
|
{
|
|
wa
|
|
}
|
|
|
|
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,
|
|
{
|
|
fa
|
|
}
|
|
}
|