async_macros/join.rs
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
#![allow(non_snake_case)]
/// Awaits multiple futures simultaneously, returning all results once complete.
///
/// While `join!(a, b)` is similar to `(a.await, b.await)`,
/// `join!` polls both futures concurrently and therefore is more efficent.
///
/// This macro is only usable inside of async functions, closures, and blocks.
///
/// # Examples
///
/// ```
/// #![feature(async_await)]
/// # futures::executor::block_on(async {
/// use async_macros::join;
/// use futures::future;
///
/// let a = future::ready(1u8);
/// let b = future::ready(2u8);
///
/// assert_eq!(join!(a, b).await, (1, 2));
/// # });
/// ```
#[macro_export]
macro_rules! join {
($($fut:ident),* $(,)?) => { {
async {
$(
// Move future into a local so that it is pinned in one place and
// is no longer accessible by the end user.
let mut $fut = $crate::MaybeDone::new($fut);
)*
$crate::utils::poll_fn(move |cx| {
use $crate::utils::future::Future;
use $crate::utils::task::Poll;
use $crate::utils::pin::Pin;
let mut all_done = true;
$(
let fut = unsafe { Pin::new_unchecked(&mut $fut) };
all_done &= Future::poll(fut, cx).is_ready();
)*
if all_done {
Poll::Ready(($(
unsafe { Pin::new_unchecked(&mut $fut) }.take().unwrap(),
)*))
} else {
Poll::Pending
}
}).await
}
} }
}