1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use consumer::*;
use stream::*;

struct SkipState<C> {
    consumer: C,
    count: u64,
}

/// Ignores the first n items.
///
/// This struct is created by the [skip()](./trait.Stream.html#method.skip)
/// method on [Stream](./trait.Stream.html).
/// See its documentation for more.
#[must_use = "stream adaptors are lazy and do nothing unless consumed"]
pub struct Skip<S> {
    count: u64,
    stream: S,
}

impl<C, T> Consumer<T> for SkipState<C>
    where C: Consumer<T>
{
    fn emit(&mut self, item: T) -> bool {
        if self.count > 0 {
            self.count -= 1;
            true
        } else {
            self.consumer.emit(item)
        }
    }
}

impl<S> Stream for Skip<S>
    where S: Stream
{
    type Item = S::Item;

    fn consume<C>(self, consumer: C)
        where C: Consumer<Self::Item>
    {
        self.stream.consume(SkipState {
            consumer: consumer,
            count: self.count,
        });
    }
}

impl<S> Skip<S> {
    pub fn new(stream: S, count: u64) -> Self {
        Skip {
            count: count,
            stream: stream,
        }
    }
}