diff --git a/src/lib.rs b/src/lib.rs index 7258e74..989de8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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]. @@ -157,8 +158,9 @@ impl ParallelTaskSet { /// /// # 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 @@ -188,9 +190,11 @@ impl ParallelTaskSet { /// /// # 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 { - 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 @@ -204,12 +208,13 @@ impl ParallelTaskSet { /// /// # 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 { - 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. @@ -220,7 +225,9 @@ impl ParallelTaskSet { /// /// # 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 { self.set.join_all().await } @@ -311,6 +318,13 @@ where } } +fn unwrap_join_result(result: Result) -> T { + match result { + Ok(output) => output, + Err(err) => std::panic::resume_unwind(err.into_panic()), + } +} + #[cfg(test)] mod test { use super::*;