]> git.proxmox.com Git - rustc.git/blob - src/vendor/rayon/src/iter/skip.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / vendor / rayon / src / iter / skip.rs
1 use super::plumbing::*;
2 use super::*;
3 use super::noop::NoopConsumer;
4 use std::cmp::min;
5
6 /// `Skip` is an iterator that skips over the first `n` elements.
7 /// This struct is created by the [`skip()`] method on [`IndexedParallelIterator`]
8 ///
9 /// [`skip()`]: trait.IndexedParallelIterator.html#method.skip
10 /// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
11 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
12 #[derive(Debug, Clone)]
13 pub struct Skip<I> {
14 base: I,
15 n: usize,
16 }
17
18 /// Create a new `Skip` iterator.
19 ///
20 /// NB: a free fn because it is NOT part of the end-user API.
21 pub fn new<I>(base: I, n: usize) -> Skip<I>
22 where I: IndexedParallelIterator
23 {
24 let n = min(base.len(), n);
25 Skip { base: base, n: n }
26 }
27
28 impl<I> ParallelIterator for Skip<I>
29 where I: IndexedParallelIterator
30 {
31 type Item = I::Item;
32
33 fn drive_unindexed<C>(self, consumer: C) -> C::Result
34 where C: UnindexedConsumer<Self::Item>
35 {
36 bridge(self, consumer)
37 }
38
39 fn opt_len(&self) -> Option<usize> {
40 Some(self.len())
41 }
42 }
43
44 impl<I> IndexedParallelIterator for Skip<I>
45 where I: IndexedParallelIterator
46 {
47 fn len(&self) -> usize {
48 self.base.len() - self.n
49 }
50
51 fn drive<C: Consumer<Self::Item>>(self, consumer: C) -> C::Result {
52 bridge(self, consumer)
53 }
54
55 fn with_producer<CB>(self, callback: CB) -> CB::Output
56 where CB: ProducerCallback<Self::Item>
57 {
58 return self.base.with_producer(Callback {
59 callback: callback,
60 n: self.n,
61 });
62
63 struct Callback<CB> {
64 callback: CB,
65 n: usize,
66 }
67
68 impl<T, CB> ProducerCallback<T> for Callback<CB>
69 where CB: ProducerCallback<T>
70 {
71 type Output = CB::Output;
72 fn callback<P>(self, base: P) -> CB::Output
73 where P: Producer<Item = T>
74 {
75 let (before_skip, after_skip) = base.split_at(self.n);
76 bridge_producer_consumer(self.n, before_skip, NoopConsumer::new());
77 self.callback.callback(after_skip)
78 }
79 }
80 }
81 }