Skip to content

Commit 80720aa

Browse files
Merge pull request #1156 from MajorBreakfast/spawn
ExecutorExt, spawn! and spawn_with_handle!
2 parents 3d0fd79 + fccf47c commit 80720aa

File tree

11 files changed

+304
-239
lines changed

11 files changed

+304
-239
lines changed

futures-executor/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,4 @@ if_std! {
3333

3434
mod enter;
3535
pub use crate::enter::{enter, Enter, EnterError};
36-
37-
mod spawn;
38-
pub use crate::spawn::{spawn, Spawn, spawn_with_handle, SpawnWithHandle, JoinHandle};
3936
}

futures-executor/src/spawn.rs

Lines changed: 0 additions & 186 deletions
This file was deleted.

futures-util/src/async_await/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ mod join;
2020
// Primary export is a macro
2121
mod select;
2222

23+
// Primary export is a macro
24+
mod spawn;
25+
2326
#[doc(hidden)]
2427
#[inline(always)]
2528
pub fn assert_unpin<T: Future + Unpin>(_: &T) {}

futures-util/src/async_await/spawn.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/// Spawns a task onto the context's executor that polls the given future with
2+
/// output `()` to completion.
3+
///
4+
/// This macro returns a [`Result`] that contains a
5+
/// [`SpawnError`](crate::task::SpawnError) if spawning fails.
6+
///
7+
/// You can use [`spawn_with_handle!`] if you want to spawn a future
8+
/// with output other than `()` or if you want to be able to await its
9+
/// completion.
10+
///
11+
/// ```
12+
/// #![feature(async_await, await_macro, futures_api)]
13+
/// #[macro_use] extern crate futures;
14+
/// # futures::executor::block_on(async {
15+
///
16+
/// let future = async { /* ... */ };
17+
/// spawn!(future).unwrap();
18+
/// # });
19+
/// ```
20+
#[macro_export]
21+
macro_rules! spawn {
22+
($future:expr) => {
23+
await!($crate::future::lazy(|cx| {
24+
use $crate::task::ExecutorExt;
25+
cx.executor().spawn($future)
26+
}))
27+
}
28+
}
29+
30+
/// Spawns a task onto the context's executor that polls the given future to
31+
/// completion and returns a future that resolves to the spawned future's
32+
/// output.
33+
///
34+
/// This macro returns a [`Result`] that contains a
35+
/// [`JoinHandle`](crate::task::JoinHandle), or, if spawning fails, a
36+
/// [`SpawnError`](crate::task::SpawnError).
37+
/// [`JoinHandle`](crate::task::JoinHandle) is a future that resolves
38+
/// to the output of the spawned future
39+
///
40+
/// # Examples
41+
///
42+
/// ```
43+
/// #![feature(async_await, await_macro, futures_api)]
44+
/// #[macro_use] extern crate futures;
45+
/// # futures::executor::block_on(async {
46+
/// use futures::future;
47+
///
48+
/// let future = future::ready(1);
49+
/// let join_handle = spawn_with_handle!(future).unwrap();
50+
/// assert_eq!(await!(join_handle), 1);
51+
/// # });
52+
/// ```
53+
#[macro_export]
54+
macro_rules! spawn_with_handle {
55+
($future:expr) => {
56+
await!($crate::future::lazy(|cx| {
57+
use $crate::task::ExecutorExt;
58+
cx.executor().spawn_with_handle($future)
59+
}))
60+
}
61+
}

futures-util/src/future/mod.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -659,16 +659,30 @@ pub trait FutureExt: Future {
659659
///
660660
/// ```
661661
/// #![feature(async_await, await_macro, futures_api)]
662+
/// #[macro_use] extern crate futures;
662663
/// # futures::executor::block_on(async {
663-
/// use futures::executor::{spawn_with_handle, ThreadPool};
664-
/// use futures::future::{self, FutureExt};
664+
/// use futures::executor::ThreadPool;
665+
/// use futures::future::FutureExt;
666+
/// use std::thread;
667+
/// # let (tx, rx) = futures::channel::oneshot::channel();
665668
///
666-
/// let pool = ThreadPool::new().expect("unable to create threadpool");
667-
/// let future = spawn_with_handle(future::ready(3))
668-
/// .with_executor(pool)
669-
/// .flatten();
670-
/// assert_eq!(await!(future), 3);
671-
/// # });
669+
/// let pool = ThreadPool::builder()
670+
/// .name_prefix("my-pool-")
671+
/// .pool_size(1)
672+
/// .create().unwrap();
673+
///
674+
/// let val = await!((async {
675+
/// assert_ne!(thread::current().name(), Some("my-pool-0"));
676+
///
677+
/// // Spawned task runs on the executor specified via `with_executor`
678+
/// spawn!(async {
679+
/// assert_eq!(thread::current().name(), Some("my-pool-0"));
680+
/// # tx.send("ran").unwrap();
681+
/// }).unwrap();
682+
/// }).with_executor(pool));
683+
///
684+
/// # assert_eq!(await!(rx), Ok("ran"))
685+
/// # })
672686
/// ```
673687
fn with_executor<E>(self, executor: E) -> WithExecutor<Self, E>
674688
where Self: Sized,

futures-util/src/task/context.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)