mm1_proto_timer/
lib.rs

1// #![warn(missing_docs)]
2#![warn(rust_2018_idioms)]
3#![warn(unreachable_pub)]
4
5use std::hash::Hash;
6use std::ops::{Add, Sub};
7
8use mm1_proto::{Message, message};
9
10pub trait Timer: Send + 'static {
11    type Key: Message + Ord + Clone + Send + Hash;
12    type Instant: Copy
13        + Send
14        + Ord
15        + Add<Self::Duration, Output = Self::Instant>
16        + Sub<Self::Instant, Output = Self::Duration>;
17    type Duration: Copy + Send + Default;
18    type Message: Message;
19
20    fn sleep(d: Self::Duration) -> impl Future<Output = ()> + Send;
21
22    #[doc(hidden)]
23    fn instant_plus_duration(t: Self::Instant, d: Self::Duration) -> Self::Instant {
24        t + d
25    }
26}
27
28#[message(base_path = ::mm1_proto)]
29pub struct ScheduleOnce<T: Timer> {
30    pub key: T::Key,
31    #[serde(skip)]
32    pub at:  T::Instant,
33    pub msg: T::Message,
34}
35
36#[message(base_path = ::mm1_proto)]
37pub struct SchedulePeriodic<T: Timer>
38where
39    T::Message: Clone,
40{
41    pub key:    T::Key,
42    pub delay:  T::Instant,
43    pub period: T::Duration,
44    pub msg:    T::Message,
45}
46
47#[message(base_path = ::mm1_proto)]
48pub struct Cancel<T: Timer> {
49    pub key: T::Key,
50}