timeout_trait/
fake_impls.rs

1use super::{
2    fugit::{KilohertzU32, RateExtU32},
3    prelude::*,
4    tick_impl::{TickTimeoutNs, TickTimeoutState},
5};
6use core::sync::atomic::{AtomicU32, Ordering};
7
8pub type FakeTimeoutNs = TickTimeoutNs<FakeInstant>;
9pub type FakeTimeoutState = TickTimeoutState<FakeInstant>;
10
11static COUNTER: AtomicU32 = AtomicU32::new(0);
12
13#[derive(Clone, Copy)]
14pub struct FakeInstant {
15    count: u32,
16}
17
18impl TickInstant for FakeInstant {
19    fn frequency() -> KilohertzU32 {
20        1.kHz()
21    }
22
23    fn now() -> Self {
24        COUNTER.fetch_add(1, Ordering::Relaxed);
25        Self {
26            count: COUNTER.load(Ordering::Relaxed),
27        }
28    }
29
30    fn tick_since(self, earlier: Self) -> u32 {
31        self.count.wrapping_sub(earlier.count)
32    }
33}