Debian Codegraph @

debian/rust-vendor/rayon/src/iter/skip.rs

raw
1 use super::noop::NoopConsumer;
2 use super::plumbing::*;
3 use super::*;
4
5 /// `Skip` is an iterator that skips over the first `n` elements.
6 /// This struct is created by the [`skip()`] method on [`IndexedParallelIterator`]
7 ///
8 /// [`skip()`]: IndexedParallelIterator::skip()
9 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
10 #[derive(Debug, Clone)]
11 pub struct Skip<I> {
12 base: I,
13 n: usize,
14 }
15
16 impl<I> Skip<I>
17 where
18 I: IndexedParallelIterator,
19 {
20 /// Creates a new `Skip` iterator.
21 pub(super) fn new(base: I, n: usize) -> Self {
22 let n = Ord::min(base.len(), n);
23 Skip { base, n }
24 }
25 }
26
27 impl<I> ParallelIterator for Skip<I>
28 where
29 I: IndexedParallelIterator,
30 {
31 type Item = I::Item;
32
33 fn drive_unindexed<C>(self, consumer: C) -> C::Result
34 where
35 C: UnindexedConsumer<Self::Item>,
36 {
37 bridge(self, consumer)
38 }
39
40 fn opt_len(&self) -> Option<usize> {
41 Some(self.len())
42 }
43 }
44
45 impl<I> IndexedParallelIterator for Skip<I>
46 where
47 I: IndexedParallelIterator,
48 {
49 fn len(&self) -> usize {
50 self.base.len() - self.n
51 }
52
53 fn drive<C: Consumer<Self::Item>>(self, consumer: C) -> C::Result {
54 bridge(self, consumer)
55 }
56
57 fn with_producer<CB>(self, callback: CB) -> CB::Output
58 where
59 CB: ProducerCallback<Self::Item>,
60 {
61 return self.base.with_producer(Callback {
62 callback,
63 n: self.n,
64 });
65
66 struct Callback<CB> {
67 callback: CB,
68 n: usize,
69 }
70
71 impl<T, CB> ProducerCallback<T> for Callback<CB>
72 where
73 CB: ProducerCallback<T>,
74 {
75 type Output = CB::Output;
76 fn callback<P>(self, base: P) -> CB::Output
77 where
78 P: Producer<Item = T>,
79 {
80 crate::in_place_scope(|scope| {
81 let Self { callback, n } = self;
82 let (before_skip, after_skip) = base.split_at(n);
83
84 // Run the skipped part separately for side effects.
85 // We'll still get any panics propagated back by the scope.
86 scope.spawn(move |_| bridge_producer_consumer(n, before_skip, NoopConsumer));
87
88 callback.callback(after_skip)
89 })
90 }
91 }
92 }
93 }