1 use super::plumbing
::*;
7 /// `Enumerate` is an iterator that returns the current count along with the element.
8 /// This struct is created by the [`enumerate()`] method on [`IndexedParallelIterator`]
10 /// [`enumerate()`]: trait.IndexedParallelIterator.html#method.enumerate
11 /// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
12 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
13 #[derive(Debug, Clone)]
14 pub struct Enumerate
<I
: IndexedParallelIterator
> {
20 I
: IndexedParallelIterator
,
22 /// Create a new `Enumerate` iterator.
23 pub(super) fn new(base
: I
) -> Self {
28 impl<I
> ParallelIterator
for Enumerate
<I
>
30 I
: IndexedParallelIterator
,
32 type Item
= (usize, I
::Item
);
34 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
36 C
: UnindexedConsumer
<Self::Item
>,
38 bridge(self, consumer
)
41 fn opt_len(&self) -> Option
<usize> {
46 impl<I
> IndexedParallelIterator
for Enumerate
<I
>
48 I
: IndexedParallelIterator
,
50 fn drive
<C
: Consumer
<Self::Item
>>(self, consumer
: C
) -> C
::Result
{
51 bridge(self, consumer
)
54 fn len(&self) -> usize {
58 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
60 CB
: ProducerCallback
<Self::Item
>,
62 return self.base
.with_producer(Callback { callback }
);
68 impl<I
, CB
> ProducerCallback
<I
> for Callback
<CB
>
70 CB
: ProducerCallback
<(usize, I
)>,
72 type Output
= CB
::Output
;
73 fn callback
<P
>(self, base
: P
) -> CB
::Output
75 P
: Producer
<Item
= I
>,
77 let producer
= EnumerateProducer { base, offset: 0 }
;
78 self.callback
.callback(producer
)
84 /// ////////////////////////////////////////////////////////////////////////
85 /// Producer implementation
87 struct EnumerateProducer
<P
> {
92 impl<P
> Producer
for EnumerateProducer
<P
>
96 type Item
= (usize, P
::Item
);
97 type IntoIter
= iter
::Zip
<Range
<usize>, P
::IntoIter
>;
99 fn into_iter(self) -> Self::IntoIter
{
100 // Enumerate only works for IndexedParallelIterators. Since those
101 // have a max length of usize::MAX, their max index is
102 // usize::MAX - 1, so the range 0..usize::MAX includes all
105 // However, we should to use a precise end to the range, otherwise
106 // reversing the iterator may have to walk back a long ways before
107 // `Zip::next_back` can produce anything.
108 let base
= self.base
.into_iter();
109 let end
= self.offset
+ base
.len();
110 (self.offset
..end
).zip(base
)
113 fn min_len(&self) -> usize {
116 fn max_len(&self) -> usize {
120 fn split_at(self, index
: usize) -> (Self, Self) {
121 let (left
, right
) = self.base
.split_at(index
);
129 offset
: self.offset
+ index
,