orx_parallel/into_parallel/par_drain.rs
1use crate::infallible::{ParIter, xap_variants::Id};
2use crate::runner::default_runner;
3use core::ops::RangeBounds;
4use orx_concurrent_iter::ConcurrentDrainableOverSlice;
5
6/// Adds parallel draining to slice-based drainable collections.
7///
8/// Sequential counterpart: draining methods such as `Vec::drain`.
9pub trait ParDrain: ConcurrentDrainableOverSlice {
10 /// Drains the specified range and returns a parallel iterator over removed items.
11 ///
12 /// # Example
13 ///
14 /// ```
15 /// use orx_parallel::*;
16 ///
17 /// let mut values = vec![0i32, 1, 2, 3, 4, 5];
18 /// let drained_sum: i32 = ParDrain::par_drain(&mut values, 0..3).sum();
19 ///
20 /// assert_eq!(drained_sum, 3);
21 /// assert_eq!(values, vec![3, 4, 5]);
22 /// ```
23 ///
24 /// # Panics
25 ///
26 /// Panics if `range` is invalid for the underlying collection.
27 fn par_drain<R>(
28 &mut self,
29 range: R,
30 ) -> ParIter<<Self as ConcurrentDrainableOverSlice>::DrainingIter<'_>, Id<Self::Item>>
31 where
32 R: RangeBounds<usize>,
33 {
34 ParIter::new(
35 self.con_drain(range),
36 Id::new(),
37 default_runner(),
38 Default::default(),
39 )
40 }
41}
42
43impl<I> ParDrain for I where I: ConcurrentDrainableOverSlice {}