orx_parallel/
into_par_iter.rs

1use crate::{Params, computational_variants::Par, runner::DefaultRunner};
2use orx_concurrent_iter::{ConcurrentIter, IntoConcurrentIter};
3
4/// Trait to convert a source (collection or generator) into a parallel iterator; i.e., [`ParIter`],
5/// using its [`into_par`] method.
6///
7/// It can be considered as the *concurrent counterpart* of the [`IntoIterator`] trait.
8///
9/// Note that every [`IntoConcurrentIter`] type automatically implements [`IntoParIter`].
10///
11/// [`into_par`]: crate::IntoParIter::into_par
12/// [`IntoConcurrentIter`]: orx_concurrent_iter::IntoConcurrentIter
13/// [`ParIter`]: crate::ParIter
14///
15/// # Examples
16///
17/// ```
18/// use orx_parallel::*;
19///
20/// // Vec<T>: IntoParIter<Item = T>
21/// let vec = vec![1, 2, 3, 4];
22/// assert_eq!(vec.into_par().max(), Some(4));
23///
24/// // Range<T>: IntoParIter<Item = T>
25/// let range = 1..5;
26/// assert_eq!(range.into_par().max(), Some(4));
27/// ```
28pub trait IntoParIter: IntoConcurrentIter {
29    /// Trait to convert a source (collection or generator) into a parallel iterator; i.e., [`ParIter`],
30    /// using its [`into_par`] method.
31    ///
32    /// It can be considered as the *concurrent counterpart* of the [`IntoIterator`] trait.
33    ///
34    /// [`into_par`]: crate::IntoParIter::into_par
35    /// [`ParIter`]: crate::ParIter
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// use orx_parallel::*;
41    ///
42    /// // Vec<T>: IntoParIter<Item = T>
43    /// let vec = vec![1, 2, 3, 4];
44    /// assert_eq!(vec.into_par().max(), Some(4));
45    ///
46    /// // Range<T>: IntoParIter<Item = T>
47    /// let range = 1..5;
48    /// assert_eq!(range.into_par().max(), Some(4));
49    /// ```
50    fn into_par(self) -> Par<Self::IntoIter, DefaultRunner>;
51}
52
53impl<I> IntoParIter for I
54where
55    I: IntoConcurrentIter,
56{
57    fn into_par(self) -> Par<Self::IntoIter, DefaultRunner> {
58        Par::new(Default::default(), Params::default(), self.into_con_iter())
59    }
60}
61
62impl<I: ConcurrentIter> IntoConcurrentIter for Par<I, DefaultRunner> {
63    type Item = I::Item;
64
65    type IntoIter = I;
66
67    fn into_con_iter(self) -> Self::IntoIter {
68        let (_, _, iter) = self.destruct();
69        iter
70    }
71}