timeout_trait/
fake_impls.rs

1use super::{
2    fugit::{KilohertzU32, RateExtU32},
3    prelude::*,
4    tick_impl::{TickTimeoutNs, TickTimeoutState},
5};
6use core::cell::Cell;
7
8pub type FakeTimeoutNs = TickTimeoutNs<FakeInstant>;
9pub type FakeTimeoutState = TickTimeoutState<FakeInstant>;
10
11static COUNTER: critical_section::Mutex<Cell<u32>> = critical_section::Mutex::new(Cell::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        critical_section::with(|cs| {
25            let c = COUNTER.borrow(cs).get() + 1;
26            COUNTER.borrow(cs).set(c);
27            Self { count: c }
28        })
29    }
30
31    fn tick_since(self, earlier: Self) -> u32 {
32        self.count.wrapping_sub(earlier.count)
33    }
34}