moirai_iter/parallel/adapters/
ref_ops.rs1use super::super::{Consumer, ParallelIterator, VecParIter};
2
3pub struct Enumerate<I> {
5 pub(super) base: I,
6}
7
8impl<I> Enumerate<I> {
9 pub(crate) fn new(base: I) -> Self {
10 Self { base }
11 }
12}
13
14impl<I> ParallelIterator for Enumerate<I>
15where
16 I: ParallelIterator,
17 I::Item: Sync + 'static,
18{
19 type Item = (usize, I::Item);
20
21 fn seq_items(self) -> Vec<Self::Item> {
22 self.base.seq_items().into_iter().enumerate().collect()
23 }
24
25 fn seq_items_window(self, skip: usize, take: Option<usize>) -> Vec<Self::Item> {
26 self.base
27 .seq_items_window(skip, take)
28 .into_iter()
29 .enumerate()
30 .map(|(offset, item)| (skip + offset, item))
31 .collect()
32 }
33
34 fn drive<C, R>(self, consumer: C) -> R
35 where
36 C: Consumer<Self::Item, Result = R> + Send + Sync,
37 R: Send,
38 {
39 consumer.consume(VecParIter::new(self.seq_items()))
40 }
41}
42
43pub struct Copied<I> {
45 pub(super) base: I,
46}
47
48impl<I> Copied<I> {
49 pub(crate) fn new(base: I) -> Self {
50 Self { base }
51 }
52}
53
54impl<'data, I, T> ParallelIterator for Copied<I>
55where
56 I: ParallelIterator<Item = &'data T>,
57 T: Copy + Send + Sync + 'data + 'static,
58{
59 type Item = T;
60
61 fn seq_items(self) -> Vec<Self::Item> {
62 self.base.seq_items().into_iter().copied().collect()
63 }
64
65 fn drive<C, R>(self, consumer: C) -> R
66 where
67 C: Consumer<Self::Item, Result = R> + Send + Sync,
68 R: Send,
69 {
70 consumer.consume(VecParIter::new(self.seq_items()))
71 }
72}
73
74pub struct Cloned<I> {
76 pub(super) base: I,
77}
78
79impl<I> Cloned<I> {
80 pub(crate) fn new(base: I) -> Self {
81 Self { base }
82 }
83}
84
85impl<'data, I, T> ParallelIterator for Cloned<I>
86where
87 I: ParallelIterator<Item = &'data T>,
88 T: Clone + Send + Sync + 'data + 'static,
89{
90 type Item = T;
91
92 fn seq_items(self) -> Vec<Self::Item> {
93 self.base.seq_items().into_iter().cloned().collect()
94 }
95
96 fn drive<C, R>(self, consumer: C) -> R
97 where
98 C: Consumer<Self::Item, Result = R> + Send + Sync,
99 R: Send,
100 {
101 consumer.consume(VecParIter::new(self.seq_items()))
102 }
103}