I want to wait for messages on a future's channel with variable timeout and have something like this:
async fn process(mut recv: UnboundedReceiver<Event>) {
let mut timer = Delay::new(Duration::from_millis(10)).fuse();
loop {
select! {
ev = recv.next() => {
// handle event
}
_ = timer => {
// handle timeout
}
}
// want to do this now but can't since timer is fused
timer.reset(Instant::now() + new_calculated_timeout);
// have to do it this (wasteful) way instead
timer = Delay::new(new_calculated_timeout).fuse();
}
}
I'm thinking there must be some way to do this without having to create a new timer each time.. Am I trying to solve this with the wrong timer api?
Hmm, I don't see how I can implement future for Delay without modifying the source code. How would I forward the poll call to Delay (which is private)? For my purpose I'm ok with just implementing FusedFuture for Delay directly and keep a custom crate for futures-timer.
I think your not understanding what I mean be wrapping. You seem to be describing an extension trait. I am referring to a new structure composed of the original with similar methods/traits.
Yes this is what I have but struct FusedDelay { delay: Delay }. Implementing FusedFuture is easy, however I also need to implement the Future trait for FusedDelay. From FusedDelay I can't access any of Delay's private functions or fields so there's no easy way of forwarding the poll() call from FusedDelay to Delay.