//! Implementation of [`MonadFail`] for [`Result`]. //! //! 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`] alternative see [`super::option`] use crate::func::*; #[derive(SharedFunctor, CovariantFunctor)] pub struct ResultClass(E); impl WeakFunctor for ResultClass { type F<'a, A: 'a> = Result where Self: 'a; } impl Functor for ResultClass { 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 Pure for ResultClass { fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> where Self: 'a, { Ok(a) } } impl ApplicativeSeq for ResultClass { 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 ApplicativeLA2 for ResultClass { 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 ApplicativeTuple for ResultClass { 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 Applicative for ResultClass { 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 Monad for ResultClass { 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>, ) -> 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 LocalFunctor for ResultClass { fn unstuff<'a, A: 'a, B: 'a>( state: Self::F<'a, ControlFlow>, ) -> ControlFlow, 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 Fail for ResultClass { 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(self, f: impl 'a + FnOnce(E0) -> Result) -> Result; } impl<'a, A: 'a, E0: 'a> ResultExt<'a, A, E0> for Result { fn bind_err(self, f: impl 'a + FnOnce(E0) -> Result) -> Result { match self { Ok(a) => Ok(a), Err(e) => f(e), } } } impl MonadFailAny for ResultFailAny { type W = ResultClass; fn map_err<'a, A: 'a, E0: 'a, E1: 'a>( wa: as WeakFunctor>::F<'a, A>, f: impl 'a + FnOnce(E0) -> E1, ) -> as WeakFunctor>::F<'a, A> where Self: 'a, { wa.map_err(f) } fn bind_err<'a, A: 'a, E0: 'a, E1: 'a>( wa: as WeakFunctor>::F<'a, A>, f: impl 'a + FnOnce(E0) -> as WeakFunctor>::F<'a, A>, ) -> as WeakFunctor>::F<'a, A> where Self: 'a, { wa.bind_err(f) } fn bind<'a, A: 'a, E0: 'a, E1: 'a>( wa: as WeakFunctor>::F<'a, A>, f: impl 'a + FnOnce(Result) -> as WeakFunctor>::F<'a, A>, ) -> as WeakFunctor>::F<'a, A> where Self: 'a, { f(wa) } fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>( wa: as WeakFunctor>::F<'a, Result>, ) -> > 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 for ResultFailAny { fn unstuff<'a, A: 'a, E: 'a>( wa: as WeakFunctor>::F<'a, A>, ) -> ::F<'a, Result> where Self: 'a, classes::solo::SoloClass: 'a, { wa } fn stuff<'a, A: 'a, E: 'a>( fa: ::F<'a, Result>, ) -> as WeakFunctor>::F<'a, A> where Self: 'a, classes::solo::SoloClass: 'a, { fa } } pub struct ResultFailOver(T); impl MonadFailAny for ResultFailOver { type W = super::composition::CompositionClass>; fn map_err<'a, A: 'a, E0: 'a, E1: 'a>( wa: as WeakFunctor>::F<'a, A>, f: impl 'a + FnOnce(E0) -> 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: as WeakFunctor>::F<'a, A>, f: impl 'a + FnOnce(E0) -> as WeakFunctor>::F<'a, A>, ) -> 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: as WeakFunctor>::F<'a, A>, f: impl 'a + FnOnce(Result) -> as WeakFunctor>::F<'a, A>, ) -> as WeakFunctor>::F<'a, A> where Self: 'a, { T::bind(wa, f) } fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>( wa: as WeakFunctor>::F<'a, Result>, ) -> > as WeakFunctor>::F<'a, A> where Self: 'a, { T::fmap(::rotate_out, wa) } } impl MonadFailOver for ResultFailOver { fn unstuff<'a, A: 'a, E: 'a>( wa: as WeakFunctor>::F<'a, A>, ) -> ::F<'a, Result> where Self: 'a, T: 'a, { wa } fn stuff<'a, A: 'a, E: 'a>( fa: ::F<'a, Result>, ) -> as WeakFunctor>::F<'a, A> where Self: 'a, T: 'a, { fa } }