WrapFuture

Trait WrapFuture 

Source
pub trait WrapFuture<O>: Sized + Future {
    // Provided method
    fn wrap<F>(self, f: F) -> WrappedFuture<Self, F, 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);
    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;

Provided Methods§

Source

fn wrap<F>(self, f: F) -> WrappedFuture<Self, F, O>
where F: FnMut(Pin<&mut Self>, &mut Context<'_>) -> Poll<O>,

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<F: Future, O> WrapFuture<O> for F