254 lines
5.9 KiB
Rust
254 lines
5.9 KiB
Rust
use std::{future::Future, pin::Pin};
|
|
|
|
use futures::{
|
|
future::{try_select, Either, Shared},
|
|
try_join, FutureExt,
|
|
};
|
|
|
|
use crate::func::*;
|
|
|
|
#[derive(CovariantFunctor)]
|
|
pub struct TryFutureInstance<E>(E);
|
|
|
|
impl<E> WeakFunctor for TryFutureInstance<E> {
|
|
type F<'a, A: 'a> = Pin<Box<dyn 'a + Future<Output = Result<A, E>>>> where Self: 'a;
|
|
}
|
|
|
|
impl<E> Functor for TryFutureInstance<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,
|
|
{
|
|
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<E> Pure for TryFutureInstance<E> {
|
|
fn pure<'a, A: 'a>(a: A) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Box::pin(async { Ok(a) })
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeSeq for TryFutureInstance<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,
|
|
{
|
|
Box::pin(async {
|
|
let (f, a) = try_join!(ff, fa)?;
|
|
Ok(f(a))
|
|
})
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeLA2 for TryFutureInstance<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,
|
|
{
|
|
Box::pin(async {
|
|
let (a, b) = try_join!(fa, fb)?;
|
|
Ok(f(a, b))
|
|
})
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeTuple for TryFutureInstance<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,
|
|
{
|
|
Box::pin(async { try_join!(fa, fb) })
|
|
}
|
|
}
|
|
|
|
impl<E> ApplicativeSelect for TryFutureInstance<E> {
|
|
fn select<'a, A: 'a, B: 'a>(
|
|
fa: Self::F<'a, A>,
|
|
fb: Self::F<'a, B>,
|
|
) -> SelectedWrapped<'a, A, B, Self>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Box::pin(async {
|
|
match try_select(fa, fb).await {
|
|
Ok(Either::Left((a, fb))) => Ok(Selected::A(a, fb)),
|
|
Ok(Either::Right((b, fa))) => Ok(Selected::B(fa, b)),
|
|
Err(Either::Left((e, _))) => Err(e),
|
|
Err(Either::Right((e, _))) => Err(e),
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
impl<E> Applicative for TryFutureInstance<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,
|
|
{
|
|
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<E> Monad for TryFutureInstance<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,
|
|
{
|
|
Box::pin(async { f(fa.await?).await })
|
|
}
|
|
|
|
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<E: Clone> SharedFunctor for TryFutureInstance<E> {
|
|
type Shared<'a, A: 'a + Clone> = Shared<Pin<Box<dyn 'a + Future<Output = Result<A, E>>>>>
|
|
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<E> Fail<E> for TryFutureInstance<E> {
|
|
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
|
|
where
|
|
Self: 'a,
|
|
E: 'a,
|
|
{
|
|
Box::pin(async { Err(e) })
|
|
}
|
|
}
|
|
|
|
pub struct FutureFailAny;
|
|
|
|
impl MonadFailAny for FutureFailAny {
|
|
type W<E> = TryFutureInstance<E>;
|
|
|
|
type T = instances::future::FutureInstance;
|
|
|
|
fn unstuff<'a, A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
wa
|
|
}
|
|
|
|
fn stuff<'a, A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
fa
|
|
}
|
|
|
|
fn map_err<'a, A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, A, E0, Self>,
|
|
f: impl 'a + FnOnce(E0) -> E1,
|
|
) -> WrapE<'a, A, E1, Self>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Box::pin(async { wa.await.map_err(f) })
|
|
}
|
|
|
|
fn bind_err<'a, 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>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Box::pin(async {
|
|
match wa.await {
|
|
Ok(a) => Ok(a),
|
|
Err(e) => f(e).await,
|
|
}
|
|
})
|
|
}
|
|
|
|
fn bind<'a, 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>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Box::pin(async { f(wa.await).await })
|
|
}
|
|
|
|
fn rotate_out<'a, A: 'a, E0: 'a, E1: 'a>(
|
|
wa: WrapE<'a, Result<A, E1>, E0, Self>,
|
|
) -> WrapE<'a, A, Result<E1, E0>, Self>
|
|
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)),
|
|
}
|
|
})
|
|
}
|
|
}
|