Diffstat (limited to 'src/effect.rs')
| -rw-r--r-- | src/effect.rs | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/src/effect.rs b/src/effect.rs index ebf082e..3ce7a07 100644 --- a/src/effect.rs +++ b/src/effect.rs @@ -28,6 +28,15 @@ pub trait Effect<'ctx>: 'static { fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T>; + fn map<'a, T, U, F>( + future: SendFuture::T<'a, 'ctx, Self::Future<T>, T>, + func: F, + ) -> SendFuture::T<'a, 'ctx, Self::Future<U>, U> + where + T: Send, + U: Send, + F: FnOnce(T) -> U + Send + 'a; + #[cfg(feature = "alloc")] #[inline] fn wrap_boxed<'a, F>( @@ -114,6 +123,19 @@ impl<'ctx, B: BlockOn> Effect<'ctx> for Blocking<B> { fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T> { core::future::ready(value) } + + fn map<'a, T, U, F>( + future: SendFuture::T<'a, 'ctx, Self::Future<T>, T>, + func: F, + ) -> SendFuture::T<'a, 'ctx, Self::Future<U>, U> + where + T: Send, + U: Send, + F: FnOnce(T) -> U + Send + 'a, + { + let value = B::block_on(future); + core::future::ready(func(value)) + } } mod sealed { @@ -160,6 +182,28 @@ impl<'ctx> Effect<'ctx> for Async { sealed::BoxedFuture::Box(Box::pin(future)) } + fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, 'ctx, Self::Future<T>, T> { + sealed::BoxedFuture::Ready(core::future::ready(value)) + } + + fn map<'a, T, U, F>( + future: SendFuture::T<'a, 'ctx, Self::Future<T>, T>, + func: F, + ) -> SendFuture::T<'a, 'ctx, Self::Future<U>, U> + where + T: Send, + U: Send, + F: FnOnce(T) -> U + Send + 'a, + { + match future { + sealed::BoxedFuture::Box(future) => Self::wrap(async { func(future.await) }), + sealed::BoxedFuture::Ready(future) => { + let value = Spin::block_on(future); + Self::ready(func(value)) + } + } + } + fn wrap_boxed<'a, F>( future: core::pin::Pin<Box<F>>, ) -> SendFuture::T<'a, 'ctx, Self::Future<F::Output>, F::Output> @@ -169,8 +213,4 @@ impl<'ctx> Effect<'ctx> for Async { { 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)) - } } |