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
#[macro_use]
extern crate log;
extern crate futures;

use std::fmt;

use futures::{Future, Poll, Stream};

/// A wrapper around a `Future` or `Stream` that logs calls to `poll`
/// and their results.
#[derive(Debug, Clone)]
pub struct Trace<T> {
    inner: T,
}

impl<F> Future for Trace<F>
where
    F: Future,
    F::Item: fmt::Debug,
    F::Error: fmt::Debug,
    F: fmt::Debug,
{
    type Item = F::Item;
    type Error = F::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        trace!("{:?}.poll()", self.inner);
        let poll = self.inner.poll();
        trace!("{:?}.poll() -> {:?};", self.inner, poll);
        poll
    }

}

impl<S> Stream for Trace<S>
where
    S: Stream,
    S::Item: fmt::Debug,
    S::Error: fmt::Debug,
    S: fmt::Debug,
{
    type Item = S::Item;
    type Error = S::Error;

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        trace!("{:?}.poll()", self.inner);
        let poll = self.inner.poll();
        trace!("{:?}.poll() -> {:?};", self.inner, poll);
        poll
    }

}

impl<T> From<T> for Trace<T> {

    fn from(inner: T) -> Self {
        Trace { inner }
    }

}