(
items: Vec<I>,
max_concurrency: usize,
run: F,
)
| 44 | } |
| 45 | |
| 46 | pub(crate) async fn run_ordered_parallel_with_limit<I, F, Fut, T>( |
| 47 | items: Vec<I>, |
| 48 | max_concurrency: usize, |
| 49 | run: F, |
| 50 | ) -> Vec<OrderedParallelResult<T>> |
| 51 | where |
| 52 | I: Send + 'static, |
| 53 | F: Fn(usize, I) -> Fut + Clone + Send + 'static, |
| 54 | Fut: Future<Output = T> + Send + 'static, |
| 55 | T: Send + 'static, |
| 56 | { |
| 57 | let mut join_set = JoinSet::new(); |
| 58 | let mut pending = items.into_iter().enumerate(); |
| 59 | let max_concurrency = max_concurrency.max(1); |
| 60 | let mut active = 0usize; |
| 61 | |
| 62 | while active < max_concurrency { |
| 63 | let Some((index, item)) = pending.next() else { |
| 64 | break; |
| 65 | }; |
| 66 | let run = run.clone(); |
| 67 | join_set.spawn(async move { |
| 68 | let output = AssertUnwindSafe(run(index, item)) |
| 69 | .catch_unwind() |
| 70 | .await |
| 71 | .map_err(|payload| { |
| 72 | OrderedParallelError::Panicked(panic_payload_to_string(payload)) |
| 73 | }); |
| 74 | OrderedParallelResult { index, output } |
| 75 | }); |
| 76 | active += 1; |
| 77 | } |
| 78 | |
| 79 | let mut results = Vec::new(); |
| 80 | while active > 0 { |
| 81 | if let Some(result) = join_set.join_next().await { |
| 82 | active -= 1; |
| 83 | match result { |
| 84 | Ok(result) => results.push(result), |
| 85 | Err(error) => results.push(OrderedParallelResult { |
| 86 | index: usize::MAX, |
| 87 | output: Err(OrderedParallelError::JoinFailed(error.to_string())), |
| 88 | }), |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | while active < max_concurrency { |
| 93 | let Some((index, item)) = pending.next() else { |
| 94 | break; |
| 95 | }; |
| 96 | let run = run.clone(); |
| 97 | join_set.spawn(async move { |
| 98 | let output = AssertUnwindSafe(run(index, item)) |
| 99 | .catch_unwind() |
| 100 | .await |
| 101 | .map_err(|payload| { |
| 102 | OrderedParallelError::Panicked(panic_payload_to_string(payload)) |
| 103 | }); |
no test coverage detected