]>
git.proxmox.com Git - rustc.git/blob - vendor/rustc-rayon/src/iter/len.rs
1 use super::plumbing
::*;
5 /// `MinLen` is an iterator that imposes a minimum length on iterator splits.
6 /// This struct is created by the [`min_len()`] method on [`IndexedParallelIterator`]
8 /// [`min_len()`]: trait.IndexedParallelIterator.html#method.min_len
9 /// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
10 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
11 #[derive(Debug, Clone)]
12 pub struct MinLen
<I
: IndexedParallelIterator
> {
17 /// Create a new `MinLen` iterator.
19 /// NB: a free fn because it is NOT part of the end-user API.
20 pub fn new_min_len
<I
>(base
: I
, min
: usize) -> MinLen
<I
>
21 where I
: IndexedParallelIterator
29 impl<I
> ParallelIterator
for MinLen
<I
>
30 where I
: IndexedParallelIterator
34 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
35 where C
: UnindexedConsumer
<Self::Item
>
37 bridge(self, consumer
)
40 fn opt_len(&self) -> Option
<usize> {
45 impl<I
> IndexedParallelIterator
for MinLen
<I
>
46 where I
: IndexedParallelIterator
48 fn drive
<C
: Consumer
<Self::Item
>>(self, consumer
: C
) -> C
::Result
{
49 bridge(self, consumer
)
52 fn len(&self) -> usize {
56 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
57 where CB
: ProducerCallback
<Self::Item
>
59 return self.base
.with_producer(Callback
{
69 impl<T
, CB
> ProducerCallback
<T
> for Callback
<CB
>
70 where CB
: ProducerCallback
<T
>
72 type Output
= CB
::Output
;
73 fn callback
<P
>(self, base
: P
) -> CB
::Output
74 where P
: Producer
<Item
= T
>
76 let producer
= MinLenProducer
{
80 self.callback
.callback(producer
)
86 /// ////////////////////////////////////////////////////////////////////////
87 /// `MinLenProducer` implementation
89 struct MinLenProducer
<P
> {
94 impl<P
> Producer
for MinLenProducer
<P
>
98 type IntoIter
= P
::IntoIter
;
100 fn into_iter(self) -> Self::IntoIter
{
101 self.base
.into_iter()
104 fn min_len(&self) -> usize {
105 cmp
::max(self.min
, self.base
.min_len())
108 fn max_len(&self) -> usize {
112 fn split_at(self, index
: usize) -> (Self, Self) {
113 let (left
, right
) = self.base
.split_at(index
);
124 fn fold_with
<F
>(self, folder
: F
) -> F
125 where F
: Folder
<Self::Item
>
127 self.base
.fold_with(folder
)
132 /// `MaxLen` is an iterator that imposes a maximum length on iterator splits.
133 /// This struct is created by the [`max_len()`] method on [`IndexedParallelIterator`]
135 /// [`max_len()`]: trait.IndexedParallelIterator.html#method.max_len
136 /// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
137 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
138 #[derive(Debug, Clone)]
139 pub struct MaxLen
<I
: IndexedParallelIterator
> {
144 /// Create a new `MaxLen` iterator.
146 /// NB: a free fn because it is NOT part of the end-user API.
147 pub fn new_max_len
<I
>(base
: I
, max
: usize) -> MaxLen
<I
>
148 where I
: IndexedParallelIterator
156 impl<I
> ParallelIterator
for MaxLen
<I
>
157 where I
: IndexedParallelIterator
161 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
162 where C
: UnindexedConsumer
<Self::Item
>
164 bridge(self, consumer
)
167 fn opt_len(&self) -> Option
<usize> {
172 impl<I
> IndexedParallelIterator
for MaxLen
<I
>
173 where I
: IndexedParallelIterator
175 fn drive
<C
: Consumer
<Self::Item
>>(self, consumer
: C
) -> C
::Result
{
176 bridge(self, consumer
)
179 fn len(&self) -> usize {
183 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
184 where CB
: ProducerCallback
<Self::Item
>
186 return self.base
.with_producer(Callback
{
191 struct Callback
<CB
> {
196 impl<T
, CB
> ProducerCallback
<T
> for Callback
<CB
>
197 where CB
: ProducerCallback
<T
>
199 type Output
= CB
::Output
;
200 fn callback
<P
>(self, base
: P
) -> CB
::Output
201 where P
: Producer
<Item
= T
>
203 let producer
= MaxLenProducer
{
207 self.callback
.callback(producer
)
213 /// ////////////////////////////////////////////////////////////////////////
214 /// `MaxLenProducer` implementation
216 struct MaxLenProducer
<P
> {
221 impl<P
> Producer
for MaxLenProducer
<P
>
225 type IntoIter
= P
::IntoIter
;
227 fn into_iter(self) -> Self::IntoIter
{
228 self.base
.into_iter()
231 fn min_len(&self) -> usize {
235 fn max_len(&self) -> usize {
236 cmp
::min(self.max
, self.base
.max_len())
239 fn split_at(self, index
: usize) -> (Self, Self) {
240 let (left
, right
) = self.base
.split_at(index
);
251 fn fold_with
<F
>(self, folder
: F
) -> F
252 where F
: Folder
<Self::Item
>
254 self.base
.fold_with(folder
)