Is there a crate that handles alarms?


#1

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>);
}

#2

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.


#3

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.


#4

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.


#5

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: https://github.com/servo/servo/blob/d3e2f94f2024f4735f836588ed11303a0abafdf8/components/compositing/timer_scheduler.rs

edit Added reference.


#6

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