224 lines
6.1 KiB
Rust
224 lines
6.1 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
|
|
//! [`ResultInstance::replace`] and [`ResultInstance::discard_first`]/[`ResultInstance::discard_second`],
|
|
//! even if the value of the option would be ignored.
|
|
//!
|
|
//! For [`Option<A>`] alternative see [`super::option`]
|
|
|
|
use crate::func::*;
|
|
|
|
#[derive(SharedFunctorAny)]
|
|
pub struct ResultInstance<E>(E);
|
|
|
|
impl<E> WeakFunctorAny for ResultInstance<E> {
|
|
type FAny<'a, A: 'a> = Result<A, E> where Self: 'a;
|
|
}
|
|
|
|
impl<'a, E: 'a> Functor<'a> for ResultInstance<E> {
|
|
fn fmap<A: 'a, B: 'a>(f: impl 'a + FnOnce(A) -> B, fa: Self::F<A>) -> Self::F<B> {
|
|
fa.map(f)
|
|
}
|
|
|
|
fn replace<A: 'a, B: 'a>(fa: Self::F<A>, b: B) -> Self::F<B> {
|
|
fa?;
|
|
Self::pure(b)
|
|
}
|
|
|
|
fn void<A: 'a>(fa: Self::F<A>) -> Self::F<()> {
|
|
fa?;
|
|
Self::pure(())
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> Pure<'a> for ResultInstance<E> {
|
|
fn pure<A: 'a>(a: A) -> Self::F<A> {
|
|
Ok(a)
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> ApplicativeSeq<'a> for ResultInstance<E> {
|
|
fn seq<A: 'a, B: 'a>(ff: Self::F<impl 'a + FnOnce(A) -> B>, fa: Self::F<A>) -> Self::F<B> {
|
|
Self::pure(ff?(fa?))
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> ApplicativeLA2<'a> for ResultInstance<E> {
|
|
fn la2<A: 'a, B: 'a, C: 'a>(
|
|
f: impl 'a + FnOnce(A, B) -> C,
|
|
fa: Self::F<A>,
|
|
fb: Self::F<B>,
|
|
) -> Self::F<C> {
|
|
Self::pure(f(fa?, fb?))
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> ApplicativeTuple<'a> for ResultInstance<E> {
|
|
fn tuple<A: 'a, B: 'a>((fa, fb): (Self::F<A>, Self::F<B>)) -> Self::F<(A, B)> {
|
|
Self::pure((fa?, fb?))
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> ApplicativeSelect<'a> for ResultInstance<E> {}
|
|
|
|
impl<'a, E: 'a> Applicative<'a> for ResultInstance<E> {
|
|
fn discard_first<A: 'a, B: 'a>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<B> {
|
|
fa?;
|
|
fb
|
|
}
|
|
|
|
fn discard_second<A: 'a, B: 'a>(fa: Self::F<A>, fb: Self::F<B>) -> Self::F<A> {
|
|
fb?;
|
|
fa
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> Monad<'a> for ResultInstance<E> {
|
|
fn bind<A: 'a, B: 'a>(fa: Self::F<A>, f: impl 'a + FnOnce(A) -> Self::F<B>) -> Self::F<B> {
|
|
f(fa?)
|
|
}
|
|
|
|
fn iterate<B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<B> {
|
|
loop {
|
|
match f.next()? {
|
|
ControlFlow::Continue(next_f) => f = next_f,
|
|
ControlFlow::Break(b) => return Self::pure(b),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn join<A: 'a>(ffa: Self::F<Self::F<A>>) -> Self::F<A> {
|
|
ffa?
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> LocalFunctor<'a> for ResultInstance<E> {
|
|
fn unstuff<A: 'a, B: 'a>(state: Self::F<ControlFlow<B, A>>) -> ControlFlow<Self::F<B>, 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, T: Pure<'a>>(fa: Self::F<T::F<A>>) -> T::F<Self::F<A>> {
|
|
match fa {
|
|
Ok(ua) => T::fmap(Ok, ua),
|
|
Err(e) => T::pure(Err(e)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, E: 'a> Fail<'a, E> for ResultInstance<E> {
|
|
fn fail<A: 'a>(e: E) -> Self::F<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<'a> MonadFailAny<'a> for ResultFailAny {
|
|
type W<E: 'a> = ResultInstance<E>;
|
|
|
|
type T = instances::solo::SoloInstance;
|
|
|
|
fn unstuff<A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T> {
|
|
wa
|
|
}
|
|
|
|
fn stuff<A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self> {
|
|
fa
|
|
}
|
|
|
|
fn map_err<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> E1,
|
|
) -> WrapE<'a, A, E1, Self> {
|
|
wa.map_err(f)
|
|
}
|
|
|
|
fn bind_err<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
|
|
) -> WrapE<'a, A, E1, Self> {
|
|
wa.bind_err(f)
|
|
}
|
|
|
|
fn bind<A: 'a, B: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
|
|
) -> WrapE<'a, B, E1, Self> {
|
|
f(wa)
|
|
}
|
|
|
|
fn rotate_out<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, Result<A, E1>, E0, Self>,
|
|
) -> WrapE<'a, A, Result<E1, E0>, Self> {
|
|
match wa {
|
|
Ok(Ok(a)) => Ok(a),
|
|
Ok(Err(e)) => Err(Ok(e)),
|
|
Err(e) => Err(Err(e)),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct ResultFailOver<T>(T);
|
|
|
|
impl<'a, T: Monad<'a>> MonadFailAny<'a> for ResultFailOver<T> {
|
|
type W<E: 'a> = super::composition::CompositionInstance<T, ResultInstance<E>>;
|
|
|
|
type T = T;
|
|
|
|
fn unstuff<A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T> {
|
|
wa
|
|
}
|
|
|
|
fn stuff<A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self> {
|
|
fa
|
|
}
|
|
|
|
fn map_err<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> E1,
|
|
) -> WrapE<'a, A, E1, Self> {
|
|
T::fmap(|a| a.map_err(f), wa)
|
|
}
|
|
|
|
fn bind_err<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
|
|
) -> WrapE<'a, A, E1, Self> {
|
|
T::bind(wa, |a| match a {
|
|
Ok(a) => T::pure(Ok(a)),
|
|
Err(e) => f(e),
|
|
})
|
|
}
|
|
|
|
fn bind<A: 'a, B: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
|
|
) -> WrapE<'a, B, E1, Self> {
|
|
T::bind(wa, f)
|
|
}
|
|
|
|
fn rotate_out<A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, Result<A, E1>, E0, Self>,
|
|
) -> WrapE<'a, A, Result<E1, E0>, Self> {
|
|
T::fmap(<ResultFailAny as MonadFailAny>::rotate_out, wa)
|
|
}
|
|
}
|