]>
git.proxmox.com Git - rustc.git/blob - vendor/rustc-rayon/src/iter/repeat.rs
1 use super::plumbing
::*;
6 /// Iterator adaptor for [the `repeat()` function](fn.repeat.html).
7 #[derive(Debug, Clone)]
8 pub struct Repeat
<T
: Clone
+ Send
> {
12 /// Creates a parallel iterator that endlessly repeats `elt` (by
13 /// cloning it). Note that this iterator has "infinite" length, so
14 /// typically you would want to use `zip` or `take` or some other
15 /// means to shorten it, or consider using
16 /// [the `repeatn()` function](fn.repeatn.html) instead.
21 /// use rayon::prelude::*;
22 /// use rayon::iter::repeat;
23 /// let x: Vec<(i32, i32)> = repeat(22).zip(0..3).collect();
24 /// assert_eq!(x, vec![(22, 0), (22, 1), (22, 2)]);
26 pub fn repeat
<T
: Clone
+ Send
>(elt
: T
) -> Repeat
<T
> {
27 Repeat { element: elt }
34 /// Takes only `n` repeats of the element, similar to the general
35 /// [`take()`](trait.IndexedParallelIterator.html#method.take).
37 /// The resulting `RepeatN` is an `IndexedParallelIterator`, allowing
38 /// more functionality than `Repeat` alone.
39 pub fn take(self, n
: usize) -> RepeatN
<T
> {
40 repeatn(self.element
, n
)
43 /// Iterates tuples, repeating the element with items from another
44 /// iterator, similar to the general
45 /// [`zip()`](trait.IndexedParallelIterator.html#method.zip).
46 pub fn zip
<Z
>(self, zip_op
: Z
) -> Zip
<RepeatN
<T
>, Z
::Iter
>
48 Z
: IntoParallelIterator
,
49 Z
::Iter
: IndexedParallelIterator
,
51 let z
= zip_op
.into_par_iter();
57 impl<T
> ParallelIterator
for Repeat
<T
>
63 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
65 C
: UnindexedConsumer
<Self::Item
>,
67 let producer
= RepeatProducer
{
68 element
: self.element
,
70 bridge_unindexed(producer
, consumer
)
74 /// Unindexed producer for `Repeat`.
75 struct RepeatProducer
<T
: Clone
+ Send
> {
79 impl<T
: Clone
+ Send
> UnindexedProducer
for RepeatProducer
<T
> {
82 fn split(self) -> (Self, Option
<Self>) {
85 element
: self.element
.clone(),
88 element
: self.element
,
93 fn fold_with
<F
>(self, folder
: F
) -> F
97 folder
.consume_iter(iter
::repeat(self.element
))
101 /// Iterator adaptor for [the `repeatn()` function](fn.repeatn.html).
102 #[derive(Debug, Clone)]
103 pub struct RepeatN
<T
: Clone
+ Send
> {
108 /// Creates a parallel iterator that produces `n` repeats of `elt`
114 /// use rayon::prelude::*;
115 /// use rayon::iter::repeatn;
116 /// let x: Vec<(i32, i32)> = repeatn(22, 3).zip(0..3).collect();
117 /// assert_eq!(x, vec![(22, 0), (22, 1), (22, 2)]);
119 pub fn repeatn
<T
: Clone
+ Send
>(elt
: T
, n
: usize) -> RepeatN
<T
> {
126 impl<T
> ParallelIterator
for RepeatN
<T
>
132 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
134 C
: UnindexedConsumer
<Self::Item
>,
136 bridge(self, consumer
)
139 fn opt_len(&self) -> Option
<usize> {
144 impl<T
> IndexedParallelIterator
for RepeatN
<T
>
148 fn drive
<C
>(self, consumer
: C
) -> C
::Result
150 C
: Consumer
<Self::Item
>,
152 bridge(self, consumer
)
155 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
157 CB
: ProducerCallback
<Self::Item
>,
159 callback
.callback(RepeatNProducer
{
160 element
: self.element
,
165 fn len(&self) -> usize {
170 /// Producer for `RepeatN`.
171 struct RepeatNProducer
<T
: Clone
+ Send
> {
176 impl<T
: Clone
+ Send
> Producer
for RepeatNProducer
<T
> {
178 type IntoIter
= Iter
<T
>;
180 fn into_iter(self) -> Self::IntoIter
{
182 element
: self.element
,
187 fn split_at(self, index
: usize) -> (Self, Self) {
190 element
: self.element
.clone(),
194 element
: self.element
,
195 count
: self.count
- index
,
201 /// Iterator for `RepeatN`.
203 /// This is conceptually like `std::iter::Take<std::iter::Repeat<T>>`, but
204 /// we need `DoubleEndedIterator` and unconditional `ExactSizeIterator`.
205 struct Iter
<T
: Clone
> {
210 impl<T
: Clone
> Iterator
for Iter
<T
> {
214 fn next(&mut self) -> Option
<T
> {
217 Some(self.element
.clone())
224 fn size_hint(&self) -> (usize, Option
<usize>) {
225 (self.count
, Some(self.count
))
229 impl<T
: Clone
> DoubleEndedIterator
for Iter
<T
> {
231 fn next_back(&mut self) -> Option
<T
> {
236 impl<T
: Clone
> ExactSizeIterator
for Iter
<T
> {
238 fn len(&self) -> usize {