Skip to main content

ai_agent/utils/
sequential.rs

1// Source: /data/home/swei/claudecode/openclaudecode/src/utils/sequential.ts
2//! Sequential execution utilities for running async functions in order.
3
4use std::future::Future;
5use std::pin::Pin;
6
7/// Run async functions sequentially, waiting for each to complete before starting the next.
8pub async fn sequential<T, R, F>(items: T, mut f: F) -> Vec<R>
9where
10    T: IntoIterator<Item = R>,
11    F: FnMut(R) -> Pin<Box<dyn Future<Output = R> + Send>>,
12    R: std::fmt::Debug,
13{
14    let mut results = Vec::new();
15
16    for item in items {
17        let result = f(item).await;
18        results.push(result);
19    }
20
21    results
22}
23
24/// Run async functions sequentially with index, waiting for each to complete.
25pub async fn sequential_with_index<T, R, F>(items: T, mut f: F) -> Vec<R>
26where
27    T: IntoIterator<Item = R>,
28    F: FnMut(R, usize) -> Pin<Box<dyn Future<Output = R> + Send>>,
29    R: std::fmt::Debug,
30{
31    let mut results = Vec::new();
32
33    for (index, item) in items.into_iter().enumerate() {
34        let result = f(item, index).await;
35        results.push(result);
36    }
37
38    results
39}