Coverage Report

Created: 2025-11-05 08:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/iter/len.rs
Line
Count
Source
1
use super::plumbing::*;
2
use super::*;
3
4
/// `MinLen` is an iterator that imposes a minimum length on iterator splits.
5
/// This struct is created by the [`with_min_len()`] method on [`IndexedParallelIterator`]
6
///
7
/// [`with_min_len()`]: IndexedParallelIterator::with_min_len()
8
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
9
#[derive(Debug, Clone)]
10
pub struct MinLen<I> {
11
    base: I,
12
    min: usize,
13
}
14
15
impl<I> MinLen<I> {
16
    /// Creates a new `MinLen` iterator.
17
0
    pub(super) fn new(base: I, min: usize) -> Self {
18
0
        MinLen { base, min }
19
0
    }
20
}
21
22
impl<I> ParallelIterator for MinLen<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 MinLen<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
        return self.base.with_producer(Callback {
57
0
            callback,
58
0
            min: self.min,
59
0
        });
60
61
        struct Callback<CB> {
62
            callback: CB,
63
            min: usize,
64
        }
65
66
        impl<T, CB> ProducerCallback<T> for Callback<CB>
67
        where
68
            CB: ProducerCallback<T>,
69
        {
70
            type Output = CB::Output;
71
0
            fn callback<P>(self, base: P) -> CB::Output
72
0
            where
73
0
                P: Producer<Item = T>,
74
            {
75
0
                let producer = MinLenProducer {
76
0
                    base,
77
0
                    min: self.min,
78
0
                };
79
0
                self.callback.callback(producer)
80
0
            }
81
        }
82
0
    }
83
}
84
85
// ////////////////////////////////////////////////////////////////////////
86
// `MinLenProducer` implementation
87
88
struct MinLenProducer<P> {
89
    base: P,
90
    min: usize,
91
}
92
93
impl<P> Producer for MinLenProducer<P>
94
where
95
    P: Producer,
96
{
97
    type Item = P::Item;
98
    type IntoIter = P::IntoIter;
99
100
0
    fn into_iter(self) -> Self::IntoIter {
101
0
        self.base.into_iter()
102
0
    }
103
104
0
    fn min_len(&self) -> usize {
105
0
        Ord::max(self.min, self.base.min_len())
106
0
    }
107
108
0
    fn max_len(&self) -> usize {
109
0
        self.base.max_len()
110
0
    }
111
112
0
    fn split_at(self, index: usize) -> (Self, Self) {
113
0
        let (left, right) = self.base.split_at(index);
114
0
        (
115
0
            MinLenProducer {
116
0
                base: left,
117
0
                min: self.min,
118
0
            },
119
0
            MinLenProducer {
120
0
                base: right,
121
0
                min: self.min,
122
0
            },
123
0
        )
124
0
    }
125
126
0
    fn fold_with<F>(self, folder: F) -> F
127
0
    where
128
0
        F: Folder<Self::Item>,
129
    {
130
0
        self.base.fold_with(folder)
131
0
    }
132
}
133
134
/// `MaxLen` is an iterator that imposes a maximum length on iterator splits.
135
/// This struct is created by the [`with_max_len()`] method on [`IndexedParallelIterator`]
136
///
137
/// [`with_max_len()`]: IndexedParallelIterator::with_max_len()
138
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
139
#[derive(Debug, Clone)]
140
pub struct MaxLen<I> {
141
    base: I,
142
    max: usize,
143
}
144
145
impl<I> MaxLen<I> {
146
    /// Creates a new `MaxLen` iterator.
147
0
    pub(super) fn new(base: I, max: usize) -> Self {
148
0
        MaxLen { base, max }
149
0
    }
150
}
151
152
impl<I> ParallelIterator for MaxLen<I>
153
where
154
    I: IndexedParallelIterator,
155
{
156
    type Item = I::Item;
157
158
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
159
0
    where
160
0
        C: UnindexedConsumer<Self::Item>,
161
    {
162
0
        bridge(self, consumer)
163
0
    }
164
165
0
    fn opt_len(&self) -> Option<usize> {
166
0
        Some(self.len())
167
0
    }
168
}
169
170
impl<I> IndexedParallelIterator for MaxLen<I>
171
where
172
    I: IndexedParallelIterator,
173
{
174
0
    fn drive<C: Consumer<Self::Item>>(self, consumer: C) -> C::Result {
175
0
        bridge(self, consumer)
176
0
    }
177
178
0
    fn len(&self) -> usize {
179
0
        self.base.len()
180
0
    }
181
182
0
    fn with_producer<CB>(self, callback: CB) -> CB::Output
183
0
    where
184
0
        CB: ProducerCallback<Self::Item>,
185
    {
186
0
        return self.base.with_producer(Callback {
187
0
            callback,
188
0
            max: self.max,
189
0
        });
190
191
        struct Callback<CB> {
192
            callback: CB,
193
            max: usize,
194
        }
195
196
        impl<T, CB> ProducerCallback<T> for Callback<CB>
197
        where
198
            CB: ProducerCallback<T>,
199
        {
200
            type Output = CB::Output;
201
0
            fn callback<P>(self, base: P) -> CB::Output
202
0
            where
203
0
                P: Producer<Item = T>,
204
            {
205
0
                let producer = MaxLenProducer {
206
0
                    base,
207
0
                    max: self.max,
208
0
                };
209
0
                self.callback.callback(producer)
210
0
            }
211
        }
212
0
    }
213
}
214
215
// ////////////////////////////////////////////////////////////////////////
216
// `MaxLenProducer` implementation
217
218
struct MaxLenProducer<P> {
219
    base: P,
220
    max: usize,
221
}
222
223
impl<P> Producer for MaxLenProducer<P>
224
where
225
    P: Producer,
226
{
227
    type Item = P::Item;
228
    type IntoIter = P::IntoIter;
229
230
0
    fn into_iter(self) -> Self::IntoIter {
231
0
        self.base.into_iter()
232
0
    }
233
234
0
    fn min_len(&self) -> usize {
235
0
        self.base.min_len()
236
0
    }
237
238
0
    fn max_len(&self) -> usize {
239
0
        Ord::min(self.max, self.base.max_len())
240
0
    }
241
242
0
    fn split_at(self, index: usize) -> (Self, Self) {
243
0
        let (left, right) = self.base.split_at(index);
244
0
        (
245
0
            MaxLenProducer {
246
0
                base: left,
247
0
                max: self.max,
248
0
            },
249
0
            MaxLenProducer {
250
0
                base: right,
251
0
                max: self.max,
252
0
            },
253
0
        )
254
0
    }
255
256
0
    fn fold_with<F>(self, folder: F) -> F
257
0
    where
258
0
        F: Folder<Self::Item>,
259
    {
260
0
        self.base.fold_with(folder)
261
0
    }
262
}