diff --git a/src/func/classes.rs b/src/func/classes.rs index 9d1c030..c81f35b 100644 --- a/src/func/classes.rs +++ b/src/func/classes.rs @@ -26,3 +26,4 @@ pub mod option; pub mod result; pub mod solo; pub mod stackless; +pub mod tryfuture; diff --git a/src/func/classes/tryfuture.rs b/src/func/classes/tryfuture.rs new file mode 100644 index 0000000..fb8cf97 --- /dev/null +++ b/src/func/classes/tryfuture.rs @@ -0,0 +1,261 @@ +use std::{future::Future, pin::Pin}; + +use futures::{future::Shared, try_join, FutureExt}; + +use crate::func::*; + +#[derive(CovariantFunctor)] +pub struct TryFutureClass(E); + +impl WeakFunctor for TryFutureClass { + type F<'a, A: 'a> = Pin>>> where Self: 'a; +} + +impl Functor for TryFutureClass { + 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, + { + Box::pin(async { Ok(f(fa.await?)) }) + } + + fn replace<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, b: B) -> Self::F<'a, B> + where + Self: 'a, + { + Box::pin(async { + fa.await?; + Ok(b) + }) + } +} + +impl Pure for TryFutureClass { + fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A> + where + Self: 'a, + { + Box::pin(async { Ok(a) }) + } +} + +impl ApplicativeSeq for TryFutureClass { + 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, + { + Box::pin(async { + let (f, a) = try_join!(ff, fa)?; + Ok(f(a)) + }) + } +} + +impl ApplicativeLA2 for TryFutureClass { + 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, + { + Box::pin(async { + let (a, b) = try_join!(fa, fb)?; + Ok(f(a, b)) + }) + } +} + +impl ApplicativeTuple for TryFutureClass { + 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, + { + Box::pin(async { try_join!(fa, fb) }) + } +} + +impl Applicative for TryFutureClass { + 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, + { + Box::pin(async { Ok(try_join!(fa, fb)?.1) }) + } + + 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, + { + Box::pin(async { Ok(try_join!(fa, fb)?.0) }) + } +} + +impl Monad for TryFutureClass { + 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, + { + Box::pin(async { f(fa.await?).await }) + } + + 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, + { + Box::pin(async move { + loop { + match f(a).await? { + ControlFlow::Continue(next_a) => a = next_a, + ControlFlow::Break(b) => return Ok(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, + { + Box::pin(async move { + loop { + match f.next(a).await? { + ControlFlow::Continue((next_a, next_f)) => (a, f) = (next_a, next_f), + ControlFlow::Break(b) => return Ok(b), + } + } + }) + } + + fn iterate<'a, B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B> + where + Self: 'a, + { + Box::pin(async move { + loop { + match f.next().await? { + ControlFlow::Continue(next_f) => f = next_f, + ControlFlow::Break(b) => return Ok(b), + } + } + }) + } + + fn join<'a, A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> + where + Self: 'a, + { + Box::pin(async { ffa.await?.await }) + } +} + +impl SharedFunctor for TryFutureClass { + type Shared<'a, A: 'a + Clone> = Shared>>>> + where + Self: 'a; + + fn share<'a, A: 'a + Clone>(fa: Self::F<'a, A>) -> Self::Shared<'a, A> + where + Self: 'a, + { + fa.shared() + } + + fn unshare<'a, A: 'a + Clone>(sa: Self::Shared<'a, A>) -> Self::F<'a, A> + where + Self: 'a, + { + Box::pin(sa) + } +} + +impl MonadFail for TryFutureClass { + fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A> + where + Self: 'a, + E: 'a, + { + Box::pin(async { Err(e) }) + } +} + +struct FutureFailAny; + +impl MonadFailAny for FutureFailAny { + type W = TryFutureClass; + + 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, + { + Box::pin(async { wa.await.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, + { + Box::pin(async { + match wa.await { + Ok(a) => Ok(a), + Err(e) => f(e).await, + } + }) + } + + fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>( + wa: as WeakFunctor>::F<'a, Result>, + ) -> > as WeakFunctor>::F<'a, A> + where + Self: 'a, + { + Box::pin(async { + match wa.await { + Ok(Ok(a)) => Ok(a), + Ok(Err(e)) => Err(Ok(e)), + Err(e) => Err(Err(e)), + } + }) + } +} + +impl MonadFailOver for FutureFailAny { + fn unstuff<'a, A: 'a, E: 'a>( + wa: as WeakFunctor>::F<'a, A>, + ) -> ::F<'a, Result> + where + Self: 'a, + classes::future::FutureClass: 'a, + { + wa + } + + fn stuff<'a, A: 'a, E: 'a>( + fa: ::F<'a, Result>, + ) -> as WeakFunctor>::F<'a, A> + where + Self: 'a, + classes::future::FutureClass: 'a, + { + fa + } +}