Download this file
| 1 | /// Timestamp used by [crate::sync::Awareness] to tag most recent updates. |
| 2 | pub type Timestamp = u64; |
| 3 | |
| 4 | /// A clock trait used to obtain the current time. |
| 5 | pub trait Clock: Send + Sync { |
| 6 | fn now(&self) -> Timestamp; |
| 7 | } |
| 8 | |
| 9 | impl<F> Clock for F |
| 10 | where |
| 11 | F: Fn() -> Timestamp + Send + Sync, |
| 12 | { |
| 13 | #[inline] |
| 14 | fn now(&self) -> Timestamp { |
| 15 | self() |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /// A clock which uses standard (non-monotonic) OS date time. |
| 20 | #[cfg(not(target_family = "wasm"))] |
| 21 | #[derive(Debug, Copy, Clone, Default)] |
| 22 | pub struct SystemClock; |
| 23 | |
| 24 | #[cfg(not(target_family = "wasm"))] |
| 25 | impl Clock for SystemClock { |
| 26 | fn now(&self) -> Timestamp { |
| 27 | std::time::SystemTime::now() |
| 28 | .duration_since(std::time::UNIX_EPOCH) |
| 29 | .unwrap() |
| 30 | .as_millis() as Timestamp |
| 31 | } |
| 32 | } |
| 33 | |