Continuing the discussion from Is Rust getting it's message out?:
Most of the complexity comes from the use of abortable
from the futures crate. Hopefully this version without cancellation is easier to understand:
fn set_interval<F, Fut>(mut f: F, dur: Duration)
where
F: Send + 'static + FnMut() -> Fut,
Fut: Future<Output = ()> + Send + 'static,
{
// Create stream of intervals.
let mut interval = time::interval(dur);
tokio::spawn(async move {
// Skip the first tick at 0ms.
interval.tick().await;
loop {
// Wait until next tick.
interval.tick().await;
// Spawn a task for this tick.
tokio::spawn(f());
}
});
}