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