Diffstat (limited to 'src/effect/async.rs')
-rw-r--r--src/effect/async.rs434
1 files changed, 114 insertions, 320 deletions
diff --git a/src/effect/async.rs b/src/effect/async.rs
index 84aacee..ffe8bb7 100644
--- a/src/effect/async.rs
+++ b/src/effect/async.rs
@@ -1,4 +1,18 @@
-use core::pin::Pin;
+// mod join;
+// mod try_join;
+mod map;
+mod shim;
+mod then;
+mod value;
+
+// pub use join::*;
+// pub use try_join::*;
+pub use map::*;
+pub use shim::*;
+pub use then::*;
+pub use value::*;
+
+use core::{ops::ControlFlow, pin::Pin};
use core::future::Future;
@@ -7,7 +21,7 @@ use pin_project::pin_project;
use crate::hkt::Marker;
-use super::{Effect, Effective, ErasedForLt, Join, TryJoin};
+use super::{Effect, Effective, ErasedEffective, ErasedForLt, Join, TryJoin};
pub enum Async {}
@@ -26,13 +40,13 @@ pub enum BoxOrReady<'a, T> {
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + Sync + 'a>>;
-impl<'a, T: Send + Sync> ErasedForLt<'a, T, Async, &'a T> for ErasedHrt<T> {
+impl<'a, T: Send + Sync, O> ErasedForLt<'a, T, Async, &'a (T, O), O> for ErasedHrt<T> {
type T = BoxOrReady<'a, T>;
}
-pub struct Value<T>(pub T);
-
-pub struct Wrapped<F>(pub F);
+impl<T: Send + Sync> super::ErasedHrt<T, Async> for ErasedHrt<T> {
+ type T<B> = ErasedHrt<T>;
+}
impl Effect for Async {
type Erased<T: Send + Sync> = ErasedHrt<T>;
@@ -40,10 +54,10 @@ impl Effect for Async {
type Ready<'a, T: Send + Sync + 'a> = Value<T>;
fn ready<'a, T: Send + Sync + 'a>(value: T) -> Self::Ready<'a, T> {
- todo!()
+ Value(value)
}
- type FromFuture<'a, F: Send + Sync + 'a> = Wrapped<F>
+ type FromFuture<'a, F: Send + Sync + 'a> = Shim<F>
where
F: futures::prelude::Future,
F::Output: Send + Sync + 'a;
@@ -53,12 +67,12 @@ impl Effect for Async {
F: futures::prelude::Future,
F::Output: Send + Sync + 'a,
{
- todo!()
+ Shim::new(future)
}
}
impl<'lt, U: Send + Sync + 'lt> Effective<'lt> for BoxOrReady<'lt, U> {
- fn into_erased(self) -> super::ErasedEffective<'lt, Self::Output, Self::Effect> {
+ fn into_erased<B>(self) -> super::ErasedEffective<'lt, Self::Output, Self::Effect, B> {
self
}
@@ -75,80 +89,47 @@ impl<'lt, U: Send + Sync + 'lt> Effective<'lt> for BoxOrReady<'lt, U> {
}
}
- type Map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a> = futures::future::Map<ErasedFuture<'lt, U>, F>
+ type Loop<'ctx: 'a, 'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
+ = BoxOrReady<'a, (U, T)>
where
- F: FnOnce(Self::Output) -> T,
+ F: for<'b> FnMut(&'b mut Self::Output) -> ErasedEffective<'b, ControlFlow<T>, Self::Effect>,
'lt: 'a;
- fn map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::Map<'a, T, F>
+ fn r#loop<'ctx: 'a, 'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, mut cb: F) -> Self::Loop<'ctx, 'a, T, F>
where
- F: FnOnce(Self::Output) -> T,
- 'lt: 'a
+ F: for<'b> FnMut(&'b mut Self::Output) -> ErasedEffective<'b, ControlFlow<T>, Self::Effect>,
+ 'lt: 'a,
{
- todo!()
- }
+ BoxOrReady::Boxed(Box::pin(async move {
+ let mut this = self.into_future().await;
- type Then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>
- = futures::future::Then<ErasedFuture<'lt, U>, V::IntoFuture, F>
- where
- F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect>;
-
- fn then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>(
- self,
- cb: F,
- ) -> Self::Then<'a, T, V, F>
- where
- F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect> {
- todo!()
+ loop {
+ if let ControlFlow::Break(value) = cb(&mut this).into_future().await {
+ return (this, value);
+ }
+ }
+ }))
}
- type AsCtx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
- = BoxedFuture<'a, T>
+ type Map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a> = Map<ErasedFuture<'lt, U>, F>
where
- F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
+ F: FnOnce(Self::Output) -> T,
'lt: 'a;
- fn as_ctx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::AsCtx<'a, T, F>
- where
- F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
- 'lt: 'a {
- todo!()
- }
-}
-
-impl<'lt, U: Send + Sync + 'lt> Effective<'lt> for Value<U> {
- fn into_erased(self) -> super::ErasedEffective<'lt, Self::Output, Self::Effect> {
- todo!()
- }
-
- type Effect = Async;
-
- type Output = U;
-
- type IntoFuture = core::future::Ready<U>;
-
- fn into_future(self) -> Self::IntoFuture {
- core::future::ready(self.0)
- }
-
- type Map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
- = core::future::Ready<T>
- where
- F: FnOnce(Self::Output) -> T;
-
fn map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::Map<'a, T, F>
where
- F: FnOnce(Self::Output) -> T {
- todo!()
+ F: FnOnce(Self::Output) -> T,
+ 'lt: 'a,
+ {
+ Map::new(self.into_future(), cb)
}
type Then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>
- = V
+ = Then<'a, ErasedFuture<'lt, U>, F, V>
where
F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect>;
+ V: Effective<'a, Output = T, Effect = Self::Effect>,
+ 'lt: 'a;
fn then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>(
self,
@@ -156,81 +137,30 @@ impl<'lt, U: Send + Sync + 'lt> Effective<'lt> for Value<U> {
) -> Self::Then<'a, T, V, F>
where
F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect> {
- todo!()
+ V: Effective<'a, Output = T, Effect = Self::Effect>,
+ 'lt: 'a,
+ {
+ Then::new(self.into_future(), cb)
}
- type AsCtx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
- = BoxedFuture<'a, T>
+ type AsCtx<'ctx: 'a, 'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
+ = BoxOrReady<'a, (Self::Output, T)>
where
F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
'lt: 'a;
- fn as_ctx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::AsCtx<'a, T, F>
+ fn as_ctx<'ctx: 'a, 'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::AsCtx<'ctx, 'a, T, F>
where
F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
- 'lt: 'a {
- todo!()
- }
-}
-
-impl<'lt, U: Send + Sync + 'lt> Effective<'lt> for Wrapped<U>
-where
- U: Future,
- U::Output: Send + Sync + 'lt
-{
- fn into_erased(self) -> super::ErasedEffective<'lt, Self::Output, Self::Effect> {
- todo!()
- }
-
- type Effect = Async;
-
- type Output = U::Output;
-
- type IntoFuture = U;
-
- fn into_future(self) -> Self::IntoFuture {
- todo!()
- }
+ 'lt: 'a,
+ {
+ BoxOrReady::Boxed(Box::pin(async {
+ let mut this = self.into_future().await;
- type Map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
- = futures::future::Map<U, F>
- where
- F: FnOnce(Self::Output) -> T;
+ let result = cb(&mut this).into_future().await;
- fn map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::Map<'a, T, F>
- where
- F: FnOnce(Self::Output) -> T {
- todo!()
- }
-
- type Then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>
- = futures::future::Then<U, V::IntoFuture, F>
- where
- F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect>;
-
- fn then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>(
- self,
- cb: F,
- ) -> Self::Then<'a, T, V, F>
- where
- F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect> {
- todo!()
- }
-
- type AsCtx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
- = BoxedFuture<'a, T>
- where
- F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
- 'lt: 'a;
-
- fn as_ctx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::AsCtx<'a, T, F>
- where
- F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
- 'lt: 'a {
- todo!()
+ (this, result)
+ }))
}
}
@@ -238,255 +168,119 @@ impl Join for Async {
type Effect = Self;
type Two<'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a>
- = futures::future::Join<ErasedFuture<'a, T0>, ErasedFuture<'a, T1>>
+ = Shim<futures::future::Join<T0::IntoFuture, T1::IntoFuture>>
where
T0: super::Effective<'a, Effect = Self::Effect>,
T1: super::Effective<'a, Effect = Self::Effect>;
- type Three<'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a, T2: Send + Sync + 'a> = futures::future::Join3<ErasedFuture<'a, T0>, ErasedFuture<'a, T1>, ErasedFuture<'a, T2>, >
+ type Three<'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a, T2: Send + Sync + 'a>
+ = Shim<futures::future::Join3<T0::IntoFuture, T1::IntoFuture, T2::IntoFuture, >>
where
T0: super::Effective<'a, Effect = Self::Effect>,
T1: super::Effective<'a, Effect = Self::Effect>,
T2: super::Effective<'a, Effect = Self::Effect>;
- fn two<
- 'a,
- T0: Send + Sync + 'a,
- T1: Send + Sync + 'a,
- F0: Send + Sync + 'a,
- F1: Send + Sync + 'a,
- >(
- cb: (F0, F1),
- ) -> Self::Two<'a, T0, T1>
+ fn two<'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a>(cb: (T0, T1)) -> Self::Two<'a, T0, T1>
where
T0: super::Effective<'a, Effect = Self::Effect>,
T1: super::Effective<'a, Effect = Self::Effect>,
- F0: FnOnce() -> T0,
- F1: FnOnce() -> T1 {
- todo!()
+ {
+ Shim::new(futures::future::join(
+ cb.0.into_future(),
+ cb.1.into_future(),
+ ))
}
- fn three<
- 'a,
- T0: Send + Sync + 'a,
- T1: Send + Sync + 'a,
- T2: Send + Sync + 'a,
- F0: Send + Sync + 'a,
- F1: Send + Sync + 'a,
- F2: Send + Sync + 'a,
- >(
- effectives: (F0, F1, F2),
+ fn three<'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a, T2: Send + Sync + 'a>(
+ cb: (T0, T1, T2),
) -> Self::Three<'a, T0, T1, T2>
where
T0: super::Effective<'a, Effect = Self::Effect>,
T1: super::Effective<'a, Effect = Self::Effect>,
T2: super::Effective<'a, Effect = Self::Effect>,
- F0: FnOnce() -> T0,
- F1: FnOnce() -> T1,
- F2: FnOnce() -> T2 {
- todo!()
+ {
+ Shim::new(futures::future::join3(
+ cb.0.into_future(),
+ cb.1.into_future(),
+ cb.2.into_future(),
+ ))
}
}
impl TryJoin for Async {
type Effect = Self;
- type Two<'a, Err: Send + Sync + 'a, V0: Send + Sync + 'a, V1: Send + Sync + 'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a>
- = futures::future::TryJoin<ErasedFuture<'a, Result<T0, Err>>, ErasedFuture<'a, Result<T1, Err>>>
+ type Two<'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a>
+ = Shim<futures::future::TryJoin<BoxedFuture<'a, T0::Output>, BoxedFuture<'a, T1::Output>>>
where
- V0: super::Effective<'a, Output = Result<T0, Err>, Effect = Self::Effect>,
- V1: super::Effective<'a, Output = Result<T1, Err>, Effect = Self::Effect>;
+ T0: super::TryEffective<'a, Effect = Self::Effect>,
+ T1: super::TryEffective<'a, Err = T0::Err, Effect = Self::Effect>;
- type Three<'a, Err: Send + Sync + 'a, V0: Send + Sync + 'a, V1: Send + Sync + 'a, V2: Send + Sync + 'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a, T2: Send + Sync + 'a>
- = futures::future::TryJoin3<ErasedFuture<'a, Result<T0, Err>>, ErasedFuture<'a, Result<T1, Err>>, ErasedFuture<'a, Result<T1, Err>>>
+ type Three<'a, T0: Send + Sync + 'a, T1: Send + Sync + 'a, T2: Send + Sync + 'a>
+ = Shim<futures::future::TryJoin3<BoxedFuture<'a, T0::Output>, BoxedFuture<'a, T1::Output>, BoxedFuture<'a, T2::Output>>>
where
- V0: super::Effective<'a, Output = Result<T0, Err>, Effect = Self::Effect>,
- V1: super::Effective<'a, Output = Result<T1, Err>, Effect = Self::Effect>,
- V2: super::Effective<'a, Output = Result<T2, Err>, Effect = Self::Effect>;
+ T0: super::TryEffective<'a, Effect = Self::Effect>,
+ T1: super::TryEffective<'a, Err = T0::Err, Effect = Self::Effect>,
+ T2: super::TryEffective<'a, Err = T0::Err, Effect = Self::Effect>;
fn two<
'a,
- Err: Send + Sync + 'a,
T0: Send + Sync + 'a,
T1: Send + Sync + 'a,
- V0: Send + Sync + 'a,
- V1: Send + Sync + 'a,
F0: Send + Sync + 'a,
F1: Send + Sync + 'a,
>(
cb: (F0, F1),
- ) -> Self::Two<'a, Err, V0, V1, T0, T1>
+ ) -> Self::Two<'a, T0, T1>
where
- V0: super::Effective<'a, Output = Result<T0, Err>, Effect = Self::Effect>,
- V1: super::Effective<'a, Output = Result<T1, Err>, Effect = Self::Effect>,
- F0: FnOnce() -> V0,
- F1: FnOnce() -> V1 {
- todo!()
+ T0: super::TryEffective<'a, Effect = Self::Effect>,
+ T1: super::TryEffective<'a, Err = T0::Err, Effect = Self::Effect>,
+ F0: FnOnce() -> T0,
+ F1: FnOnce() -> T1,
+ {
+ Shim::new(futures::future::try_join(
+ Box::pin(async { cb.0().into_future().await }),
+ Box::pin(async { cb.1().into_future().await }),
+ ))
}
fn three<
'a,
- Err: Send + Sync + 'a,
T0: Send + Sync + 'a,
T1: Send + Sync + 'a,
T2: Send + Sync + 'a,
- V0: Send + Sync + 'a,
- V1: Send + Sync + 'a,
- V2: Send + Sync + 'a,
F0: Send + Sync + 'a,
F1: Send + Sync + 'a,
F2: Send + Sync + 'a,
>(
cb: (F0, F1, F2),
- ) -> Self::Three<'a, Err, V0, V1, V2, T0, T1, T2>
+ ) -> Self::Three<'a, T0, T1, T2>
where
- V0: super::Effective<'a, Output = Result<T0, Err>, Effect = Self::Effect>,
- V1: super::Effective<'a, Output = Result<T1, Err>, Effect = Self::Effect>,
- V2: super::Effective<'a, Output = Result<T2, Err>, Effect = Self::Effect>,
- F0: FnOnce() -> V0,
- F1: FnOnce() -> V1,
- F2: FnOnce() -> V2 {
- todo!()
+ T0: super::TryEffective<'a, Effect = Self::Effect>,
+ T1: super::TryEffective<'a, Err = T0::Err, Effect = Self::Effect>,
+ T2: super::TryEffective<'a, Err = T0::Err, Effect = Self::Effect>,
+ F0: FnOnce() -> T0,
+ F1: FnOnce() -> T1,
+ F2: FnOnce() -> T2,
+ {
+ Shim::new(futures::future::try_join3(
+ Box::pin(async { cb.0().into_future().await }),
+ Box::pin(async { cb.1().into_future().await }),
+ Box::pin(async { cb.2().into_future().await }),
+ ))
}
}
impl<'lt, T> Future for ErasedFuture<'lt, T> {
type Output = T;
- fn poll(self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> core::task::Poll<Self::Output> {
+ fn poll(
+ self: Pin<&mut Self>,
+ cx: &mut core::task::Context<'_>,
+ ) -> core::task::Poll<Self::Output> {
match self.project() {
ErasedFutureProj::Boxed(fut) => fut.poll_unpin(cx),
ErasedFutureProj::Ready(fut) => fut.poll(cx),
}
}
}
-
-impl<'c, 'lt: 'c, Fut0: Send + Sync + 'lt, F0: Send + Sync + 'c, R0: Send + Sync + 'c> Effective<'c> for futures::future::Map<Fut0, F0>
-where
- F0: FnOnce(Fut0::Output) -> R0,
- Fut0: Future,
- Fut0::Output: 'lt,
-{
- fn into_erased(self) -> super::ErasedEffective<'c, Self::Output, Self::Effect> {
- todo!()
- }
-
- type Effect = Async;
-
- type Output = R0;
-
- type IntoFuture = Self;
-
- fn into_future(self) -> Self::IntoFuture {
- self
- }
-
- type Map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a> =
- futures::future::Map<Self, F>
- where
- F: FnOnce(Self::Output) -> T,
- 'c: 'a;
-
- fn map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::Map<'a, T, F>
- where
- F: FnOnce(Self::Output) -> T,
- 'c: 'a
- {
- todo!()
- }
-
- type Then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>
- = futures::future::Then<Self, V, F>
- where
- F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect>;
-
- fn then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>(
- self,
- cb: F,
- ) -> Self::Then<'a, T, V, F>
- where
- F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect> {
- todo!()
- }
-
- type AsCtx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
- = BoxOrReady<'a, T>
- where
- F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
- 'a: 'a;
-
- fn as_ctx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::AsCtx<'a, T, F>
- where
- F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
- 'a: 'a {
- todo!()
- }
-}
-
-impl<'c, 'lt: 'c, Fut0: Send + Sync + 'lt, V0: Send + Sync + 'lt, Fut1: Send + Sync + 'lt, F0: Send + Sync + 'c> Effective<'c> for futures::future::Then<Fut0, Fut1, F0>
-where
- F0: FnOnce(Fut0::Output) -> V0,
- V0: Effective<'lt, Effect = Async, IntoFuture = Fut1>,
- Fut1: Future,
- Fut1::Output: Send + Sync + 'lt,
- Fut0: Future,
- Fut0::Output: Send + Sync + 'lt,
-{
- fn into_erased(self) -> super::ErasedEffective<'c, Self::Output, Self::Effect> {
- todo!()
- }
-
- type Effect = Async;
-
- type Output = Fut1::Output;
-
- type IntoFuture = Self;
-
- fn into_future(self) -> Self::IntoFuture {
- todo!()
- }
-
- type Map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
- = futures::future::Map<Self, F>
- where
- F: FnOnce(Self::Output) -> T,
- 'c: 'a;
-
- fn map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::Map<'a, T, F>
- where
- F: FnOnce(Self::Output) -> T,
- 'c: 'a {
- todo!()
- }
-
- type Then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>
- = futures::future::Then<Self, V, F>
- where
- F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect>;
-
- fn then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a>(
- self,
- cb: F,
- ) -> Self::Then<'a, T, V, F>
- where
- F: FnOnce(Self::Output) -> V,
- V: Effective<'a, Output = T, Effect = Self::Effect> {
- todo!()
- }
-
- type AsCtx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>
- = BoxOrReady<'a, T>
- where
- F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
- 'c: 'a;
-
- fn as_ctx<'a, T: Send + Sync + 'a, F: Send + Sync + 'a>(self, cb: F) -> Self::AsCtx<'a, T, F>
- where
- F: for<'b> FnOnce(&'b mut Self::Output) -> super::ErasedEffective<'b, T, Self::Effect>,
- 'c: 'a {
- todo!()
- }
-}