/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/iter/skip.rs
Line | Count | Source |
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 | 0 | pub(super) fn new(base: I, n: usize) -> Self { |
22 | 0 | let n = Ord::min(base.len(), n); |
23 | 0 | Skip { base, n } |
24 | 0 | } |
25 | | } |
26 | | |
27 | | impl<I> ParallelIterator for Skip<I> |
28 | | where |
29 | | I: IndexedParallelIterator, |
30 | | { |
31 | | type Item = I::Item; |
32 | | |
33 | 0 | fn drive_unindexed<C>(self, consumer: C) -> C::Result |
34 | 0 | where |
35 | 0 | C: UnindexedConsumer<Self::Item>, |
36 | | { |
37 | 0 | bridge(self, consumer) |
38 | 0 | } |
39 | | |
40 | 0 | fn opt_len(&self) -> Option<usize> { |
41 | 0 | Some(self.len()) |
42 | 0 | } |
43 | | } |
44 | | |
45 | | impl<I> IndexedParallelIterator for Skip<I> |
46 | | where |
47 | | I: IndexedParallelIterator, |
48 | | { |
49 | 0 | fn len(&self) -> usize { |
50 | 0 | self.base.len() - self.n |
51 | 0 | } |
52 | | |
53 | 0 | fn drive<C: Consumer<Self::Item>>(self, consumer: C) -> C::Result { |
54 | 0 | bridge(self, consumer) |
55 | 0 | } |
56 | | |
57 | 0 | fn with_producer<CB>(self, callback: CB) -> CB::Output |
58 | 0 | where |
59 | 0 | CB: ProducerCallback<Self::Item>, |
60 | | { |
61 | 0 | return self.base.with_producer(Callback { |
62 | 0 | callback, |
63 | 0 | n: self.n, |
64 | 0 | }); |
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 | 0 | fn callback<P>(self, base: P) -> CB::Output |
77 | 0 | where |
78 | 0 | P: Producer<Item = T>, |
79 | | { |
80 | 0 | crate::in_place_scope(|scope| { |
81 | 0 | let Self { callback, n } = self; |
82 | 0 | 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 | 0 | scope.spawn(move |_| bridge_producer_consumer(n, before_skip, NoopConsumer)); |
87 | | |
88 | 0 | callback.callback(after_skip) |
89 | 0 | }) |
90 | 0 | } |
91 | | } |
92 | 0 | } |
93 | | } |