Skip to content

Commit

Permalink
Implement Extend for JoinSet
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Mar 5, 2025
1 parent 0284d1b commit e68f366
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tokio/src/task/join_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,45 @@ where
}
}

/// Extend a [`JoinSet`] with futures from an iterator.
///
/// This is equivalent to calling [`JoinSet::spawn`] on each element of the iterator.
///
/// # Examples
///
/// ```
/// use tokio::task::JoinSet;
///
/// #[tokio::main]
/// async fn main() {
/// let mut set: JoinSet<_> = (0..5).map(|i| async move { i }).collect();
///
/// set.extend((5..10).map(|i| async move { i }));
///
/// 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]);
/// }
/// }
/// ```
impl<T, F> std::iter::Extend<F> for JoinSet<T>
where
F: Future<Output = T>,
F: Send + 'static,
T: Send + 'static,
{
fn extend<I: IntoIterator<Item = F>>(&mut self, iter: I) {
iter.into_iter().for_each(|task| {
self.spawn(task);
});
}
}

// === impl Builder ===

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

0 comments on commit e68f366

Please sign in to comment.