1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Integration with rayon

use rayon_lib::prelude::*;
use {VoluntaryServitude, VS};

impl<T: Send + Sync> VoluntaryServitude<T> {
    /// Parallely Extends [`VS`] like the ParallelExtend trait, but without a mutable reference
    ///
    /// [`VS`]: ./type.VS.html
    ///
    /// ```rust
    /// # #[macro_use] extern crate voluntary_servitude;
    /// # #[cfg(feature = "logs")] voluntary_servitude::setup_logger();
    /// let list = vs![1u8, 2, 3];
    /// list.par_extend(vec![4, 5, 6]);
    /// assert_eq!(list.iter().sum::<u8>(), 21u8);
    /// ```
    #[cfg(feature = "rayon-traits")]
    #[cfg_attr(docs_rs_workaround, doc(cfg(feature = "rayon-traits")))]
    #[inline]
    pub fn par_extend<I: IntoParallelIterator<Item = T>>(&self, par_iter: I) {
        trace!("par_extend()");
        par_iter.into_par_iter().for_each(|el| self.append(el));
    }
}

#[cfg_attr(docs_rs_workaround, doc(cfg(feature = "rayon-traits")))]
impl<T: Send + Sync> FromParallelIterator<T> for VoluntaryServitude<T> {
    #[inline]
    fn from_par_iter<I: IntoParallelIterator<Item = T>>(par_iter: I) -> Self {
        trace!("from_par_iter()");
        let vs = vs![];
        par_iter.into_par_iter().for_each(|el| vs.append(el));
        vs
    }
}

#[cfg_attr(docs_rs_workaround, doc(cfg(feature = "rayon-traits")))]
impl<T: Send + Sync> ParallelExtend<T> for VoluntaryServitude<T> {
    #[inline]
    fn par_extend<I: IntoParallelIterator<Item = T>>(&mut self, par_iter: I) {
        trace!("ParExtend");
        VS::par_extend(self, par_iter);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn setup_logger() {
        #[cfg(feature = "logs")]
        ::setup_logger();
    }

    #[test]
    fn from_par_iter() {
        setup_logger();
        let vec = vec![1, 2, 3, 4, 5, 6];
        let sum: u8 = vec.iter().sum();
        let vs = VS::from_par_iter(vec);
        assert_eq!(vs.iter().sum::<u8>(), sum);
    }
}