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
56
57
58
59
60
61
62
63
64
65
66
67
use consumer::*;
use std::collections::VecDeque;
use stream::*;
struct SkipLastState<C, T>
where C: Consumer<T>
{
consumer: C,
count: usize,
queue: VecDeque<T>,
}
#[must_use = "stream adaptors are lazy and do nothing unless consumed"]
pub struct SkipLast<S> {
count: usize,
stream: S,
}
impl<C, T> Consumer<T> for SkipLastState<C, T>
where C: Consumer<T>
{
fn emit(&mut self, item: T) -> bool {
if self.count == self.queue.len() {
if let Some(old_item) = self.queue.pop_back() {
if !self.consumer.emit(old_item) {
return false;
}
}
}
self.queue.push_front(item);
true
}
}
impl<S> Stream for SkipLast<S>
where S: Stream
{
type Item = S::Item;
fn consume<C>(self, consumer: C)
where C: Consumer<Self::Item>
{
if self.count == 0 {
self.stream.consume(consumer);
} else {
self.stream.consume(SkipLastState {
consumer: consumer,
count: self.count,
queue: VecDeque::new(),
});
}
}
}
impl<S> SkipLast<S> {
pub fn new(stream: S, count: usize) -> Self {
SkipLast {
count: count,
stream: stream,
}
}
}