vertigo/
instant.rs

1use crate::ApiImport;
2use std::time::Duration;
3
4/// Duration in seconds, returned from [Instant] methods.
5pub type InstantType = u64;
6
7/// Monotonically non-decreasing clock using a driver, similar to [std::time::Instant].
8#[derive(Clone)]
9pub struct Instant {
10    api: ApiImport,
11    pub instant: InstantType,
12}
13
14impl Instant {
15    pub fn now(api: ApiImport) -> Self {
16        Self {
17            instant: api.instant_now(),
18            api,
19        }
20    }
21
22    #[must_use]
23    pub fn refresh(&self) -> Self {
24        Self {
25            instant: self.api.instant_now(),
26            api: self.api.clone(),
27        }
28    }
29
30    pub fn elapsed(&self) -> InstantType {
31        self.api.instant_now() - self.instant
32    }
33
34    pub fn seconds_elapsed(&self) -> InstantType {
35        self.elapsed() / 1000
36    }
37
38    pub fn add_duration(&self, time: Duration) -> Self {
39        let new_instant = self.instant + time.as_millis() as u64;
40
41        Self {
42            api: self.api.clone(),
43            instant: new_instant,
44        }
45    }
46
47    pub fn is_expire(&self) -> bool {
48        self.api.instant_now() > self.instant
49    }
50}
51
52impl PartialEq for Instant {
53    fn eq(&self, other: &Self) -> bool {
54        self.instant == other.instant
55    }
56}