Diffstat (limited to 'src/effect/async.rs')
| -rw-r--r-- | src/effect/async.rs | 492 |
1 files changed, 492 insertions, 0 deletions
diff --git a/src/effect/async.rs b/src/effect/async.rs new file mode 100644 index 0000000..84aacee --- /dev/null +++ b/src/effect/async.rs @@ -0,0 +1,492 @@ +use core::pin::Pin; + +use core::future::Future; + +use futures::FutureExt as _; +use pin_project::pin_project; + +use crate::hkt::Marker; + +use super::{Effect, Effective, ErasedForLt, Join, TryJoin}; + +pub enum Async {} + +pub struct ErasedHrt<T>(Marker<T>); + +#[pin_project(project = ErasedFutureProj)] +pub enum ErasedFuture<'a, T> { + Boxed(Pin<Box<dyn Future<Output = T> + Send + Sync + 'a>>), + Ready(#[pin] core::future::Ready<T>), +} + +pub enum BoxOrReady<'a, T> { + Boxed(Pin<Box<dyn Future<Output = T> + Send + Sync + 'a>>), + Ready(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> { + type T = BoxOrReady<'a, T>; +} + +pub struct Value<T>(pub T); + +pub struct Wrapped<F>(pub F); + +impl Effect for Async { + type Erased<T: Send + Sync> = ErasedHrt<T>; + + type Ready<'a, T: Send + Sync + 'a> = Value<T>; + + fn ready<'a, T: Send + Sync + 'a>(value: T) -> Self::Ready<'a, T> { + todo!() + } + + type FromFuture<'a, F: Send + Sync + 'a> = Wrapped<F> + where + F: futures::prelude::Future, + F::Output: Send + Sync + 'a; + + fn from_future<'a, F: Send + Sync + 'a>(future: F) -> Self::FromFuture<'a, F> + where + F: futures::prelude::Future, + F::Output: Send + Sync + 'a, + { + todo!() + } +} + +impl<'lt, U: Send + Sync + 'lt> Effective<'lt> for BoxOrReady<'lt, U> { + fn into_erased(self) -> super::ErasedEffective<'lt, Self::Output, Self::Effect> { + self + } + + type Effect = Async; + + type Output = U; + + type IntoFuture = ErasedFuture<'lt, U>; + + fn into_future(self) -> Self::IntoFuture { + match self { + BoxOrReady::Boxed(fut) => ErasedFuture::Boxed(fut), + BoxOrReady::Ready(value) => ErasedFuture::Ready(core::future::ready(value)), + } + } + + type Map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a> = futures::future::Map<ErasedFuture<'lt, U>, F> + where + F: FnOnce(Self::Output) -> T, + 'lt: '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, + 'lt: 'a + { + todo!() + } + + 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!() + } + + 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!() + } +} + +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!() + } + + type Then<'a, T: Send + Sync + 'a, V: Send + Sync + 'a, F: Send + Sync + 'a> + = V + 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!() + } +} + +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!() + } + + type Map<'a, T: Send + Sync + 'a, F: Send + Sync + 'a> + = futures::future::Map<U, F> + 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!() + } + + 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!() + } +} + +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>> + 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>, > + 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> + where + T0: super::Effective<'a, Effect = Self::Effect>, + T1: super::Effective<'a, Effect = Self::Effect>, + F0: FnOnce() -> T0, + F1: FnOnce() -> T1 { + todo!() + } + + 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), + ) -> 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!() + } +} + +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>>> + where + V0: super::Effective<'a, Output = Result<T0, Err>, Effect = Self::Effect>, + V1: super::Effective<'a, Output = Result<T1, 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>>> + 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>; + + 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> + 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!() + } + + 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> + 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!() + } +} + +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> { + 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!() + } +} |