/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/iter/rev.rs
Line | Count | Source |
1 | | use super::plumbing::*; |
2 | | use super::*; |
3 | | use std::iter; |
4 | | |
5 | | /// `Rev` is an iterator that produces elements in reverse order. This struct |
6 | | /// is created by the [`rev()`] method on [`IndexedParallelIterator`] |
7 | | /// |
8 | | /// [`rev()`]: IndexedParallelIterator::rev() |
9 | | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
10 | | #[derive(Debug, Clone)] |
11 | | pub struct Rev<I> { |
12 | | base: I, |
13 | | } |
14 | | |
15 | | impl<I> Rev<I> { |
16 | | /// Creates a new `Rev` iterator. |
17 | 0 | pub(super) fn new(base: I) -> Self { |
18 | 0 | Rev { base } |
19 | 0 | } |
20 | | } |
21 | | |
22 | | impl<I> ParallelIterator for Rev<I> |
23 | | where |
24 | | I: IndexedParallelIterator, |
25 | | { |
26 | | type Item = I::Item; |
27 | | |
28 | 0 | fn drive_unindexed<C>(self, consumer: C) -> C::Result |
29 | 0 | where |
30 | 0 | C: UnindexedConsumer<Self::Item>, |
31 | | { |
32 | 0 | bridge(self, consumer) |
33 | 0 | } |
34 | | |
35 | 0 | fn opt_len(&self) -> Option<usize> { |
36 | 0 | Some(self.len()) |
37 | 0 | } |
38 | | } |
39 | | |
40 | | impl<I> IndexedParallelIterator for Rev<I> |
41 | | where |
42 | | I: IndexedParallelIterator, |
43 | | { |
44 | 0 | fn drive<C: Consumer<Self::Item>>(self, consumer: C) -> C::Result { |
45 | 0 | bridge(self, consumer) |
46 | 0 | } |
47 | | |
48 | 0 | fn len(&self) -> usize { |
49 | 0 | self.base.len() |
50 | 0 | } |
51 | | |
52 | 0 | fn with_producer<CB>(self, callback: CB) -> CB::Output |
53 | 0 | where |
54 | 0 | CB: ProducerCallback<Self::Item>, |
55 | | { |
56 | 0 | let len = self.base.len(); |
57 | 0 | return self.base.with_producer(Callback { callback, len }); |
58 | | |
59 | | struct Callback<CB> { |
60 | | callback: CB, |
61 | | len: usize, |
62 | | } |
63 | | |
64 | | impl<T, CB> ProducerCallback<T> for Callback<CB> |
65 | | where |
66 | | CB: ProducerCallback<T>, |
67 | | { |
68 | | type Output = CB::Output; |
69 | 0 | fn callback<P>(self, base: P) -> CB::Output |
70 | 0 | where |
71 | 0 | P: Producer<Item = T>, |
72 | | { |
73 | 0 | let producer = RevProducer { |
74 | 0 | base, |
75 | 0 | len: self.len, |
76 | 0 | }; |
77 | 0 | self.callback.callback(producer) |
78 | 0 | } |
79 | | } |
80 | 0 | } |
81 | | } |
82 | | |
83 | | struct RevProducer<P> { |
84 | | base: P, |
85 | | len: usize, |
86 | | } |
87 | | |
88 | | impl<P> Producer for RevProducer<P> |
89 | | where |
90 | | P: Producer, |
91 | | { |
92 | | type Item = P::Item; |
93 | | type IntoIter = iter::Rev<P::IntoIter>; |
94 | | |
95 | 0 | fn into_iter(self) -> Self::IntoIter { |
96 | 0 | self.base.into_iter().rev() |
97 | 0 | } |
98 | | |
99 | 0 | fn min_len(&self) -> usize { |
100 | 0 | self.base.min_len() |
101 | 0 | } |
102 | 0 | fn max_len(&self) -> usize { |
103 | 0 | self.base.max_len() |
104 | 0 | } |
105 | | |
106 | 0 | fn split_at(self, index: usize) -> (Self, Self) { |
107 | 0 | let (left, right) = self.base.split_at(self.len - index); |
108 | 0 | ( |
109 | 0 | RevProducer { |
110 | 0 | base: right, |
111 | 0 | len: index, |
112 | 0 | }, |
113 | 0 | RevProducer { |
114 | 0 | base: left, |
115 | 0 | len: self.len - index, |
116 | 0 | }, |
117 | 0 | ) |
118 | 0 | } |
119 | | } |