1use crate::{
2 Consumer, Executor, ExecutorCallback, IndexedParallelIterator, IndexedProducer,
3 IndexedProducerCallback, IntoParallelIterator, ParallelIterator, Producer, ProducerCallback,
4 Reducer, WithIndexedProducer, WithProducer, WithSetup,
5};
6
7impl<'a, T> IntoParallelIterator<'a> for &'a [T]
8where
9 T: Send + Sync,
10{
11 type Iter = Iter<'a, T>;
12 type Item = &'a T;
13
14 fn into_par_iter(self) -> Self::Iter {
15 Iter { slice: self }
16 }
17}
18
19impl<'a, T> IntoParallelIterator<'a> for &'a mut [T]
20where
21 T: Send + Sync,
22{
23 type Iter = IterMut<'a, T>;
24 type Item = &'a mut T;
25
26 fn into_par_iter(self) -> Self::Iter {
27 IterMut { slice: self }
28 }
29}
30
31impl<'a, T> IntoParallelIterator<'a> for &'a Vec<T>
32where
33 T: Send + Sync,
34{
35 type Iter = Iter<'a, T>;
36 type Item = &'a T;
37
38 fn into_par_iter(self) -> Self::Iter {
39 Iter { slice: self }
40 }
41}
42
43impl<'a, T> IntoParallelIterator<'a> for &'a mut Vec<T>
44where
45 T: Send + Sync,
46{
47 type Iter = IterMut<'a, T>;
48 type Item = &'a mut T;
49
50 fn into_par_iter(self) -> Self::Iter {
51 IterMut { slice: self }
52 }
53}
54
55pub struct Iter<'a, T> {
58 slice: &'a [T],
59}
60
61impl<'a, T> ParallelIterator<'a> for Iter<'a, T>
62where
63 T: Send + Sync,
64{
65 type Item = &'a T;
66
67 fn drive<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
68 where
69 E: Executor<'a, D>,
70 C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
71 D: Send + 'a,
72 R: Reducer<D> + Send + 'a,
73 {
74 self.with_indexed_producer(ExecutorCallback::new(executor, consumer))
75 }
76
77 fn len_hint_opt(&self) -> Option<usize> {
78 Some(self.slice.len())
79 }
80}
81
82impl<'a, T> IndexedParallelIterator<'a> for Iter<'a, T>
83where
84 T: Send + Sync,
85{
86 fn drive_indexed<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
87 where
88 E: Executor<'a, D>,
89 C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
90 D: Send + 'a,
91 R: Reducer<D> + Send + 'a,
92 {
93 self.with_indexed_producer(ExecutorCallback::new(executor, consumer))
94 }
95
96 fn len_hint(&self) -> usize {
97 self.slice.len()
98 }
99}
100
101impl<'a, T> WithProducer<'a> for Iter<'a, T>
102where
103 T: Send + Sync,
104{
105 type Item = &'a T;
106
107 fn with_producer<CB>(self, callback: CB) -> CB::Output
108 where
109 CB: ProducerCallback<'a, Self::Item>,
110 {
111 callback.callback(IterProducer { slice: self.slice })
112 }
113}
114
115impl<'a, T> WithIndexedProducer<'a> for Iter<'a, T>
116where
117 T: Send + Sync,
118{
119 type Item = &'a T;
120
121 fn with_indexed_producer<CB>(self, callback: CB) -> CB::Output
122 where
123 CB: IndexedProducerCallback<'a, Self::Item>,
124 {
125 callback.callback(IterProducer { slice: self.slice })
126 }
127}
128
129struct IterProducer<'a, T> {
132 slice: &'a [T],
133}
134
135impl<'a, T> WithSetup for IterProducer<'a, T> {}
136
137impl<'a, T> Producer for IterProducer<'a, T>
138where
139 T: Send + Sync,
140{
141 type Item = &'a T;
142 type IntoIter = std::slice::Iter<'a, T>;
143
144 fn into_iter(self) -> Self::IntoIter {
145 self.slice.iter()
146 }
147
148 fn split(self) -> (Self, Option<Self>) {
149 if self.slice.len() < 2 {
150 (self, None)
151 } else {
152 let mid = self.slice.len() / 2;
153 let (left, right) = self.split_at(mid);
154
155 (left, Some(right))
156 }
157 }
158}
159
160impl<'a, T> IndexedProducer for IterProducer<'a, T>
161where
162 T: Send + Sync,
163{
164 type Item = &'a T;
165 type IntoIter = std::slice::Iter<'a, T>;
166
167 fn into_iter(self) -> Self::IntoIter {
168 self.slice.iter()
169 }
170
171 fn len(&self) -> usize {
172 self.slice.len()
173 }
174
175 fn split_at(self, index: usize) -> (Self, Self) {
176 let (left, right) = self.slice.split_at(index);
177
178 let left = IterProducer { slice: left };
179 let right = IterProducer { slice: right };
180
181 (left, right)
182 }
183}
184
185pub struct IterMut<'a, T> {
188 slice: &'a mut [T],
189}
190
191impl<'a, T> ParallelIterator<'a> for IterMut<'a, T>
192where
193 T: Send + Sync,
194{
195 type Item = &'a mut T;
196
197 fn drive<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
198 where
199 E: Executor<'a, D>,
200 C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
201 D: Send + 'a,
202 R: Reducer<D> + Send + 'a,
203 {
204 self.with_indexed_producer(ExecutorCallback::new(executor, consumer))
205 }
206
207 fn len_hint_opt(&self) -> Option<usize> {
208 Some(self.slice.len())
209 }
210}
211
212impl<'a, T> IndexedParallelIterator<'a> for IterMut<'a, T>
213where
214 T: Send + Sync,
215{
216 fn drive_indexed<E, C, D, R>(self, executor: E, consumer: C) -> E::Result
217 where
218 E: Executor<'a, D>,
219 C: Consumer<Self::Item, Result = D, Reducer = R> + 'a,
220 D: Send + 'a,
221 R: Reducer<D> + Send + 'a,
222 {
223 self.with_indexed_producer(ExecutorCallback::new(executor, consumer))
224 }
225
226 fn len_hint(&self) -> usize {
227 self.slice.len()
228 }
229}
230
231impl<'a, T> WithProducer<'a> for IterMut<'a, T>
232where
233 T: Send + Sync,
234{
235 type Item = &'a mut T;
236
237 fn with_producer<CB>(self, callback: CB) -> CB::Output
238 where
239 CB: ProducerCallback<'a, Self::Item>,
240 {
241 callback.callback(IterMutProducer { slice: self.slice })
242 }
243}
244
245impl<'a, T> WithIndexedProducer<'a> for IterMut<'a, T>
246where
247 T: Send + Sync,
248{
249 type Item = &'a mut T;
250
251 fn with_indexed_producer<CB>(self, callback: CB) -> CB::Output
252 where
253 CB: IndexedProducerCallback<'a, Self::Item>,
254 {
255 callback.callback(IterMutProducer { slice: self.slice })
256 }
257}
258
259struct IterMutProducer<'a, T> {
262 slice: &'a mut [T],
263}
264
265impl<'a, T> WithSetup for IterMutProducer<'a, T> {}
266
267impl<'a, T> Producer for IterMutProducer<'a, T>
268where
269 T: Send + Sync,
270{
271 type Item = &'a mut T;
272 type IntoIter = std::slice::IterMut<'a, T>;
273
274 fn into_iter(self) -> Self::IntoIter {
275 self.slice.iter_mut()
276 }
277
278 fn split(self) -> (Self, Option<Self>) {
279 if self.slice.len() < 2 {
280 (self, None)
281 } else {
282 let mid = self.slice.len() / 2;
283 let (left, right) = self.split_at(mid);
284
285 (left, Some(right))
286 }
287 }
288}
289
290impl<'a, T> IndexedProducer for IterMutProducer<'a, T>
291where
292 T: Send + Sync,
293{
294 type Item = &'a mut T;
295 type IntoIter = std::slice::IterMut<'a, T>;
296
297 fn into_iter(self) -> Self::IntoIter {
298 self.slice.iter_mut()
299 }
300
301 fn len(&self) -> usize {
302 self.slice.len()
303 }
304
305 fn split_at(self, index: usize) -> (Self, Self) {
306 let (left, right) = self.slice.split_at_mut(index);
307
308 let left = IterMutProducer { slice: left };
309 let right = IterMutProducer { slice: right };
310
311 (left, right)
312 }
313}