Skip to main content

rsynko_session/
clock.rs

1use ambassador::delegatable_trait;
2use std::time::Duration;
3
4/// Specifies reading the passage of time.
5///
6/// Time is read rather than taken, so how long a run has been running is a statement about a
7/// clock the interpreter supplies and not about the one this machine happens to have.
8#[delegatable_trait]
9pub trait ClockAlg {
10    /// Represents one reading of the clock.
11    type Moment;
12
13    /// Reads the clock now.
14    fn now(&self) -> Self::Moment;
15
16    /// States how long it has been since one reading.
17    fn since(&self, moment: &Self::Moment) -> Duration;
18}
19
20/// Denotes one run, and the time it has spent running and held still.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub struct Attending<Id, Run, Moment> {
23    /// Names the request the run works on behalf of.
24    pub id: Id,
25    /// Carries the run itself.
26    pub run: Run,
27    /// Denotes when the run began.
28    pub began: Moment,
29    /// Denotes when it was last held still, while it is still being held.
30    pub held_since: Option<Moment>,
31    /// Denotes how long it was held still before that.
32    pub held_for: Duration,
33}
34
35impl<Id, Run, Moment> Attending<Id, Run, Moment> {
36    /// Denotes one run that has just begun, and has not been held still yet.
37    pub const fn begun(id: Id, run: Run, began: Moment) -> Self {
38        Self { id, run, began, held_since: None, held_for: Duration::ZERO }
39    }
40}