hexput_ast_api/
parallel.rs

1use std::future::Future;
2use tokio::runtime::Runtime;
3use tokio::task::JoinSet;
4
5
6pub fn create_runtime() -> Runtime {
7    Runtime::new().expect("Failed to create Tokio runtime")
8}
9
10
11pub async fn process_items<T, U, F, Fut>(
12    items: Vec<T>,
13    processor: F,
14) -> Vec<U>
15where
16    T: Send + 'static,
17    U: Send + 'static,
18    F: Fn(T) -> Fut + Send + Sync + Clone + 'static,
19    Fut: Future<Output = U> + Send,
20{
21    
22    if items.len() <= 1 {
23        let mut results = Vec::with_capacity(items.len());
24        for item in items {
25            results.push(processor(item).await);
26        }
27        return results;
28    }
29
30    let mut join_set = JoinSet::new();
31
32    
33    for item in items {
34        let processor_clone = processor.clone();
35        join_set.spawn(async move {
36            processor_clone(item).await
37        });
38    }
39
40    
41    let mut results = Vec::with_capacity(join_set.len());
42    while let Some(result) = join_set.join_next().await {
43        if let Ok(value) = result {
44            results.push(value);
45        }
46    }
47
48    results
49}
50
51
52
53pub fn process_items_sync<T, U, F>(runtime: &Runtime, items: Vec<T>, processor: F) -> Vec<U>
54where
55    T: Send + 'static,
56    U: Send + 'static,
57    F: Fn(T, &Runtime) -> U + Send + Sync + Clone + 'static,
58{
59    
60    if items.len() <= 1 {
61        return items.into_iter().map(|item| processor(item, runtime)).collect();
62    }
63    
64    
65    let mut handles = Vec::with_capacity(items.len());
66    
67    
68    for item in items {
69        
70        let processor_clone = processor.clone();
71        
72        
73        let handle = runtime.spawn_blocking(move || {
74            
75            
76            (item, processor_clone)
77        });
78        
79        handles.push(handle);
80    }
81    
82    
83    let mut results = Vec::with_capacity(handles.len());
84    for handle in handles {
85        
86        if let Ok((item, processor_fn)) = runtime.block_on(handle) {
87            
88            let result = processor_fn(item, runtime);
89            results.push(result);
90        }
91    }
92    
93    results
94}