//! Parallel iterator types for [vectors][std::vec] (`Vec<T>`) //! //! You will rarely need to interact with this module directly unless you need //! to name one of the iterator types. //! //! [std::vec]: https://doc.rust-lang.org/stable/std/vec/
usecrate::iter::plumbing::*; usecrate::iter::*; usecrate::math::simplify_range; usecrate::slice::{Iter, IterMut}; use std::iter; use std::mem; use std::ops::{Range, RangeBounds}; use std::ptr; use std::slice;
impl<'data, T: Sync + 'data> IntoParallelIterator for &'data Vec<T> { type Item = &'data T; type Iter = Iter<'data, T>;
impl<T: Send> IndexedParallelIterator for IntoIter<T> { fn drive<C>(self, consumer: C) -> C::Result where
C: Consumer<Self::Item>,
{
bridge(self, consumer)
}
fn len(&self) -> usize { self.vec.len()
}
fn with_producer<CB>(mutself, callback: CB) -> CB::Output where
CB: ProducerCallback<Self::Item>,
{ // Drain every item, and then the vector only needs to free its buffer. self.vec.par_drain(..).with_producer(callback)
}
}
impl<'data, T: Send> ParallelDrainRange<usize> for &'data mut Vec<T> { type Iter = Drain<'data, T>; type Item = T;
/// Draining parallel iterator that moves a range out of a vector, but keeps the total capacity. #[derive(Debug)] pubstruct Drain<'data, T: Send> {
vec: &'data mut Vec<T>,
range: Range<usize>,
orig_len: usize,
}
impl<'data, T: Send> ParallelIterator for Drain<'data, T> { type Item = T;
fn drive_unindexed<C>(self, consumer: C) -> C::Result where
C: UnindexedConsumer<Self::Item>,
{
bridge(self, consumer)
}
impl<'data, T: Send> IndexedParallelIterator for Drain<'data, T> { fn drive<C>(self, consumer: C) -> C::Result where
C: Consumer<Self::Item>,
{
bridge(self, consumer)
}
fn len(&self) -> usize { self.range.len()
}
fn with_producer<CB>(self, callback: CB) -> CB::Output where
CB: ProducerCallback<Self::Item>,
{ unsafe { // Make the vector forget about the drained items, and temporarily the tail too. self.vec.set_len(self.range.start);
// Create the producer as the exclusive "owner" of the slice. let producer = DrainProducer::from_vec(self.vec, self.range.len());
// The producer will move or drop each item from the drained range.
callback.callback(producer)
}
}
}
impl<'data, T: Send> Drop for Drain<'data, T> { fn drop(&mutself) { let Range { start, end } = self.range; ifself.vec.len() == self.orig_len { // We must not have produced, so just call a normal drain to remove the items. self.vec.drain(start..end);
} elseif start == end { // Empty range, so just restore the length to its original state unsafe { self.vec.set_len(self.orig_len);
}
} elseif end < self.orig_len { // The producer was responsible for consuming the drained items. // Move the tail items to their new place, then set the length to include them. unsafe { let ptr = self.vec.as_mut_ptr().add(start); let tail_ptr = self.vec.as_ptr().add(end); let tail_len = self.orig_len - end;
ptr::copy(tail_ptr, ptr, tail_len); self.vec.set_len(start + tail_len);
}
}
}
}
impl<T: Send> DrainProducer<'_, T> { /// Creates a draining producer, which *moves* items from the slice. /// /// Unsafe because `!Copy` data must not be read after the borrow is released. pub(crate) unsafefn new(slice: &mut [T]) -> DrainProducer<'_, T> {
DrainProducer { slice }
}
/// Creates a draining producer, which *moves* items from the tail of the vector. /// /// Unsafe because we're moving from beyond `vec.len()`, so the caller must ensure /// that data is initialized and not read after the borrow is released. unsafefn from_vec(vec: &mut Vec<T>, len: usize) -> DrainProducer<'_, T> { let start = vec.len();
assert!(vec.capacity() - start >= len);
// The pointer is derived from `Vec` directly, not through a `Deref`, // so it has provenance over the whole allocation. let ptr = vec.as_mut_ptr().add(start);
DrainProducer::new(slice::from_raw_parts_mut(ptr, len))
}
}
impl<'data, T: 'data + Send> Producer for DrainProducer<'data, T> { type Item = T; type IntoIter = SliceDrain<'data, T>;
fn into_iter(mutself) -> Self::IntoIter { // replace the slice so we don't drop it twice let slice = mem::take(&mutself.slice);
SliceDrain {
iter: slice.iter_mut(),
}
}
fn split_at(mutself, index: usize) -> (Self, Self) { // replace the slice so we don't drop it twice let slice = mem::take(&mutself.slice); let (left, right) = slice.split_at_mut(index); unsafe { (DrainProducer::new(left), DrainProducer::new(right)) }
}
}
impl<'data, T: 'data + Send> Drop for DrainProducer<'data, T> { fn drop(&mutself) { // extract the slice so we can use `Drop for [T]` let slice_ptr: *mut [T] = mem::take::<&'data mut [T]>(&mut self.slice); unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
}
}
// like std::vec::Drain, without updating a source Vec pub(crate) struct SliceDrain<'data, T> {
iter: slice::IterMut<'data, T>,
}
impl<'data, T: 'data> Iterator for SliceDrain<'data, T> { type Item = T;
fn next(&mutself) -> Option<T> { // Coerce the pointer early, so we don't keep the // reference that's about to be invalidated. let ptr: *const T = self.iter.next()?;
Some(unsafe { ptr::read(ptr) })
}
impl<'data, T: 'data> DoubleEndedIterator for SliceDrain<'data, T> { fn next_back(&mutself) -> Option<Self::Item> { // Coerce the pointer early, so we don't keep the // reference that's about to be invalidated. let ptr: *const T = self.iter.next_back()?;
Some(unsafe { ptr::read(ptr) })
}
}
impl<'data, T: 'data> iter::FusedIterator for SliceDrain<'data, T> {}
impl<'data, T: 'data> Drop for SliceDrain<'data, T> { fn drop(&mutself) { // extract the iterator so we can use `Drop for [T]` let slice_ptr: *mut [T] = mem::replace(&mutself.iter, [].iter_mut()).into_slice(); unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.