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,
: Debug {
E: F,
future: String
label}
impl<T, E, F> Future for InspectFuture<T, E, F>
where T: Debug,
: Debug,
E: Future<Item=T, Error=E> {
Ftype 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,
: Debug,
ESelf: 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,
: Debug,
E: Future<Item=T, Error=E> {
Ffn inspect(self, label: &str) -> InspectFuture<T, E, Self> {
{ future: self, label: label.to_owned() }
InspectFuture }
}