orx_parallel/
parallel_drainable.rs

1use crate::{DefaultRunner, Params, computational_variants::Par};
2use orx_concurrent_iter::ConcurrentDrainableOverSlice;
3use std::ops::RangeBounds;
4
5/// A type which can create a parallel draining iterator over any of its sub-slices.
6///
7/// * Created draining iterator takes out and returns all elements of the slice defined by the `range`.
8/// * The slice defined by this `range` will be removed from the original collection.
9///
10/// If the iterator is dropped before being fully consumed, it drops the remaining removed elements.
11///
12/// If the complete range is provided (`..` or `0..self.len()`), self will remain empty.
13///
14/// # Examples
15///
16/// ```
17/// use orx_parallel::*;
18///
19/// let mut v = vec![1, 2, 3];
20/// let u: Vec<_> = v.par_drain(1..).num_threads(2).collect();
21///
22/// assert_eq!(v, &[1]);
23/// assert_eq!(u, &[2, 3]);
24/// ```
25pub trait ParallelDrainableOverSlice: ConcurrentDrainableOverSlice {
26    /// Creates a parallel draining iterator over the slice defined by the given `range`.
27    ///
28    /// * Created draining iterator takes out and returns all elements of the slice defined by the `range`.
29    /// * The slice defined by this `range` will be removed from the original collection.
30    ///
31    /// If the iterator is dropped before being fully consumed, it drops the remaining removed elements.
32    ///
33    /// If the complete range is provided (`..` or `0..self.len()`), self will remain empty.
34    ///
35    /// # Examples
36    ///
37    /// ```
38    /// use orx_parallel::*;
39    ///
40    /// let mut v = vec![1, 2, 3];
41    /// let u: Vec<_> = v.par_drain(1..).num_threads(2).collect();
42    ///
43    /// assert_eq!(v, &[1]);
44    /// assert_eq!(u, &[2, 3]);
45    /// ```
46    fn par_drain<R>(
47        &mut self,
48        range: R,
49    ) -> Par<<Self as ConcurrentDrainableOverSlice>::DrainingIter<'_>, DefaultRunner>
50    where
51        R: RangeBounds<usize>,
52    {
53        Par::new(Params::default(), self.con_drain(range))
54    }
55}
56
57impl<I> ParallelDrainableOverSlice for I where I: ConcurrentDrainableOverSlice {}