rayon_logs/
prelude.rs

1//! We redefine (not all of them yet) rayon traits.
2pub use rayon::prelude::ParallelIterator;
3
4use crate::Logged;
5
6/// `IntoParallelRefIterator` implements the conversion to a
7/// [`ParallelIterator`], providing shared references to the data.
8///
9/// This is a parallel version of the `iter()` method
10/// defined by various collections.
11///
12/// This trait is automatically implemented
13/// `for I where &I: IntoParallelIterator`. In most cases, users
14/// will want to implement [`IntoParallelIterator`] rather than implement
15/// this trait directly.
16///
17/// [`ParallelIterator`]: trait.ParallelIterator.html
18/// [`IntoParallelIterator`]: trait.IntoParallelIterator.html
19pub trait IntoParallelRefIterator<'data>: rayon::prelude::IntoParallelRefIterator<'data> {
20    /// Converts `self` into a parallel iterator.
21    ///
22    /// # Examples
23    ///
24    /// ```
25    /// use rayon::prelude::*;
26    ///
27    /// let v: Vec<_> = (0..100).collect();
28    /// assert_eq!(v.par_iter().sum::<i32>(), 100 * 99 / 2);
29    ///
30    /// // `v.par_iter()` is shorthand for `(&v).into_par_iter()`,
31    /// // producing the exact same references.
32    /// assert!(v.par_iter().zip(&v)
33    ///          .all(|(a, b)| std::ptr::eq(a, b)));
34    /// ```
35    fn par_iter(&'data self) -> Logged<Self::Iter> {
36        Logged::new(rayon::prelude::IntoParallelRefIterator::par_iter(self))
37    }
38}
39
40impl<'data, I: rayon::prelude::IntoParallelRefIterator<'data>> IntoParallelRefIterator<'data>
41    for I
42{
43}
44
45/// `IntoParallelIterator` implements the conversion to a [`ParallelIterator`].
46///
47/// By implementing `IntoParallelIterator` for a type, you define how it will
48/// transformed into an iterator. This is a parallel version of the standard
49/// library's [`std::iter::IntoIterator`] trait.
50///
51/// [`ParallelIterator`]: trait.ParallelIterator.html
52/// [`std::iter::IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html
53pub trait IntoParallelIterator: rayon::prelude::IntoParallelIterator + Sized {
54    /// Converts `self` into a parallel iterator.
55    ///
56    /// # Examples
57    ///
58    /// ```
59    /// use rayon::prelude::*;
60    ///
61    /// println!("counting in parallel:");
62    /// (0..100).into_par_iter()
63    ///     .for_each(|i| println!("{}", i));
64    /// ```
65    ///
66    /// This conversion is often implicit for arguments to methods like [`zip`].
67    ///
68    /// ```
69    /// use rayon::prelude::*;
70    ///
71    /// let v: Vec<_> = (0..5).into_par_iter().zip(5..10).collect();
72    /// assert_eq!(v, [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]);
73    /// ```
74    ///
75    /// [`zip`]: trait.IndexedParallelIterator.html#method.zip
76    fn into_par_iter(self) -> Logged<Self::Iter> {
77        Logged::new(rayon::prelude::IntoParallelIterator::into_par_iter(self))
78    }
79}
80
81impl<I: rayon::prelude::IntoParallelIterator> IntoParallelIterator for I {}
82
83/// `IntoParallelRefMutIterator` implements the conversion to a
84/// [`ParallelIterator`], providing mutable references to the data.
85///
86/// This is a parallel version of the `iter_mut()` method
87/// defined by various collections.
88///
89/// This trait is automatically implemented
90/// `for I where &mut I: IntoParallelIterator`. In most cases, users
91/// will want to implement [`IntoParallelIterator`] rather than implement
92/// this trait directly.
93///
94/// [`ParallelIterator`]: trait.ParallelIterator.html
95/// [`IntoParallelIterator`]: trait.IntoParallelIterator.html
96pub trait IntoParallelRefMutIterator<'data>:
97    rayon::prelude::IntoParallelRefMutIterator<'data>
98{
99    /// Creates the parallel iterator from `self`.
100    ///
101    /// # Examples
102    ///
103    /// ```
104    /// use rayon::prelude::*;
105    ///
106    /// let mut v = vec![0usize; 5];
107    /// v.par_iter_mut().enumerate().for_each(|(i, x)| *x = i);
108    /// assert_eq!(v, [0, 1, 2, 3, 4]);
109    /// ```
110    fn par_iter_mut(&'data mut self) -> Logged<Self::Iter> {
111        Logged::new(rayon::prelude::IntoParallelRefMutIterator::par_iter_mut(
112            self,
113        ))
114    }
115}
116impl<'data, I: rayon::prelude::IntoParallelRefMutIterator<'data>> IntoParallelRefMutIterator<'data>
117    for I
118{
119}
120
121pub use crate::rayon_algorithms::slice::ParallelSliceMut;