Skip to content

Commit

Permalink
task: implement FromIterator for JoinSet (#6300)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdesjardins authored Feb 2, 2024
1 parent cb197de commit 0b31b2a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tokio/src/task/join_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,49 @@ impl<T> Default for JoinSet<T> {
}
}

/// Collect an iterator of futures into a [`JoinSet`].
///
/// This is equivalent to calling [`JoinSet::spawn`] on each element of the iterator.
///
/// # Examples
///
/// The main example from [`JoinSet`]'s documentation can also be written using [`collect`]:
///
/// ```
/// use tokio::task::JoinSet;
///
/// #[tokio::main]
/// async fn main() {
/// let mut set: JoinSet<_> = (0..10).map(|i| async move { i }).collect();
///
/// let mut seen = [false; 10];
/// while let Some(res) = set.join_next().await {
/// let idx = res.unwrap();
/// seen[idx] = true;
/// }
///
/// for i in 0..10 {
/// assert!(seen[i]);
/// }
/// }
/// ```
///
/// [`collect`]: std::iter::Iterator::collect
impl<T, F> std::iter::FromIterator<F> for JoinSet<T>
where
F: Future<Output = T>,
F: Send + 'static,
T: Send + 'static,
{
fn from_iter<I: IntoIterator<Item = F>>(iter: I) -> Self {
let mut set = Self::new();
iter.into_iter().for_each(|task| {
set.spawn(task);
});
set
}
}

// === impl Builder ===

#[cfg(all(tokio_unstable, feature = "tracing"))]
Expand Down

0 comments on commit 0b31b2a

Please sign in to comment.