turbo_persistence/
parallel_scheduler.rs

1pub trait ParallelScheduler: Clone + Sync + Send {
2    fn block_in_place<R>(&self, f: impl FnOnce() -> R + Send) -> R
3    where
4        R: Send;
5
6    fn parallel_for_each<T>(&self, items: &[T], f: impl Fn(&T) + Send + Sync)
7    where
8        T: Sync;
9
10    fn try_parallel_for_each<'l, T, E>(
11        &self,
12        items: &'l [T],
13        f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
14    ) -> Result<(), E>
15    where
16        T: Sync,
17        E: Send + 'static;
18
19    fn try_parallel_for_each_mut<'l, T, E>(
20        &self,
21        items: &'l mut [T],
22        f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
23    ) -> Result<(), E>
24    where
25        T: Send + Sync,
26        E: Send + 'static;
27
28    fn try_parallel_for_each_owned<T, E>(
29        &self,
30        items: Vec<T>,
31        f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
32    ) -> Result<(), E>
33    where
34        T: Send + Sync,
35        E: Send + 'static;
36
37    fn parallel_map_collect<'l, Item, PerItemResult, Result>(
38        &self,
39        items: &'l [Item],
40        f: impl Fn(&'l Item) -> PerItemResult + Send + Sync,
41    ) -> Result
42    where
43        Item: Sync,
44        PerItemResult: Send + Sync + 'l,
45        Result: FromIterator<PerItemResult>;
46
47    fn parallel_map_collect_owned<Item, PerItemResult, Result>(
48        &self,
49        items: Vec<Item>,
50        f: impl Fn(Item) -> PerItemResult + Send + Sync,
51    ) -> Result
52    where
53        Item: Send + Sync,
54        PerItemResult: Send + Sync,
55        Result: FromIterator<PerItemResult>;
56}
57
58#[derive(Clone, Copy, Default)]
59pub struct SerialScheduler;
60
61impl ParallelScheduler for SerialScheduler {
62    fn block_in_place<R>(&self, f: impl FnOnce() -> R + Send) -> R
63    where
64        R: Send,
65    {
66        f()
67    }
68
69    fn parallel_for_each<T>(&self, items: &[T], f: impl Fn(&T) + Send + Sync)
70    where
71        T: Sync,
72    {
73        for item in items {
74            f(item);
75        }
76    }
77
78    fn try_parallel_for_each<'l, T, E>(
79        &self,
80        items: &'l [T],
81        f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
82    ) -> Result<(), E>
83    where
84        T: Sync,
85        E: Send,
86    {
87        for item in items {
88            f(item)?;
89        }
90        Ok(())
91    }
92
93    fn try_parallel_for_each_mut<'l, T, E>(
94        &self,
95        items: &'l mut [T],
96        f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
97    ) -> Result<(), E>
98    where
99        T: Sync,
100        E: Send,
101    {
102        for item in items {
103            f(item)?;
104        }
105        Ok(())
106    }
107
108    fn try_parallel_for_each_owned<T, E>(
109        &self,
110        items: Vec<T>,
111        f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
112    ) -> Result<(), E>
113    where
114        T: Sync,
115        E: Send,
116    {
117        for item in items {
118            f(item)?;
119        }
120        Ok(())
121    }
122
123    fn parallel_map_collect<'l, Item, PerItemResult, Result>(
124        &self,
125        items: &'l [Item],
126        f: impl Fn(&'l Item) -> PerItemResult + Send + Sync,
127    ) -> Result
128    where
129        Item: Sync,
130        PerItemResult: Send + Sync + 'l,
131        Result: FromIterator<PerItemResult>,
132    {
133        items.iter().map(f).collect()
134    }
135
136    fn parallel_map_collect_owned<Item, PerItemResult, Result>(
137        &self,
138        items: Vec<Item>,
139        f: impl Fn(Item) -> PerItemResult + Send + Sync,
140    ) -> Result
141    where
142        Item: Send + Sync,
143        PerItemResult: Send + Sync,
144        Result: FromIterator<PerItemResult>,
145    {
146        items.into_iter().map(f).collect()
147    }
148}