Inspecting Futures

Ever wanted to know when your futures are polled and what they return? Here’s a decorator for Futures that prints whenever that is the case.

(Obviously, inner types must be Debug).

extern crate futures;

use std::fmt::Debug;
use futures::{Future, Poll};

#[derive(Debug)]
pub struct InspectFuture<T, E, F: Future<Item=T, Error=E>>
    where T: Debug,
          E: Debug {
    future: F,
    label: String
}

impl<T, E, F> Future for InspectFuture<T, E, F>
    where T: Debug,
          E: Debug,
          F: Future<Item=T, Error=E> {
    type Item = F::Item;
    type Error = F::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let poll = self.future.poll();
        println!("Future {} polled: {:?}", self.label, poll);
        poll
    }
}

pub trait InspectExt<T, E>
    where T: Debug,
          E: Debug,
          Self: Future<Item=T, Error=E> + Sized {
    fn inspect(self, label: &str) -> InspectFuture<T, E, Self>;
}

impl<T, E, F> InspectExt<T, E> for F
    where T: Debug,
          E: Debug,
          F: Future<Item=T, Error=E> {
    fn inspect(self, label: &str) -> InspectFuture<T, E, Self> {
        InspectFuture { future: self, label: label.to_owned() }
    }
}
top