button_driver/
instant.rs

1use core::{ops::Sub, time::Duration};
2
3/// An abstraction for retrieving the current time.
4///
5/// The underlying counter shell be monotonic in order for the crate to
6/// operate correctly.
7pub trait InstantProvider<D = Duration>
8where
9    // `Clone` is less strict then `Copy` and usually implemented using it.
10    Self: Sub<Self, Output = D> + Clone,
11{
12    /// Returns an instant corresponding to "now".
13    fn now() -> Self;
14
15    /// Returns the amount of time elapsed since this instant.
16    fn elapsed(&self) -> D {
17        Self::now() - self.clone()
18    }
19}
20
21#[cfg(feature = "std")]
22impl InstantProvider<std::time::Duration> for std::time::Instant {
23    fn now() -> Self {
24        std::time::Instant::now()
25    }
26}
27
28#[cfg(feature = "embassy")]
29impl InstantProvider<embassy_time::Duration> for embassy_time::Instant {
30    fn now() -> Self {
31        embassy_time::Instant::now()
32    }
33}