Skip to main content

orx_parallel/into_parallel/
par_collection.rs

1use crate::infallible::{ParIter, xap_variants::Id};
2use crate::runner::default_runner;
3use orx_concurrent_iter::{ConcurrentCollection, ConcurrentIterable};
4
5/// A collection from which a parallel iterator can be created repeatedly
6/// using `par()` method.
7///
8/// Sequential counterpart: `iter()`.
9pub trait ParCollection: ConcurrentCollection {
10    /// Returns a parallel iterator over shared references to collection items.
11    ///
12    /// # Example
13    ///
14    /// ```
15    /// use orx_parallel::*;
16    ///
17    /// let values = vec![1, 2, 3, 4];
18    ///
19    /// let sum: i32 = ParCollection::par(&values).copied().sum();
20    /// assert_eq!(sum, 10);
21    ///
22    /// // alternatively
23    /// let sum: i32 = values.par().copied().sum();
24    /// assert_eq!(sum, 10);
25    /// ```
26    fn par(&self) -> ParIter<<Self::Iterable<'_> as ConcurrentIterable>::Iter, Id<&Self::Item>> {
27        ParIter::new(
28            self.con_iter(),
29            Id::new(),
30            default_runner(),
31            Default::default(),
32        )
33    }
34}
35
36impl<X> ParCollection for X where X: ConcurrentCollection {}