use async_trait::async_trait; use std::future::Future; use tokio::task::JoinError; #[async_trait] pub trait ParSpawnAndAwait { type Awaited; async fn par_spawn_and_await(self) -> Result; } #[async_trait] impl ParSpawnAndAwait for Vec where R: Send + 'static, F: Future + Send + 'static, { type Awaited = Vec; async fn par_spawn_and_await(self) -> Result { let handles: Vec<_> = self.into_iter().map(|f| tokio::spawn(f)).collect(); let mut results = Vec::new(); for handle in handles { results.push(handle.await?); } Ok(results) } }