-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ExecutorExt, spawn! and spawn_with_handle!
- Loading branch information
1 parent
43bfbbe
commit 1aa6705
Showing
11 changed files
with
304 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/// 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`] 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`], or, if | ||
/// spawning fails, a [`SpawnError`]. [`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| { | ||
use $crate::task::ExecutorExt; | ||
cx.executor().spawn_with_handle($future) | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.