diff --git a/benches/baseline.rs b/benches/baseline.rs index 0f15d20e..83eb49cd 100644 --- a/benches/baseline.rs +++ b/benches/baseline.rs @@ -4,6 +4,7 @@ extern crate test; mod baseline { use futures::executor; + use futures::future::RemoteHandle; use futures::prelude::*; use std::pin::Pin; use std::task::{Context, Poll}; @@ -100,7 +101,7 @@ mod baseline { } })) .map(|_: Result<(), ()>| ()), - ) + ).await }) }) .collect::>(); @@ -113,38 +114,15 @@ mod baseline { } /// Spawn function for juliex to get back a handle - pub fn spawn(fut: F) -> JoinHandle + pub fn spawn(fut: F) -> RemoteHandle where F: Future + Send + 'static, T: Send + 'static, { - let (tx, rx) = futures::channel::oneshot::channel(); - - let fut = async move { - let t = fut.await; - let _ = tx.send(t); - }; + let (fut, handle) = fut.remote_handle(); juliex::spawn(fut); - JoinHandle { rx } - } - - /// Handle returned from Juliex. - // We should patch Juliex to support this natively, and be more efficient on channel use. - #[derive(Debug)] - pub struct JoinHandle { - pub(crate) rx: futures::channel::oneshot::Receiver, - } - - impl Future for JoinHandle { - type Output = T; - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.rx.poll_unpin(cx) { - Poll::Pending => Poll::Pending, - Poll::Ready(Ok(t)) => Poll::Ready(t), - Poll::Ready(Err(_)) => panic!(), // TODO: Is this OK? Print a better error message? - } - } + handle } } diff --git a/benches/common/mod.rs b/benches/common/mod.rs index a8c136fd..e8ba2529 100644 --- a/benches/common/mod.rs +++ b/benches/common/mod.rs @@ -83,7 +83,7 @@ macro_rules! benchmark_suite { } })) .map(|_: Result<(), ()>| ()), - ) + ).await }) }) .collect::>(); diff --git a/src/task.rs b/src/task.rs index c30ad55b..c8a5c29b 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,10 +1,8 @@ //! Types and Functions for working with asynchronous tasks. -use std::pin::Pin; - -use futures::future::FutureObj; +use futures::future::{FutureObj, RemoteHandle}; use futures::prelude::*; -use futures::task::{Context, Poll, Spawn, SpawnError}; +use futures::task::{Spawn, SpawnError}; /// A [`Spawn`] handle to runtime's thread pool for spawning futures. /// @@ -63,42 +61,16 @@ impl<'a> Spawn for &'a Spawner { /// assert_eq!(handle.await, 42); /// } /// ``` -pub fn spawn(fut: F) -> JoinHandle +pub fn spawn(fut: F) -> RemoteHandle where F: Future + Send + 'static, T: Send + 'static, { - let (tx, rx) = futures::channel::oneshot::channel(); - - let fut = async move { - let t = fut.await; - let _ = tx.send(t); - }; + let (fut, handle) = fut.remote_handle(); runtime_raw::current_runtime() .spawn_boxed(fut.boxed()) .expect("cannot spawn a future"); - JoinHandle { rx } -} - -/// A handle that awaits the result of a [`spawn`]ed future. -/// -/// [`spawn`]: fn.spawn.html -#[must_use = "futures do nothing unless you `.await` or poll them"] -#[derive(Debug)] -pub struct JoinHandle { - pub(crate) rx: futures::channel::oneshot::Receiver, -} - -impl Future for JoinHandle { - type Output = T; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.rx.poll_unpin(cx) { - Poll::Pending => Poll::Pending, - Poll::Ready(Ok(t)) => Poll::Ready(t), - Poll::Ready(Err(_)) => panic!(), // TODO: Is this OK? Print a better error message? - } - } + handle }