1use std::iter;
2
3use super::{plumbing::*, *};
4
5#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
12#[derive(Debug, Clone)]
13pub struct Copied<I: ParallelIterator> {
14 base: I,
15}
16
17impl<I> Copied<I>
18where
19 I: ParallelIterator,
20{
21 pub(super) fn new(base: I) -> Self {
23 Copied { base }
24 }
25}
26
27impl<'a, T, I> ParallelIterator for Copied<I>
28where
29 I: ParallelIterator<Item = &'a T>,
30 T: 'a + Copy + Send + Sync,
31{
32 type Item = T;
33
34 fn drive_unindexed<C>(self, consumer: C) -> C::Result
35 where
36 C: UnindexedConsumer<Self::Item>,
37 {
38 let consumer1 = CopiedConsumer::new(consumer);
39 self.base.drive_unindexed(consumer1)
40 }
41
42 fn opt_len(&self) -> Option<usize> {
43 self.base.opt_len()
44 }
45}
46
47impl<'a, T, I> IndexedParallelIterator for Copied<I>
48where
49 I: IndexedParallelIterator<Item = &'a T>,
50 T: 'a + Copy + Send + Sync,
51{
52 fn drive<C>(self, consumer: C) -> C::Result
53 where
54 C: Consumer<Self::Item>,
55 {
56 let consumer1 = CopiedConsumer::new(consumer);
57 self.base.drive(consumer1)
58 }
59
60 fn len(&self) -> usize {
61 self.base.len()
62 }
63
64 fn with_producer<CB>(self, callback: CB) -> CB::Output
65 where
66 CB: ProducerCallback<Self::Item>,
67 {
68 return self.base.with_producer(Callback { callback });
69
70 struct Callback<CB> {
71 callback: CB,
72 }
73
74 impl<'a, T, CB> ProducerCallback<&'a T> for Callback<CB>
75 where
76 CB: ProducerCallback<T>,
77 T: 'a + Copy + Send,
78 {
79 type Output = CB::Output;
80
81 fn callback<P>(self, base: P) -> CB::Output
82 where
83 P: Producer<Item = &'a T>,
84 {
85 let producer = CopiedProducer { base };
86 self.callback.callback(producer)
87 }
88 }
89 }
90}
91
92struct CopiedProducer<P> {
95 base: P,
96}
97
98impl<'a, T, P> Producer for CopiedProducer<P>
99where
100 P: Producer<Item = &'a T>,
101 T: 'a + Copy,
102{
103 type IntoIter = iter::Copied<P::IntoIter>;
104 type Item = T;
105
106 fn into_iter(self) -> Self::IntoIter {
107 self.base.into_iter().copied()
108 }
109
110 fn min_len(&self) -> usize {
111 self.base.min_len()
112 }
113
114 fn max_len(&self) -> usize {
115 self.base.max_len()
116 }
117
118 fn split_at(self, index: usize) -> (Self, Self) {
119 let (left, right) = self.base.split_at(index);
120 (
121 CopiedProducer { base: left },
122 CopiedProducer { base: right },
123 )
124 }
125
126 fn fold_with<F>(self, folder: F) -> F
127 where
128 F: Folder<Self::Item>,
129 {
130 self.base.fold_with(CopiedFolder { base: folder }).base
131 }
132}
133
134struct CopiedConsumer<C> {
138 base: C,
139}
140
141impl<C> CopiedConsumer<C> {
142 fn new(base: C) -> Self {
143 CopiedConsumer { base }
144 }
145}
146
147impl<'a, T, C> Consumer<&'a T> for CopiedConsumer<C>
148where
149 C: Consumer<T>,
150 T: 'a + Copy,
151{
152 type Folder = CopiedFolder<C::Folder>;
153 type Reducer = C::Reducer;
154 type Result = C::Result;
155
156 fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
157 let (left, right, reducer) = self.base.split_at(index);
158 (
159 CopiedConsumer::new(left),
160 CopiedConsumer::new(right),
161 reducer,
162 )
163 }
164
165 fn into_folder(self) -> Self::Folder {
166 CopiedFolder {
167 base: self.base.into_folder(),
168 }
169 }
170
171 fn full(&self) -> bool {
172 self.base.full()
173 }
174}
175
176impl<'a, T, C> UnindexedConsumer<&'a T> for CopiedConsumer<C>
177where
178 C: UnindexedConsumer<T>,
179 T: 'a + Copy,
180{
181 fn split_off_left(&self) -> Self {
182 CopiedConsumer::new(self.base.split_off_left())
183 }
184
185 fn to_reducer(&self) -> Self::Reducer {
186 self.base.to_reducer()
187 }
188}
189
190struct CopiedFolder<F> {
191 base: F,
192}
193
194impl<'a, T, F> Folder<&'a T> for CopiedFolder<F>
195where
196 F: Folder<T>,
197 T: 'a + Copy,
198{
199 type Result = F::Result;
200
201 fn consume(self, &item: &'a T) -> Self {
202 CopiedFolder {
203 base: self.base.consume(item),
204 }
205 }
206
207 fn consume_iter<I>(mut self, iter: I) -> Self
208 where
209 I: IntoIterator<Item = &'a T>,
210 {
211 self.base = self.base.consume_iter(iter.into_iter().copied());
212 self
213 }
214
215 fn complete(self) -> F::Result {
216 self.base.complete()
217 }
218
219 fn full(&self) -> bool {
220 self.base.full()
221 }
222}