I'm exploring timers, using (again) the echo-udp example as a starting point.
I wanted to extend it so that, on receipt of a datagram, it would do something after a delay, perhaps answer back as before, or maybe just print something to the console. In either case, the delay shouldn't block other datagrams from being received and processed.
There is a tokio-timer crate, but after studying the documents for that, I still quite couldn't figure out how to approach the problem. I tried modifying the poll()
method to simply print a message after a delay. The whole method was this:
fn poll(&mut self) -> Poll<(), io::Error> {
loop {
if let Some((size, peer)) = self.to_send {
let timer = Timer::default();
let sleep = timer.sleep(Duration::from_millis(500));
sleep.and_then(|result| {
println!("timer expired {:?}", result);
});
self.to_send = None;
}
self.to_send = Some(try_nb!(self.socket.recv_from(&mut self.buf)));
}
}
That didn't compile.
error[E0277]: the trait bound `(): futures::Future` is not satisfied
--> src/main.rs:47:23
|
47 | sleep.and_then(|result| {
| ^^^^^^^^ the trait `futures::Future` is not implemented for `()`
|
= note: required because of the requirements on the impl of `futures::IntoFuture` for `()`
The error message may be because the Item type alias defined in the tokio_timer::Sleep
struct is ()
(the unit type). If that's the case, then maybe and_then()
can't be used here? What's the technique for doing something when a timer expires and how do you squirrel away information before starting the timer that the timeout can use later?
Thanks,
Chuck