Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExecutorExt, spawn! and spawn_with_handle! #1156

Merged
merged 3 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions futures-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,4 @@ if_std! {

mod enter;
pub use crate::enter::{enter, Enter, EnterError};

mod spawn;
pub use crate::spawn::{spawn, Spawn, spawn_with_handle, SpawnWithHandle, JoinHandle};
}
186 changes: 0 additions & 186 deletions futures-executor/src/spawn.rs

This file was deleted.

3 changes: 3 additions & 0 deletions futures-util/src/async_await/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ mod join;
// Primary export is a macro
mod select;

// Primary export is a macro
mod spawn;

#[doc(hidden)]
#[inline(always)]
pub fn assert_unpin<T: Future + Unpin>(_: &T) {}
61 changes: 61 additions & 0 deletions futures-util/src/async_await/spawn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/// Spawns a task onto the context's executor that polls the given future with
/// output `()` to completion.
///
/// This macro returns a [`Result`] that contains a
/// [`SpawnError`](crate::task::SpawnError) if spawning fails.
///
/// You can use [`spawn_with_handle!`] if you want to spawn a future
/// with output other than `()` or if you want to be able to await its
/// completion.
///
/// ```
/// #![feature(async_await, await_macro, futures_api)]
/// #[macro_use] extern crate futures;
/// # futures::executor::block_on(async {
///
/// let future = async { /* ... */ };
/// spawn!(future).unwrap();
/// # });
/// ```
#[macro_export]
macro_rules! spawn {
($future:expr) => {
await!($crate::future::lazy(|cx| {
use $crate::task::ExecutorExt;
cx.executor().spawn($future)
}))
}
}

/// Spawns a task onto the context's executor that polls the given future to
/// completion and returns a future that resolves to the spawned future's
/// output.
///
/// This macro returns a [`Result`] that contains a
/// [`JoinHandle`](crate::task::JoinHandle), or, if spawning fails, a
/// [`SpawnError`](crate::task::SpawnError).
/// [`JoinHandle`](crate::task::JoinHandle) is a future that resolves
/// to the output of the spawned future
///
/// # Examples
///
/// ```
/// #![feature(async_await, await_macro, futures_api)]
/// #[macro_use] extern crate futures;
/// # futures::executor::block_on(async {
/// use futures::future;
///
/// let future = future::ready(1);
/// let join_handle = spawn_with_handle!(future).unwrap();
/// assert_eq!(await!(join_handle), 1);
/// # });
/// ```
#[macro_export]
macro_rules! spawn_with_handle {
($future:expr) => {
await!($crate::future::lazy(|cx| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$crate::core_reexport::await! maybe? (probably not, can just rely on await being in scope).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, it's probably a builtin macro rather than a core defined macro anyway

Copy link
Contributor Author

@MajorBreakfast MajorBreakfast Aug 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other macros (poll!, pending!) do it this way as well.

I don't know where await! comes from, but I don't think it's from libcore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in libstd right now, but it'll probably be moved into the compiler itself as a builtin and referenced from libcore at some point. I wouldn't worry about scoping it.

use $crate::task::ExecutorExt;
cx.executor().spawn_with_handle($future)
}))
}
}
30 changes: 22 additions & 8 deletions futures-util/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,30 @@ pub trait FutureExt: Future {
///
/// ```
/// #![feature(async_await, await_macro, futures_api)]
/// #[macro_use] extern crate futures;
/// # futures::executor::block_on(async {
/// use futures::executor::{spawn_with_handle, ThreadPool};
/// use futures::future::{self, FutureExt};
/// use futures::executor::ThreadPool;
/// use futures::future::FutureExt;
/// use std::thread;
/// # let (tx, rx) = futures::channel::oneshot::channel();
///
/// let pool = ThreadPool::new().expect("unable to create threadpool");
/// let future = spawn_with_handle(future::ready(3))
/// .with_executor(pool)
/// .flatten();
/// assert_eq!(await!(future), 3);
/// # });
/// let pool = ThreadPool::builder()
/// .name_prefix("my-pool-")
/// .pool_size(1)
/// .create().unwrap();
///
/// let val = await!((async {
/// assert_ne!(thread::current().name(), Some("my-pool-0"));
///
/// // Spawned task runs on the executor specified via `with_executor`
/// spawn!(async {
/// assert_eq!(thread::current().name(), Some("my-pool-0"));
/// # tx.send("ran").unwrap();
/// }).unwrap();
/// }).with_executor(pool));
///
/// # assert_eq!(await!(rx), Ok("ran"))
/// # })
/// ```
fn with_executor<E>(self, executor: E) -> WithExecutor<Self, E>
where Self: Sized,
Expand Down
29 changes: 0 additions & 29 deletions futures-util/src/task/context.rs

This file was deleted.

Loading