|
| 1 | +#![allow(unused_imports)] // items are used by the macro |
| 2 | + |
| 3 | +use crate::cell::UnsafeCell; |
| 4 | +use crate::future::{poll_fn, Future}; |
| 5 | +use crate::mem; |
| 6 | +use crate::pin::Pin; |
| 7 | +use crate::task::{Context, Poll}; |
| 8 | + |
| 9 | +/// Polls multiple futures simultaneously, returning a tuple |
| 10 | +/// of all results once complete. |
| 11 | +/// |
| 12 | +/// While `join!(a, b)` is similar to `(a.await, b.await)`, |
| 13 | +/// `join!` polls both futures concurrently and is therefore more efficient. |
| 14 | +/// |
| 15 | +/// # Examples |
| 16 | +/// |
| 17 | +/// ``` |
| 18 | +/// #![feature(future_join, future_poll_fn)] |
| 19 | +/// |
| 20 | +/// use std::future::join; |
| 21 | +/// |
| 22 | +/// async fn one() -> usize { 1 } |
| 23 | +/// async fn two() -> usize { 2 } |
| 24 | +/// |
| 25 | +/// # let _ = async { |
| 26 | +/// let x = join!(one(), two()).await; |
| 27 | +/// assert_eq!(x, (1, 2)); |
| 28 | +/// # }; |
| 29 | +/// ``` |
| 30 | +/// |
| 31 | +/// `join!` is variadic, so you can pass any number of futures: |
| 32 | +/// |
| 33 | +/// ``` |
| 34 | +/// #![feature(future_join, future_poll_fn)] |
| 35 | +/// |
| 36 | +/// use std::future::join; |
| 37 | +/// |
| 38 | +/// async fn one() -> usize { 1 } |
| 39 | +/// async fn two() -> usize { 2 } |
| 40 | +/// async fn three() -> usize { 3 } |
| 41 | +/// |
| 42 | +/// # let _ = async { |
| 43 | +/// let x = join!(one(), two(), three()).await; |
| 44 | +/// assert_eq!(x, (1, 2, 3)); |
| 45 | +/// # }; |
| 46 | +/// ``` |
| 47 | +#[unstable(feature = "future_join", issue = "91642")] |
| 48 | +pub macro join { |
| 49 | + ( $($fut:expr),* $(,)?) => { |
| 50 | + join! { @count: (), @futures: {}, @rest: ($($fut,)*) } |
| 51 | + }, |
| 52 | + // Recurse until we have the position of each future in the tuple |
| 53 | + ( |
| 54 | + // A token for each future that has been expanded: "_ _ _" |
| 55 | + @count: ($($count:tt)*), |
| 56 | + // Futures and their positions in the tuple: "{ a => (_), b => (_ _)) }" |
| 57 | + @futures: { $($fut:tt)* }, |
| 58 | + // Take a future from @rest to expand |
| 59 | + @rest: ($current:expr, $($rest:tt)*) |
| 60 | + ) => { |
| 61 | + join! { |
| 62 | + @count: ($($count)* _), |
| 63 | + @futures: { $($fut)* $current => ($($count)*), }, |
| 64 | + @rest: ($($rest)*) |
| 65 | + } |
| 66 | + }, |
| 67 | + // Now generate the output future |
| 68 | + ( |
| 69 | + @count: ($($count:tt)*), |
| 70 | + @futures: { |
| 71 | + $( $(@$f:tt)? $fut:expr => ( $($pos:tt)* ), )* |
| 72 | + }, |
| 73 | + @rest: () |
| 74 | + ) => { |
| 75 | + async move { |
| 76 | + let mut futures = ( $( MaybeDone::Future($fut), )* ); |
| 77 | + |
| 78 | + poll_fn(move |cx| { |
| 79 | + let mut done = true; |
| 80 | + |
| 81 | + $( |
| 82 | + let ( $($pos,)* fut, .. ) = &mut futures; |
| 83 | + |
| 84 | + // SAFETY: The futures are never moved |
| 85 | + done &= unsafe { Pin::new_unchecked(fut).poll(cx).is_ready() }; |
| 86 | + )* |
| 87 | + |
| 88 | + if done { |
| 89 | + // Extract all the outputs |
| 90 | + Poll::Ready(($({ |
| 91 | + let ( $($pos,)* fut, .. ) = &mut futures; |
| 92 | + |
| 93 | + fut.take_output().unwrap() |
| 94 | + }),*)) |
| 95 | + } else { |
| 96 | + Poll::Pending |
| 97 | + } |
| 98 | + }).await |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// Future used by `join!` that stores it's output to |
| 104 | +/// be later taken and doesn't panic when polled after ready. |
| 105 | +/// |
| 106 | +/// This type is public in a private module for use by the macro. |
| 107 | +#[allow(missing_debug_implementations)] |
| 108 | +#[unstable(feature = "future_join", issue = "91642")] |
| 109 | +pub enum MaybeDone<F: Future> { |
| 110 | + Future(F), |
| 111 | + Done(F::Output), |
| 112 | + Took, |
| 113 | +} |
| 114 | + |
| 115 | +#[unstable(feature = "future_join", issue = "91642")] |
| 116 | +impl<F: Future> MaybeDone<F> { |
| 117 | + pub fn take_output(&mut self) -> Option<F::Output> { |
| 118 | + match &*self { |
| 119 | + MaybeDone::Done(_) => match mem::replace(self, Self::Took) { |
| 120 | + MaybeDone::Done(val) => Some(val), |
| 121 | + _ => unreachable!(), |
| 122 | + }, |
| 123 | + _ => None, |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#[unstable(feature = "future_join", issue = "91642")] |
| 129 | +impl<F: Future> Future for MaybeDone<F> { |
| 130 | + type Output = (); |
| 131 | + |
| 132 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 133 | + // SAFETY: pinning in structural for `f` |
| 134 | + unsafe { |
| 135 | + match self.as_mut().get_unchecked_mut() { |
| 136 | + MaybeDone::Future(f) => match Pin::new_unchecked(f).poll(cx) { |
| 137 | + Poll::Ready(val) => self.set(Self::Done(val)), |
| 138 | + Poll::Pending => return Poll::Pending, |
| 139 | + }, |
| 140 | + MaybeDone::Done(_) => {} |
| 141 | + MaybeDone::Took => unreachable!(), |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + Poll::Ready(()) |
| 146 | + } |
| 147 | +} |
0 commit comments