Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::future::Future;
use tokio::task::JoinError;
use tokio::task::JoinSet;

/// The default number of parallel tasks used by [ParallelTaskSet].
Expand Down Expand Up @@ -157,8 +158,9 @@ impl<T: 'static + Send> ParallelTaskSet<T> {
///
/// # Panics
///
/// This method panics if it must await the completion of a currently
/// running task, and that task returns a [`tokio::task::JoinError`].
/// If this method must await the completion of a currently running task,
/// and that task has panicked, this method propagates the panic (i.e. by
/// panicking itself with the same message as the panicked task).
///
/// [`max_parallelism`]: ParallelTaskSet#limiting-parallelism
/// [`join_next()`]: Self::join_next
Expand Down Expand Up @@ -188,9 +190,11 @@ impl<T: 'static + Send> ParallelTaskSet<T> {
///
/// # Panics
///
/// This method panics if the task returns a JoinError
/// If the next completed task has panicked, this method propagates the
/// panic (i.e. by panicking itself with the same message as the panicked
/// task).
pub async fn join_next(&mut self) -> Option<T> {
self.set.join_next().await.map(|r| r.expect("Failed to join task"))
self.set.join_next().await.map(unwrap_join_result)
}

/// Returns the output of a task that has completed, if there are any such
Expand All @@ -204,12 +208,13 @@ impl<T: 'static + Send> ParallelTaskSet<T> {
///
/// # Panics
///
/// This method panics if the next completed task has returned a
/// [`tokio::task::JoinError`].
/// If the next completed task has panicked, this method propagates the
/// panic (i.e. by panicking itself with the same message as the panicked
/// task).
///
/// [`join_next`]: Self::join_next
pub fn try_join_next(&mut self) -> Option<T> {
self.set.try_join_next().map(|r| r.expect("Failed to join task"))
self.set.try_join_next().map(unwrap_join_result)
}

/// Wait for all remaining tasks to complete and return their output.
Expand All @@ -220,7 +225,9 @@ impl<T: 'static + Send> ParallelTaskSet<T> {
///
/// # Panics
///
/// This method panics any of the tasks return a JoinError
/// If any of the remaining tasks have panicked, this method propagates the
/// panic (i.e. by panicking itself with the same message as the panicked
/// task).
pub async fn join_remaining(self) -> Vec<T> {
self.set.join_all().await
}
Expand Down Expand Up @@ -311,6 +318,13 @@ where
}
}

fn unwrap_join_result<T>(result: Result<T, JoinError>) -> T {
match result {
Ok(output) => output,
Err(err) => std::panic::resume_unwind(err.into_panic()),
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
Loading