Coverage Report

Created: 2025-10-28 08:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/slice/rchunks.rs
Line
Count
Source
1
use crate::iter::plumbing::*;
2
use crate::iter::*;
3
4
/// Parallel iterator over immutable non-overlapping chunks of a slice, starting at the end.
5
#[derive(Debug)]
6
pub struct RChunks<'data, T> {
7
    chunk_size: usize,
8
    slice: &'data [T],
9
}
10
11
impl<'data, T> RChunks<'data, T> {
12
0
    pub(super) fn new(chunk_size: usize, slice: &'data [T]) -> Self {
13
0
        Self { chunk_size, slice }
14
0
    }
15
}
16
17
impl<T> Clone for RChunks<'_, T> {
18
0
    fn clone(&self) -> Self {
19
0
        RChunks { ..*self }
20
0
    }
21
}
22
23
impl<'data, T: Sync> ParallelIterator for RChunks<'data, T> {
24
    type Item = &'data [T];
25
26
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
27
0
    where
28
0
        C: UnindexedConsumer<Self::Item>,
29
    {
30
0
        bridge(self, consumer)
31
0
    }
32
33
0
    fn opt_len(&self) -> Option<usize> {
34
0
        Some(self.len())
35
0
    }
36
}
37
38
impl<T: Sync> IndexedParallelIterator for RChunks<'_, T> {
39
0
    fn drive<C>(self, consumer: C) -> C::Result
40
0
    where
41
0
        C: Consumer<Self::Item>,
42
    {
43
0
        bridge(self, consumer)
44
0
    }
45
46
0
    fn len(&self) -> usize {
47
0
        self.slice.len().div_ceil(self.chunk_size)
48
0
    }
49
50
0
    fn with_producer<CB>(self, callback: CB) -> CB::Output
51
0
    where
52
0
        CB: ProducerCallback<Self::Item>,
53
    {
54
0
        callback.callback(RChunksProducer {
55
0
            chunk_size: self.chunk_size,
56
0
            slice: self.slice,
57
0
        })
58
0
    }
59
}
60
61
struct RChunksProducer<'data, T: Sync> {
62
    chunk_size: usize,
63
    slice: &'data [T],
64
}
65
66
impl<'data, T: 'data + Sync> Producer for RChunksProducer<'data, T> {
67
    type Item = &'data [T];
68
    type IntoIter = ::std::slice::RChunks<'data, T>;
69
70
0
    fn into_iter(self) -> Self::IntoIter {
71
0
        self.slice.rchunks(self.chunk_size)
72
0
    }
73
74
0
    fn split_at(self, index: usize) -> (Self, Self) {
75
0
        let elem_index = self.slice.len().saturating_sub(index * self.chunk_size);
76
0
        let (left, right) = self.slice.split_at(elem_index);
77
0
        (
78
0
            RChunksProducer {
79
0
                chunk_size: self.chunk_size,
80
0
                slice: right,
81
0
            },
82
0
            RChunksProducer {
83
0
                chunk_size: self.chunk_size,
84
0
                slice: left,
85
0
            },
86
0
        )
87
0
    }
88
}
89
90
/// Parallel iterator over immutable non-overlapping chunks of a slice, starting at the end.
91
#[derive(Debug)]
92
pub struct RChunksExact<'data, T> {
93
    chunk_size: usize,
94
    slice: &'data [T],
95
    rem: &'data [T],
96
}
97
98
impl<'data, T> RChunksExact<'data, T> {
99
0
    pub(super) fn new(chunk_size: usize, slice: &'data [T]) -> Self {
100
0
        let rem_len = slice.len() % chunk_size;
101
0
        let (rem, slice) = slice.split_at(rem_len);
102
0
        Self {
103
0
            chunk_size,
104
0
            slice,
105
0
            rem,
106
0
        }
107
0
    }
108
109
    /// Return the remainder of the original slice that is not going to be
110
    /// returned by the iterator. The returned slice has at most `chunk_size-1`
111
    /// elements.
112
0
    pub fn remainder(&self) -> &'data [T] {
113
0
        self.rem
114
0
    }
115
}
116
117
impl<T> Clone for RChunksExact<'_, T> {
118
0
    fn clone(&self) -> Self {
119
0
        RChunksExact { ..*self }
120
0
    }
121
}
122
123
impl<'data, T: Sync> ParallelIterator for RChunksExact<'data, T> {
124
    type Item = &'data [T];
125
126
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
127
0
    where
128
0
        C: UnindexedConsumer<Self::Item>,
129
    {
130
0
        bridge(self, consumer)
131
0
    }
132
133
0
    fn opt_len(&self) -> Option<usize> {
134
0
        Some(self.len())
135
0
    }
136
}
137
138
impl<T: Sync> IndexedParallelIterator for RChunksExact<'_, T> {
139
0
    fn drive<C>(self, consumer: C) -> C::Result
140
0
    where
141
0
        C: Consumer<Self::Item>,
142
    {
143
0
        bridge(self, consumer)
144
0
    }
145
146
0
    fn len(&self) -> usize {
147
0
        self.slice.len() / self.chunk_size
148
0
    }
149
150
0
    fn with_producer<CB>(self, callback: CB) -> CB::Output
151
0
    where
152
0
        CB: ProducerCallback<Self::Item>,
153
    {
154
0
        callback.callback(RChunksExactProducer {
155
0
            chunk_size: self.chunk_size,
156
0
            slice: self.slice,
157
0
        })
158
0
    }
159
}
160
161
struct RChunksExactProducer<'data, T: Sync> {
162
    chunk_size: usize,
163
    slice: &'data [T],
164
}
165
166
impl<'data, T: 'data + Sync> Producer for RChunksExactProducer<'data, T> {
167
    type Item = &'data [T];
168
    type IntoIter = ::std::slice::RChunksExact<'data, T>;
169
170
0
    fn into_iter(self) -> Self::IntoIter {
171
0
        self.slice.rchunks_exact(self.chunk_size)
172
0
    }
173
174
0
    fn split_at(self, index: usize) -> (Self, Self) {
175
0
        let elem_index = self.slice.len() - index * self.chunk_size;
176
0
        let (left, right) = self.slice.split_at(elem_index);
177
0
        (
178
0
            RChunksExactProducer {
179
0
                chunk_size: self.chunk_size,
180
0
                slice: right,
181
0
            },
182
0
            RChunksExactProducer {
183
0
                chunk_size: self.chunk_size,
184
0
                slice: left,
185
0
            },
186
0
        )
187
0
    }
188
}
189
190
/// Parallel iterator over mutable non-overlapping chunks of a slice, starting at the end.
191
#[derive(Debug)]
192
pub struct RChunksMut<'data, T> {
193
    chunk_size: usize,
194
    slice: &'data mut [T],
195
}
196
197
impl<'data, T> RChunksMut<'data, T> {
198
0
    pub(super) fn new(chunk_size: usize, slice: &'data mut [T]) -> Self {
199
0
        Self { chunk_size, slice }
200
0
    }
201
}
202
203
impl<'data, T: Send> ParallelIterator for RChunksMut<'data, T> {
204
    type Item = &'data mut [T];
205
206
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
207
0
    where
208
0
        C: UnindexedConsumer<Self::Item>,
209
    {
210
0
        bridge(self, consumer)
211
0
    }
212
213
0
    fn opt_len(&self) -> Option<usize> {
214
0
        Some(self.len())
215
0
    }
216
}
217
218
impl<T: Send> IndexedParallelIterator for RChunksMut<'_, T> {
219
0
    fn drive<C>(self, consumer: C) -> C::Result
220
0
    where
221
0
        C: Consumer<Self::Item>,
222
    {
223
0
        bridge(self, consumer)
224
0
    }
225
226
0
    fn len(&self) -> usize {
227
0
        self.slice.len().div_ceil(self.chunk_size)
228
0
    }
229
230
0
    fn with_producer<CB>(self, callback: CB) -> CB::Output
231
0
    where
232
0
        CB: ProducerCallback<Self::Item>,
233
    {
234
0
        callback.callback(RChunksMutProducer {
235
0
            chunk_size: self.chunk_size,
236
0
            slice: self.slice,
237
0
        })
238
0
    }
239
}
240
241
struct RChunksMutProducer<'data, T: Send> {
242
    chunk_size: usize,
243
    slice: &'data mut [T],
244
}
245
246
impl<'data, T: 'data + Send> Producer for RChunksMutProducer<'data, T> {
247
    type Item = &'data mut [T];
248
    type IntoIter = ::std::slice::RChunksMut<'data, T>;
249
250
0
    fn into_iter(self) -> Self::IntoIter {
251
0
        self.slice.rchunks_mut(self.chunk_size)
252
0
    }
253
254
0
    fn split_at(self, index: usize) -> (Self, Self) {
255
0
        let elem_index = self.slice.len().saturating_sub(index * self.chunk_size);
256
0
        let (left, right) = self.slice.split_at_mut(elem_index);
257
0
        (
258
0
            RChunksMutProducer {
259
0
                chunk_size: self.chunk_size,
260
0
                slice: right,
261
0
            },
262
0
            RChunksMutProducer {
263
0
                chunk_size: self.chunk_size,
264
0
                slice: left,
265
0
            },
266
0
        )
267
0
    }
268
}
269
270
/// Parallel iterator over mutable non-overlapping chunks of a slice, starting at the end.
271
#[derive(Debug)]
272
pub struct RChunksExactMut<'data, T: Send> {
273
    chunk_size: usize,
274
    slice: &'data mut [T],
275
    rem: &'data mut [T],
276
}
277
278
impl<'data, T: Send> RChunksExactMut<'data, T> {
279
0
    pub(super) fn new(chunk_size: usize, slice: &'data mut [T]) -> Self {
280
0
        let rem_len = slice.len() % chunk_size;
281
0
        let (rem, slice) = slice.split_at_mut(rem_len);
282
0
        Self {
283
0
            chunk_size,
284
0
            slice,
285
0
            rem,
286
0
        }
287
0
    }
288
289
    /// Return the remainder of the original slice that is not going to be
290
    /// returned by the iterator. The returned slice has at most `chunk_size-1`
291
    /// elements.
292
    ///
293
    /// Note that this has to consume `self` to return the original lifetime of
294
    /// the data, which prevents this from actually being used as a parallel
295
    /// iterator since that also consumes. This method is provided for parity
296
    /// with `std::iter::RChunksExactMut`, but consider calling `remainder()` or
297
    /// `take_remainder()` as alternatives.
298
0
    pub fn into_remainder(self) -> &'data mut [T] {
299
0
        self.rem
300
0
    }
301
302
    /// Return the remainder of the original slice that is not going to be
303
    /// returned by the iterator. The returned slice has at most `chunk_size-1`
304
    /// elements.
305
    ///
306
    /// Consider `take_remainder()` if you need access to the data with its
307
    /// original lifetime, rather than borrowing through `&mut self` here.
308
0
    pub fn remainder(&mut self) -> &mut [T] {
309
0
        self.rem
310
0
    }
311
312
    /// Return the remainder of the original slice that is not going to be
313
    /// returned by the iterator. The returned slice has at most `chunk_size-1`
314
    /// elements. Subsequent calls will return an empty slice.
315
0
    pub fn take_remainder(&mut self) -> &'data mut [T] {
316
0
        std::mem::take(&mut self.rem)
317
0
    }
318
}
319
320
impl<'data, T: Send + 'data> ParallelIterator for RChunksExactMut<'data, T> {
321
    type Item = &'data mut [T];
322
323
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
324
0
    where
325
0
        C: UnindexedConsumer<Self::Item>,
326
    {
327
0
        bridge(self, consumer)
328
0
    }
329
330
0
    fn opt_len(&self) -> Option<usize> {
331
0
        Some(self.len())
332
0
    }
333
}
334
335
impl<'data, T: Send + 'data> IndexedParallelIterator for RChunksExactMut<'data, T> {
336
0
    fn drive<C>(self, consumer: C) -> C::Result
337
0
    where
338
0
        C: Consumer<Self::Item>,
339
    {
340
0
        bridge(self, consumer)
341
0
    }
342
343
0
    fn len(&self) -> usize {
344
0
        self.slice.len() / self.chunk_size
345
0
    }
346
347
0
    fn with_producer<CB>(self, callback: CB) -> CB::Output
348
0
    where
349
0
        CB: ProducerCallback<Self::Item>,
350
    {
351
0
        callback.callback(RChunksExactMutProducer {
352
0
            chunk_size: self.chunk_size,
353
0
            slice: self.slice,
354
0
        })
355
0
    }
356
}
357
358
struct RChunksExactMutProducer<'data, T: Send> {
359
    chunk_size: usize,
360
    slice: &'data mut [T],
361
}
362
363
impl<'data, T: 'data + Send> Producer for RChunksExactMutProducer<'data, T> {
364
    type Item = &'data mut [T];
365
    type IntoIter = ::std::slice::RChunksExactMut<'data, T>;
366
367
0
    fn into_iter(self) -> Self::IntoIter {
368
0
        self.slice.rchunks_exact_mut(self.chunk_size)
369
0
    }
370
371
0
    fn split_at(self, index: usize) -> (Self, Self) {
372
0
        let elem_index = self.slice.len() - index * self.chunk_size;
373
0
        let (left, right) = self.slice.split_at_mut(elem_index);
374
0
        (
375
0
            RChunksExactMutProducer {
376
0
                chunk_size: self.chunk_size,
377
0
                slice: right,
378
0
            },
379
0
            RChunksExactMutProducer {
380
0
                chunk_size: self.chunk_size,
381
0
                slice: left,
382
0
            },
383
0
        )
384
0
    }
385
}