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
use {Stream, Poll, Async};
#[derive(Debug)]
#[must_use = "streams do nothing unless polled"]
pub struct Inspect<S, F> where S: Stream {
stream: S,
inspect: F,
}
pub fn new<S, F>(stream: S, f: F) -> Inspect<S, F>
where S: Stream,
F: FnMut(&S::Item) -> (),
{
Inspect {
stream: stream,
inspect: f,
}
}
impl<S: Stream, F> Inspect<S, F> {
pub fn get_ref(&self) -> &S {
&self.stream
}
pub fn get_mut(&mut self) -> &mut S {
&mut self.stream
}
pub fn into_inner(self) -> S {
self.stream
}
}
impl<S, F> Stream for Inspect<S, F>
where S: Stream,
F: FnMut(&S::Item),
{
type Item = S::Item;
type Error = S::Error;
fn poll(&mut self) -> Poll<Option<S::Item>, S::Error> {
match try_ready!(self.stream.poll()) {
Some(e) => {
(self.inspect)(&e);
Ok(Async::Ready(Some(e)))
}
None => Ok(Async::Ready(None)),
}
}
}