Registering Waker with futures LocalPool

(I strugle to ask this question a few weeks now, because I think this should be easy and obvious :weary:)

I do have a struct A which implements the Future trait. It should be as easy as return Poll::Pending as long as 5 seconds have not been elapsed.

So I go with:

struct A { 
    start: Instant,
}

impl Future for A { 
    type Output = (); 

    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        dbg!();
        if Instant::now().duration_since(self.start).as_secs() > 5 { 
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }   
}

Sure enough, If I use the futures LocalPool it will be only triggered once, because I miss to make the waker again to be polled. According to the async book chp. 3 it should be as easy as self.waker.register(cx.waker()) (or similar).
But what is waker? How to I can make A to be polled again with futures LocalPool (or ThreadPool)? There is the LocalSpawner, but I can't manage to register the waker again.

If future is not ready upon first poll, no more polling will be performed. You need to hook up to external event (timer in your case) and perform waker.wake().
Waker is async runtime specific callback to tell runtime "ok, this time I am ready, poll me again".

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.