orx_parallel/generic_iterator/
iter.rs

1use crate::ParIter;
2
3/// An iterator that generalizes over:
4///
5/// * sequential iterators,
6/// * rayon's parallel iterators, and
7/// * orx-parallel's parallel iterators.
8///
9/// This is particularly useful for enabling a convenient way to run experiments
10/// using these different computation approaches.
11pub enum GenericIterator<T, S, R, O>
12where
13    T: Send + Sync,
14    S: Iterator<Item = T>,
15    R: rayon::iter::ParallelIterator<Item = T>,
16    O: ParIter<Item = T>,
17{
18    /// Sequential, or regular, iterator.
19    Sequential(S),
20    /// rayon's parallel iterator.
21    Rayon(R),
22    /// orx-parallel's parallel iterator.
23    Orx(O),
24}
25
26impl<T, S> GenericIterator<T, S, rayon::iter::Empty<T>, crate::iter::ParEmpty<T>>
27where
28    T: Send + Sync,
29    S: Iterator<Item = T>,
30{
31    /// Creates the generic iterator from sequential iterator variant.
32    pub fn sequential(iter: S) -> Self {
33        Self::Sequential(iter)
34    }
35}
36
37impl<T, R> GenericIterator<T, core::iter::Empty<T>, R, crate::iter::ParEmpty<T>>
38where
39    T: Send + Sync,
40    R: rayon::iter::ParallelIterator<Item = T>,
41{
42    /// Creates the generic iterator from rayon iterator variant.
43    pub fn rayon(iter: R) -> Self {
44        Self::Rayon(iter)
45    }
46}
47
48impl<T, O> GenericIterator<T, core::iter::Empty<T>, rayon::iter::Empty<T>, O>
49where
50    T: Send + Sync,
51    O: ParIter<Item = T>,
52{
53    /// Creates the generic iterator from orx-parallel iterator variant.
54    pub fn orx(iter: O) -> Self {
55        Self::Orx(iter)
56    }
57}