Is there a crate that handles alarms?

I am looking for a crate that would offer something like the following methods. Does anybody know if such a crate exists?

trait Alarm {
  /// Call a callback after a given delay.
  fn schedule_with_delay(&self, cb: Box<FnOnce()>, delay: Duration);

  /// Call a callback once a given date&time has been reached.
  fn schedule_with_timestap(&self, cb: Box<FnOnce()>, timestamp: DateTime<UTC>);
}

I don't know, but the schedule_with_delay could be implemented by spawning a thread that calls the callback after thread::sleep(some_duration) returned.

Well, I don't want to spawn a new thread for every single call to schedule_with_delay. I am planning to have several dozens/hundreds of them.

I understand, but you want to have non-blocking behaviour. I've currently no (easy) idea how to achieve this \wo spawning another thread that sleeps until the duration ends.

Well, I'm pretty sure this can be implemented with park_timeout and select. But I'd rather avoid reimplementing that if I can.

Possible reference: servo/timer_scheduler.rs at d3e2f94f2024f4735f836588ed11303a0abafdf8 · servo/servo · GitHub

edit Added reference.

1 Like

Ah, well, I went ahead and wrote one. Feedback welcome.