1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::rc::Rc;

use crate::DriverTrait;

pub type InstantType = u64;

/// Monotonically nondecrasing clock using a driver, similar to [std::time::Instant].
#[derive(Clone)]
pub struct Instant {
    driver: Rc<dyn DriverTrait>,
    pub instant: InstantType,
}

impl Instant {
    pub fn now(driver: Rc<dyn DriverTrait>) -> Self {
        Self {
            instant: driver.now(),
            driver,
        }
    }

    pub fn refresh(&self) -> Self {
        Self {
            instant: self.driver.now(),
            driver: self.driver.clone(),
        }
    }

    pub fn elapsed(&self) -> InstantType {
        self.driver.now() - self.instant
    }

    pub fn seconds_elapsed(&self) -> InstantType {
        self.elapsed() / 1000
    }
}

impl PartialEq for Instant {
    fn eq(&self, other: &Self) -> bool {
        self.instant == other.instant
    }
}