Trait future_wrap::WrapFuture
source · [−]pub trait WrapFuture<O>: Sized + Future {
fn wrap<F>(self, f: F) -> WrappedFuture<Self, F, O>ⓘNotable traits for WrappedFuture<Fut, F, O>impl<Fut, F, O> Future for WrappedFuture<Fut, F, O> where
Fut: Future,
F: FnMut(Pin<&mut Fut>, &mut Context<'_>) -> Poll<O>, type Output = O;
where
F: FnMut(Pin<&mut Self>, &mut Context<'_>) -> Poll<O>,
{ ... }
}Expand description
Adds a wrap function to all types that implement Future
Lets you track each poll, interrupt the execution of the Future and change the return type
use future_wrap::WrapFuture;
use std::future::Future;
use std::task::Poll;
use std::time::{Duration, Instant};
let fut = some_async_fn();
let mut remaining_time = Duration::from_millis(10);
fut.wrap(|fut, cx| {
let poll_start = Instant::now();
println!("Poll start");
let res = fut.poll(cx).map(|v| Some(v));
println!("Poll end");
remaining_time = remaining_time.saturating_sub(poll_start.elapsed());
if remaining_time.is_zero() {
println!("Too much time spent on polls :(");
Poll::Ready(None)
} else {
res.map(|v| Some(v))
}
}).await;