Diffstat (limited to 'src/effect.rs')
| -rw-r--r-- | src/effect.rs | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/effect.rs b/src/effect.rs index bb54166..ebf082e 100644 --- a/src/effect.rs +++ b/src/effect.rs @@ -26,6 +26,8 @@ pub trait Effect<'ctx>: 'static { F: core::future::Future + Send + 'a, <F as core::future::Future>::Output: Send; + fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T>; + #[cfg(feature = "alloc")] #[inline] fn wrap_boxed<'a, F>( @@ -108,6 +110,31 @@ impl<'ctx, B: BlockOn> Effect<'ctx> for Blocking<B> { { core::future::ready(B::block_on(future)) } + + fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T> { + core::future::ready(value) + } +} + +mod sealed { + pub enum BoxedFuture<'lt, Output> { + Box(core::pin::Pin<Box<dyn core::future::Future<Output = Output> + Send + 'lt>>), + Ready(core::future::Ready<Output>), + } + + impl<'lt, Output> core::future::Future for BoxedFuture<'lt, Output> { + type Output = Output; + + fn poll( + mut self: core::pin::Pin<&mut Self>, + cx: &mut core::task::Context<'_>, + ) -> core::task::Poll<Self::Output> { + match &mut *self { + BoxedFuture::Box(future) => future.as_mut().poll(cx), + BoxedFuture::Ready(future) => core::pin::Pin::new(future).poll(cx), + } + } + } } #[cfg(feature = "alloc")] @@ -115,7 +142,7 @@ higher_ranked_type! { pub type BoxFuture['ctx, Output]: (SendFuture)[Output] where { Output: Send, - } = for<'lt> core::pin::Pin<Box<dyn core::future::Future<Output = Output> + Send + 'lt>> + } = for<'lt> sealed::BoxedFuture<'lt, Output> } #[cfg(feature = "alloc")] @@ -130,7 +157,7 @@ impl<'ctx> Effect<'ctx> for Async { F: core::future::Future + Send + 'a, <F as core::future::Future>::Output: Send, { - Box::pin(future) + sealed::BoxedFuture::Box(Box::pin(future)) } fn wrap_boxed<'a, F>( @@ -140,6 +167,10 @@ impl<'ctx> Effect<'ctx> for Async { F: core::future::Future + Send + 'a, <F as core::future::Future>::Output: Send, { - future + sealed::BoxedFuture::Box(future) + } + + fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T> { + sealed::BoxedFuture::Ready(core::future::ready(value)) } } |