From 979d1a587ab30b6467ac07b8b005ce6a9a4b4150 Mon Sep 17 00:00:00 2001 From: timofey Date: Sat, 20 May 2023 13:59:26 +0000 Subject: [PATCH] `MonadFailAny` default implementations --- src/func.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/func.rs b/src/func.rs index 97e8d58..aa7cd8b 100644 --- a/src/func.rs +++ b/src/func.rs @@ -287,21 +287,35 @@ pub trait MonadFailAny { f: impl 'a + FnOnce(E0) -> E1, ) -> as WeakFunctor>::F<'a, A> where - Self: 'a; + Self: 'a, + { + Self::bind_err(wa, |e0| Self::fail(f(e0))) + } 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; + Self: 'a, + { + Self::bind(wa, |result| match result { + Ok(a) => Self::pure(a), + Err(e0) => f(e0), + }) + } fn bind<'a, A: 'a, B: 'a, E0: 'a, E1: 'a>( wa: as WeakFunctor>::F<'a, A>, f: impl 'a + FnOnce(Result) -> as WeakFunctor>::F<'a, B>, ) -> as WeakFunctor>::F<'a, B> where - Self: 'a; + Self: 'a, + { + Self::stuff(::bind(Self::unstuff(wa), |result| { + Self::unstuff(f(result)) + })) + } fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>( wa: as WeakFunctor>::F<'a, Result>,