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