par_iter/
par_either.rs

1use crate::iter::{
2    plumbing::*,
3    Either::{Left, Right},
4    *,
5};
6
7/// `Either<L, R>` is a parallel iterator if both `L` and `R` are parallel
8/// iterators.
9impl<L, R> ParallelIterator for Either<L, R>
10where
11    L: ParallelIterator,
12    R: ParallelIterator<Item = L::Item>,
13{
14    type Item = L::Item;
15
16    fn drive_unindexed<C>(self, consumer: C) -> C::Result
17    where
18        C: UnindexedConsumer<Self::Item>,
19    {
20        match self {
21            Left(iter) => iter.drive_unindexed(consumer),
22            Right(iter) => iter.drive_unindexed(consumer),
23        }
24    }
25
26    fn opt_len(&self) -> Option<usize> {
27        self.as_ref().either(L::opt_len, R::opt_len)
28    }
29}
30
31impl<L, R> IndexedParallelIterator for Either<L, R>
32where
33    L: IndexedParallelIterator,
34    R: IndexedParallelIterator<Item = L::Item>,
35{
36    fn drive<C>(self, consumer: C) -> C::Result
37    where
38        C: Consumer<Self::Item>,
39    {
40        match self {
41            Left(iter) => iter.drive(consumer),
42            Right(iter) => iter.drive(consumer),
43        }
44    }
45
46    fn len(&self) -> usize {
47        self.as_ref().either(L::len, R::len)
48    }
49
50    fn with_producer<CB>(self, callback: CB) -> CB::Output
51    where
52        CB: ProducerCallback<Self::Item>,
53    {
54        match self {
55            Left(iter) => iter.with_producer(callback),
56            Right(iter) => iter.with_producer(callback),
57        }
58    }
59}
60
61/// `Either<L, R>` can be extended if both `L` and `R` are parallel extendable.
62impl<L, R, T> ParallelExtend<T> for Either<L, R>
63where
64    L: ParallelExtend<T>,
65    R: ParallelExtend<T>,
66    T: Send,
67{
68    fn par_extend<I>(&mut self, par_iter: I)
69    where
70        I: IntoParallelIterator<Item = T>,
71    {
72        match self.as_mut() {
73            Left(collection) => collection.par_extend(par_iter),
74            Right(collection) => collection.par_extend(par_iter),
75        }
76    }
77}