Diffstat (limited to 'src/effect.rs')
| -rw-r--r-- | src/effect.rs | 92 |
1 files changed, 59 insertions, 33 deletions
diff --git a/src/effect.rs b/src/effect.rs index b50b37a..9c72977 100644 --- a/src/effect.rs +++ b/src/effect.rs @@ -5,12 +5,19 @@ use core::{ task::{Context, Poll, RawWaker, RawWakerVTable, Waker}, }; -use crate::{bijective_higher_ranked_trait, bijective_higher_ranked_type}; - -bijective_higher_ranked_trait! { - pub type class SendFuture[][Output]: [for<'lt> core::future::Future<Output = Output> + Sized + Send + 'lt] - where { - Output: Send, +use crate::{higher_ranked_trait, higher_ranked_type, hkt::Marker}; + +higher_ranked_trait! { + pub type class SendFuture[Output] for<'a> { + type Bound = &'a Output; + + type T: { core::future::Future<Output = Output> + Sized + Send + 'a } + where { + Output: 'a + }; + type HigherRanked: {} where { + // Output: Send + }; } } @@ -18,17 +25,17 @@ bijective_higher_ranked_trait! { pub trait Effect: Send + 'static { type Future<T: Send>: SendFuture::MemberType<T>; - fn wrap<'a, F>(future: F) -> SendFuture::T<'a, Self::Future<F::Output>, F::Output> + fn wrap<'a, F>(future: F) -> SendFuture::T<'a, F::Output, Self::Future<F::Output>> where F: core::future::Future + Send + 'a, <F as core::future::Future>::Output: Send; - fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, Self::Future<T>, T>; + fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, T, Self::Future<T>>; fn map<'a, T, U, F>( - future: SendFuture::T<'a, Self::Future<T>, T>, + future: SendFuture::T<'a, T, Self::Future<T>>, func: F, - ) -> SendFuture::T<'a, Self::Future<U>, U> + ) -> SendFuture::T<'a, U, Self::Future<U>> where T: Send, U: Send, @@ -38,7 +45,7 @@ pub trait Effect: Send + 'static { #[inline] fn wrap_boxed<'a, F>( future: core::pin::Pin<Box<F>>, - ) -> SendFuture::T<'a, Self::Future<F::Output>, F::Output> + ) -> SendFuture::T<'a, F::Output, Self::Future<F::Output>> where F: core::future::Future + Send + 'a, <F as core::future::Future>::Output: Send, @@ -47,7 +54,7 @@ pub trait Effect: Send + 'static { } } -pub type Future<'a, T, E> = SendFuture::T<'a, <E as Effect>::Future<T>, T>; +pub type Future<'a, T, E> = SendFuture::T<'a, T, <E as Effect>::Future<T>>; pub struct Blocking<B = Spin> { _marker: PhantomData<fn() -> B>, @@ -99,18 +106,26 @@ pub fn noop() -> Waker { unsafe { Waker::from_raw(RAW) } } -bijective_higher_ranked_type! { - pub type ReadyFuture[][Output]: SendFuture[][Output] - for<'lt> (core::future::Ready<Output>) - where { - Output: Send, +higher_ranked_type! { + impl SendFuture { + impl['a, Output] type T['a, Output] for core::future::Ready<Output> = + core::future::Ready<Output> + where { + Output: Send + }; + + impl['a, Output] type HigherRanked['a, Output] for core::future::Ready<Output> = + core::future::Ready<Output> + where { + Output: Send + }; } } impl<B: BlockOn> Effect for Blocking<B> { - type Future<T: Send> = ReadyFuture<T>; + type Future<T: Send> = core::future::Ready<T>; - fn wrap<'a, F>(future: F) -> SendFuture::T<'a, Self::Future<F::Output>, F::Output> + fn wrap<'a, F>(future: F) -> SendFuture::T<'a, F::Output, Self::Future<F::Output>> where F: core::future::Future + Send + 'a, <F as core::future::Future>::Output: Send, @@ -118,14 +133,14 @@ impl<B: BlockOn> Effect for Blocking<B> { core::future::ready(B::block_on(future)) } - fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, Self::Future<T>, T> { + fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, T, Self::Future<T>> { core::future::ready(value) } fn map<'a, T, U, F>( - future: SendFuture::T<'a, Self::Future<T>, T>, + future: SendFuture::T<'a, T, Self::Future<T>>, func: F, - ) -> SendFuture::T<'a, Self::Future<U>, U> + ) -> SendFuture::T<'a, U, Self::Future<U>> where T: Send, U: Send, @@ -158,11 +173,22 @@ mod sealed { } #[cfg(feature = "alloc")] -bijective_higher_ranked_type! { - pub type BoxFuture[][Output]: SendFuture[][Output] - for<'lt> (sealed::BoxedFuture<'lt, Output>) - where { - Output: Send, +pub struct BoxedFutureHrt<Output>(Marker<Output>); + +#[cfg(feature = "alloc")] +higher_ranked_type! { + impl SendFuture { + impl['a, Output] type T['a, Output] for BoxedFutureHrt<Output> = + sealed::BoxedFuture<'a, Output> + where { + Output: Send + }; + + impl['a, Output] type HigherRanked['a, Output] for sealed::BoxedFuture<'a, Output> = + BoxedFutureHrt<Output> + where { + Output: Send + }; } } @@ -171,9 +197,9 @@ pub enum Async {} #[cfg(feature = "alloc")] impl Effect for Async { - type Future<T: Send> = BoxFuture<T>; + type Future<T: Send> = BoxedFutureHrt<T>; - fn wrap<'a, F>(future: F) -> SendFuture::T<'a, Self::Future<F::Output>, F::Output> + fn wrap<'a, F>(future: F) -> SendFuture::T<'a, F::Output, Self::Future<F::Output>> where F: core::future::Future + Send + 'a, <F as core::future::Future>::Output: Send, @@ -181,14 +207,14 @@ impl Effect for Async { sealed::BoxedFuture::Box(Box::pin(future)) } - fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, Self::Future<T>, T> { + fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, T, Self::Future<T>> { sealed::BoxedFuture::Ready(core::future::ready(value)) } fn map<'a, T, U, F>( - future: SendFuture::T<'a, Self::Future<T>, T>, + future: SendFuture::T<'a, T, Self::Future<T>>, func: F, - ) -> SendFuture::T<'a, Self::Future<U>, U> + ) -> SendFuture::T<'a, U, Self::Future<U>> where T: Send, U: Send, @@ -205,7 +231,7 @@ impl Effect for Async { fn wrap_boxed<'a, F>( future: core::pin::Pin<Box<F>>, - ) -> SendFuture::T<'a, Self::Future<F::Output>, F::Output> + ) -> SendFuture::T<'a, F::Output, Self::Future<F::Output>> where F: core::future::Future + Send + 'a, <F as core::future::Future>::Output: Send, |