Coverage Report

Created: 2025-11-16 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.10.0/src/vec.rs
Line
Count
Source
1
//! Parallel iterator types for [vectors][std::vec] (`Vec<T>`)
2
//!
3
//! You will rarely need to interact with this module directly unless you need
4
//! to name one of the iterator types.
5
//!
6
//! [std::vec]: https://doc.rust-lang.org/stable/std/vec/
7
8
use crate::iter::plumbing::*;
9
use crate::iter::*;
10
use crate::math::simplify_range;
11
use crate::slice::{Iter, IterMut};
12
use std::iter;
13
use std::mem;
14
use std::ops::{Range, RangeBounds};
15
use std::ptr;
16
use std::slice;
17
18
impl<'data, T: Sync + 'data> IntoParallelIterator for &'data Vec<T> {
19
    type Item = &'data T;
20
    type Iter = Iter<'data, T>;
21
22
0
    fn into_par_iter(self) -> Self::Iter {
23
0
        <&[T]>::into_par_iter(self)
24
0
    }
25
}
26
27
impl<'data, T: Send + 'data> IntoParallelIterator for &'data mut Vec<T> {
28
    type Item = &'data mut T;
29
    type Iter = IterMut<'data, T>;
30
31
0
    fn into_par_iter(self) -> Self::Iter {
32
0
        <&mut [T]>::into_par_iter(self)
33
0
    }
34
}
35
36
/// Parallel iterator that moves out of a vector.
37
#[derive(Debug, Clone)]
38
pub struct IntoIter<T: Send> {
39
    vec: Vec<T>,
40
}
41
42
impl<T: Send> IntoParallelIterator for Vec<T> {
43
    type Item = T;
44
    type Iter = IntoIter<T>;
45
46
0
    fn into_par_iter(self) -> Self::Iter {
47
0
        IntoIter { vec: self }
48
0
    }
49
}
50
51
impl<T: Send> ParallelIterator for IntoIter<T> {
52
    type Item = T;
53
54
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
55
0
    where
56
0
        C: UnindexedConsumer<Self::Item>,
57
    {
58
0
        bridge(self, consumer)
59
0
    }
60
61
0
    fn opt_len(&self) -> Option<usize> {
62
0
        Some(self.len())
63
0
    }
64
}
65
66
impl<T: Send> IndexedParallelIterator for IntoIter<T> {
67
0
    fn drive<C>(self, consumer: C) -> C::Result
68
0
    where
69
0
        C: Consumer<Self::Item>,
70
    {
71
0
        bridge(self, consumer)
72
0
    }
73
74
0
    fn len(&self) -> usize {
75
0
        self.vec.len()
76
0
    }
77
78
0
    fn with_producer<CB>(mut self, callback: CB) -> CB::Output
79
0
    where
80
0
        CB: ProducerCallback<Self::Item>,
81
    {
82
        // Drain every item, and then the vector only needs to free its buffer.
83
0
        self.vec.par_drain(..).with_producer(callback)
84
0
    }
85
}
86
87
impl<'data, T: Send> ParallelDrainRange<usize> for &'data mut Vec<T> {
88
    type Iter = Drain<'data, T>;
89
    type Item = T;
90
91
0
    fn par_drain<R: RangeBounds<usize>>(self, range: R) -> Self::Iter {
92
0
        Drain {
93
0
            orig_len: self.len(),
94
0
            range: simplify_range(range, self.len()),
95
0
            vec: self,
96
0
        }
97
0
    }
98
}
99
100
/// Draining parallel iterator that moves a range out of a vector, but keeps the total capacity.
101
#[derive(Debug)]
102
pub struct Drain<'data, T: Send> {
103
    vec: &'data mut Vec<T>,
104
    range: Range<usize>,
105
    orig_len: usize,
106
}
107
108
impl<'data, T: Send> ParallelIterator for Drain<'data, T> {
109
    type Item = T;
110
111
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
112
0
    where
113
0
        C: UnindexedConsumer<Self::Item>,
114
    {
115
0
        bridge(self, consumer)
116
0
    }
117
118
0
    fn opt_len(&self) -> Option<usize> {
119
0
        Some(self.len())
120
0
    }
121
}
122
123
impl<'data, T: Send> IndexedParallelIterator for Drain<'data, T> {
124
0
    fn drive<C>(self, consumer: C) -> C::Result
125
0
    where
126
0
        C: Consumer<Self::Item>,
127
    {
128
0
        bridge(self, consumer)
129
0
    }
130
131
0
    fn len(&self) -> usize {
132
0
        self.range.len()
133
0
    }
134
135
0
    fn with_producer<CB>(self, callback: CB) -> CB::Output
136
0
    where
137
0
        CB: ProducerCallback<Self::Item>,
138
    {
139
        unsafe {
140
            // Make the vector forget about the drained items, and temporarily the tail too.
141
0
            self.vec.set_len(self.range.start);
142
143
            // Create the producer as the exclusive "owner" of the slice.
144
0
            let producer = DrainProducer::from_vec(self.vec, self.range.len());
145
146
            // The producer will move or drop each item from the drained range.
147
0
            callback.callback(producer)
148
        }
149
0
    }
150
}
151
152
impl<'data, T: Send> Drop for Drain<'data, T> {
153
0
    fn drop(&mut self) {
154
0
        let Range { start, end } = self.range;
155
0
        if self.vec.len() == self.orig_len {
156
0
            // We must not have produced, so just call a normal drain to remove the items.
157
0
            self.vec.drain(start..end);
158
0
        } else if start == end {
159
            // Empty range, so just restore the length to its original state
160
0
            unsafe {
161
0
                self.vec.set_len(self.orig_len);
162
0
            }
163
0
        } else if end < self.orig_len {
164
            // The producer was responsible for consuming the drained items.
165
            // Move the tail items to their new place, then set the length to include them.
166
0
            unsafe {
167
0
                let ptr = self.vec.as_mut_ptr().add(start);
168
0
                let tail_ptr = self.vec.as_ptr().add(end);
169
0
                let tail_len = self.orig_len - end;
170
0
                ptr::copy(tail_ptr, ptr, tail_len);
171
0
                self.vec.set_len(start + tail_len);
172
0
            }
173
0
        }
174
0
    }
175
}
176
177
/// ////////////////////////////////////////////////////////////////////////
178
179
pub(crate) struct DrainProducer<'data, T: Send> {
180
    slice: &'data mut [T],
181
}
182
183
impl<T: Send> DrainProducer<'_, T> {
184
    /// Creates a draining producer, which *moves* items from the slice.
185
    ///
186
    /// Unsafe because `!Copy` data must not be read after the borrow is released.
187
0
    pub(crate) unsafe fn new(slice: &mut [T]) -> DrainProducer<'_, T> {
188
0
        DrainProducer { slice }
189
0
    }
190
191
    /// Creates a draining producer, which *moves* items from the tail of the vector.
192
    ///
193
    /// Unsafe because we're moving from beyond `vec.len()`, so the caller must ensure
194
    /// that data is initialized and not read after the borrow is released.
195
0
    unsafe fn from_vec(vec: &mut Vec<T>, len: usize) -> DrainProducer<'_, T> {
196
0
        let start = vec.len();
197
0
        assert!(vec.capacity() - start >= len);
198
199
        // The pointer is derived from `Vec` directly, not through a `Deref`,
200
        // so it has provenance over the whole allocation.
201
0
        let ptr = vec.as_mut_ptr().add(start);
202
0
        DrainProducer::new(slice::from_raw_parts_mut(ptr, len))
203
0
    }
204
}
205
206
impl<'data, T: 'data + Send> Producer for DrainProducer<'data, T> {
207
    type Item = T;
208
    type IntoIter = SliceDrain<'data, T>;
209
210
0
    fn into_iter(mut self) -> Self::IntoIter {
211
        // replace the slice so we don't drop it twice
212
0
        let slice = mem::take(&mut self.slice);
213
0
        SliceDrain {
214
0
            iter: slice.iter_mut(),
215
0
        }
216
0
    }
217
218
0
    fn split_at(mut self, index: usize) -> (Self, Self) {
219
        // replace the slice so we don't drop it twice
220
0
        let slice = mem::take(&mut self.slice);
221
0
        let (left, right) = slice.split_at_mut(index);
222
0
        unsafe { (DrainProducer::new(left), DrainProducer::new(right)) }
223
0
    }
224
}
225
226
impl<'data, T: 'data + Send> Drop for DrainProducer<'data, T> {
227
0
    fn drop(&mut self) {
228
        // extract the slice so we can use `Drop for [T]`
229
0
        let slice_ptr: *mut [T] = mem::take::<&'data mut [T]>(&mut self.slice);
230
0
        unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
231
0
    }
232
}
233
234
/// ////////////////////////////////////////////////////////////////////////
235
236
// like std::vec::Drain, without updating a source Vec
237
pub(crate) struct SliceDrain<'data, T> {
238
    iter: slice::IterMut<'data, T>,
239
}
240
241
impl<'data, T: 'data> Iterator for SliceDrain<'data, T> {
242
    type Item = T;
243
244
0
    fn next(&mut self) -> Option<T> {
245
        // Coerce the pointer early, so we don't keep the
246
        // reference that's about to be invalidated.
247
0
        let ptr: *const T = self.iter.next()?;
248
0
        Some(unsafe { ptr::read(ptr) })
249
0
    }
250
251
0
    fn size_hint(&self) -> (usize, Option<usize>) {
252
0
        self.iter.size_hint()
253
0
    }
254
255
0
    fn count(self) -> usize {
256
0
        self.iter.len()
257
0
    }
258
}
259
260
impl<'data, T: 'data> DoubleEndedIterator for SliceDrain<'data, T> {
261
0
    fn next_back(&mut self) -> Option<Self::Item> {
262
        // Coerce the pointer early, so we don't keep the
263
        // reference that's about to be invalidated.
264
0
        let ptr: *const T = self.iter.next_back()?;
265
0
        Some(unsafe { ptr::read(ptr) })
266
0
    }
267
}
268
269
impl<'data, T: 'data> ExactSizeIterator for SliceDrain<'data, T> {
270
0
    fn len(&self) -> usize {
271
0
        self.iter.len()
272
0
    }
273
}
274
275
impl<'data, T: 'data> iter::FusedIterator for SliceDrain<'data, T> {}
276
277
impl<'data, T: 'data> Drop for SliceDrain<'data, T> {
278
0
    fn drop(&mut self) {
279
        // extract the iterator so we can use `Drop for [T]`
280
0
        let slice_ptr: *mut [T] = mem::replace(&mut self.iter, [].iter_mut()).into_slice();
281
0
        unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
282
0
    }
283
}