hermes_async_runtime_components/task/impls/
concurrent.rs

1use alloc::boxed::Box;
2use alloc::vec::Vec;
3
4use futures_util::stream::{self, Stream, StreamExt};
5use hermes_runtime_components::traits::task::{ConcurrentTaskRunner, Task};
6
7use crate::stream::traits::boxed::HasBoxedStreamType;
8
9pub struct RunConcurrentTasks;
10
11impl<Runtime> ConcurrentTaskRunner<Runtime> for RunConcurrentTasks
12where
13    Runtime: HasBoxedStreamType,
14{
15    async fn run_concurrent_tasks<T>(_runtime: &Runtime, tasks: Vec<T>)
16    where
17        T: Task,
18    {
19        run_concurrent_tasks(stream::iter(tasks)).await
20    }
21
22    async fn run_concurrent_task_stream<T>(_runtime: &Runtime, tasks: Runtime::Stream<T>)
23    where
24        T: Task,
25    {
26        run_concurrent_tasks(Runtime::to_boxed_stream(tasks)).await
27    }
28}
29pub async fn run_concurrent_tasks<T>(tasks: impl Stream<Item = T>)
30where
31    T: Task,
32{
33    tasks
34        .for_each_concurrent(None, |task| Box::pin(async move { task.run().await }))
35        .await;
36}