orx_parallel/runner/implementations/
sequential.rs

1use crate::ParThreadPool;
2use core::num::NonZeroUsize;
3
4/// A 'thread pool' with [`max_num_threads`] of 1.
5/// All computations using this thread pool are executed sequentially by the main thread.
6///
7/// This is the default thread pool used when "std" feature is disabled.
8/// Note that the thread pool to be used for a parallel computation can be set by the
9/// [`with_runner`] transformation separately for each parallel iterator.
10///
11/// [`max_num_threads`]: ParThreadPool::max_num_threads
12/// [`with_runner`]: crate::ParIter::with_runner
13#[derive(Default)]
14pub struct SequentialPool;
15
16impl ParThreadPool for SequentialPool {
17    type ScopeRef<'s, 'env, 'scope>
18        = ()
19    where
20        'scope: 's,
21        'env: 'scope + 's;
22
23    fn run_in_scope<'s, 'env, 'scope, W>(_: &Self::ScopeRef<'s, 'env, 'scope>, work: W)
24    where
25        'scope: 's,
26        'env: 'scope + 's,
27        W: Fn() + Send + 'scope + 'env,
28    {
29        work()
30    }
31
32    fn scoped_computation<'env, 'scope, F>(&'env mut self, f: F)
33    where
34        'env: 'scope,
35        for<'s> F: FnOnce(()) + Send,
36    {
37        f(())
38    }
39
40    fn max_num_threads(&self) -> NonZeroUsize {
41        NonZeroUsize::new(1).expect(">0")
42    }
43}