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