accel_stepper/
clock.rs

1use core::time::Duration;
2
3/// Something which records the elapsed real time.
4///
5/// This uses shared references because it may be shared between multiple
6/// components at any one time.
7pub trait SystemClock {
8    /// The amount of time that has passed since a clock-specific reference
9    /// point (e.g. device startup or the unix epoch).
10    fn elapsed(&self) -> Duration;
11}
12
13impl<'a, C: SystemClock> SystemClock for &'a C {
14    fn elapsed(&self) -> Duration { (*self).elapsed() }
15}
16
17/// A monotonically non-decreasing clock backed by the operating system.
18///
19/// Requires the `std` feature.
20#[cfg(feature = "std")]
21#[derive(Debug, Clone, PartialEq)]
22pub struct OperatingSystemClock {
23    created_at: std::time::Instant,
24}
25
26#[cfg(feature = "std")]
27impl OperatingSystemClock {
28    pub fn new() -> OperatingSystemClock { OperatingSystemClock::default() }
29}
30
31#[cfg(feature = "std")]
32impl SystemClock for OperatingSystemClock {
33    fn elapsed(&self) -> Duration { self.created_at.elapsed() }
34}
35
36#[cfg(feature = "std")]
37impl Default for OperatingSystemClock {
38    fn default() -> OperatingSystemClock {
39        OperatingSystemClock {
40            created_at: std::time::Instant::now(),
41        }
42    }
43}