Skip to main content

orx_parallel/into_parallel/
par_collection_mut.rs

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