1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/// 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)]
/// # futures::executor::block_on(async {
/// use futures::spawn;
///
/// let future = async { /* ... */ };
/// spawn!(future).unwrap();
/// # });
/// ```
#[macro_export]
macro_rules! spawn {
    ($future:expr) => {
        await!($crate::future::lazy(|cx| {
            $crate::task::SpawnExt::spawn(cx.spawner(), $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)]
/// # futures::executor::block_on(async {
/// use futures::{future, spawn_with_handle};
///
/// 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| {
            $crate::task::SpawnExt::spawn_with_handle(cx.spawner(), $future)
        }))
    }
}