Diffstat (limited to 'src/effect.rs')
| -rw-r--r-- | src/effect.rs | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/src/effect.rs b/src/effect.rs index 9c72977..1d10cbc 100644 --- a/src/effect.rs +++ b/src/effect.rs @@ -22,7 +22,7 @@ higher_ranked_trait! { } /// Trait for effects. -pub trait Effect: Send + 'static { +pub trait Effect: Send + Sync + 'static { type Future<T: Send>: SendFuture::MemberType<T>; fn wrap<'a, F>(future: F) -> SendFuture::T<'a, F::Output, Self::Future<F::Output>> @@ -106,16 +106,36 @@ pub fn noop() -> Waker { unsafe { Waker::from_raw(RAW) } } +pub struct Ready<Output> { + pub value: Option<Output>, +} + +impl<Output> Ready<Output> { + pub fn into_innter(self) -> Output { + self.value.expect("`into_inner` called after completion") + } +} + +impl<Output> Unpin for Ready<Output> {} + +impl<Output> core::future::Future for Ready<Output> { + type Output = Output; + + fn poll(mut self: core::pin::Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { + Poll::Ready(self.value.take().expect("`Ready` polled after completion")) + } +} + higher_ranked_type! { impl SendFuture { - impl['a, Output] type T['a, Output] for core::future::Ready<Output> = - core::future::Ready<Output> + impl['a, Output] type T['a, Output] for Ready<Output> = + Ready<Output> where { Output: Send }; - impl['a, Output] type HigherRanked['a, Output] for core::future::Ready<Output> = - core::future::Ready<Output> + impl['a, Output] type HigherRanked['a, Output] for Ready<Output> = + Ready<Output> where { Output: Send }; @@ -123,18 +143,20 @@ higher_ranked_type! { } impl<B: BlockOn> Effect for Blocking<B> { - type Future<T: Send> = core::future::Ready<T>; + type Future<T: Send> = Ready<T>; 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, { - core::future::ready(B::block_on(future)) + Ready { + value: Some(B::block_on(future)), + } } fn ready<'a, T: Send>(value: T) -> SendFuture::T<'a, T, Self::Future<T>> { - core::future::ready(value) + Ready { value: Some(value) } } fn map<'a, T, U, F>( @@ -147,7 +169,9 @@ impl<B: BlockOn> Effect for Blocking<B> { F: FnOnce(T) -> U + Send + 'a, { let value = B::block_on(future); - core::future::ready(func(value)) + Ready { + value: Some(func(value)), + } } } |