Skip to main content

orx_parallel/into_parallel/
into_par_iter.rs

1use crate::infallible::{ParIter, xap_variants::Id};
2use crate::runner::default_runner;
3use orx_concurrent_iter::IntoConcurrentIter;
4
5/// Converts values into a parallel iterator.
6///
7/// Sequential counterpart: [`IntoIterator::into_iter`](core::iter::IntoIterator::into_iter).
8pub trait IntoParIter: IntoConcurrentIter {
9    /// Consumes `self` and returns a parallel iterator over its items.
10    ///
11    /// # Example
12    ///
13    /// ```
14    /// use orx_parallel::*;
15    ///
16    /// let sum: i32 = vec![1, 2, 3, 4]
17    ///     .into_par()
18    ///     .map(|x| x * 2)
19    ///     .sum();
20    ///
21    /// assert_eq!(sum, 20);
22    /// ```
23    fn into_par(self) -> ParIter<Self::IntoIter, Id<Self::Item>>;
24}
25
26impl<I: IntoConcurrentIter> IntoParIter for I {
27    fn into_par(self) -> ParIter<Self::IntoIter, Id<Self::Item>> {
28        ParIter::new(
29            self.into_con_iter(),
30            Id::new(),
31            default_runner(),
32            Default::default(),
33        )
34    }
35}