1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! `groundhog` - A rolling timer
//!
//! Sometimes you just want a simple rolling timer.
//!
//! Make sure you poll it often enough.

#![cfg_attr(not(test), no_std)]

use sealed::{RollingSince, Promote};
use core::ops::Div;

pub trait RollingTimer {
    type Tick: RollingSince + Promote + Div<Output = Self::Tick>;

    const TICKS_PER_SECOND: Self::Tick;

    /// Get the current tick
    fn get_ticks(&self) -> Self::Tick;

    /// Get the number of ticks since the other measurement
    ///
    /// Make sure the old value isn't too stale.
    fn ticks_since(&self, rhs: Self::Tick) -> Self::Tick {
        self.get_ticks().since(rhs)
    }

    /// Get the number of whole seconds since the other measurement
    ///
    /// Make sure the old value isn't too stale
    fn seconds_since(&self, rhs: Self::Tick) -> Self::Tick {
        self.ticks_since(rhs) / Self::TICKS_PER_SECOND
    }

    /// Get the number of whole milliseconds since the other measurement
    ///
    /// Make sure the old value isn't too stale
    ///
    /// If the number of milliseconds is larger than Self::Tick::max(),
    /// then it will saturate at the max value
    fn millis_since(&self, rhs: Self::Tick) -> Self::Tick {
        let delta_tick = self.ticks_since(rhs);
        delta_tick.mul_then_div(<Self::Tick as RollingSince>::MILLIS_PER_SECOND, Self::TICKS_PER_SECOND)
    }

    /// Get the number of whole microseconds since the other measurement
    ///
    /// Make sure the old value isn't too stale
    ///
    /// If the number of microseconds is larger than Self::Tick::max(),
    /// then it will saturate at the max value
    fn micros_since(&self, rhs: Self::Tick) -> Self::Tick {
        let delta_tick = self.ticks_since(rhs);
        delta_tick.mul_then_div(<Self::Tick as RollingSince>::MICROS_PER_SECOND, Self::TICKS_PER_SECOND)
    }
}



mod sealed {
    use core::convert::TryInto;
    use core::ops::{Mul, Div};

    pub trait Promote: Sized + Copy {
        type NextSize: From<Self> + Ord + TryInto<Self> + Mul<Output = Self::NextSize> + Div<Output = Self::NextSize>;
        const MAX_VAL: Self::NextSize;

        fn promote(&self) -> Self::NextSize {
            (*self).into()
        }
        fn saturate_demote(other: Self::NextSize) -> Self {
            match Self::MAX_VAL.min(other).try_into() {
                Ok(t) => t,
                Err(_) => unsafe {
                    core::hint::unreachable_unchecked()
                }
            }
        }

        fn mul_then_div(&self, mul: Self, div: Self) -> Self {
            Self::saturate_demote((self.promote() * mul.promote()) / div.promote())
        }
    }

    pub trait RollingSince {
        const MILLIS_PER_SECOND: Self;
        const MICROS_PER_SECOND: Self;
        fn since(&self, other: Self) -> Self;
    }

    impl Promote for u32 {
        type NextSize = u64;
        const MAX_VAL: u64 = 0xFFFF_FFFF;
    }

    #[cfg(feature = "u128")]
    impl Promote for u64 {
        type NextSize = u128;
        const MAX_VAL: u128 = 0xFFFF_FFFF_FFFF_FFFF;
    }

    impl RollingSince for u32 {
        const MILLIS_PER_SECOND: u32 = 1_000;
        const MICROS_PER_SECOND: u32 = 1_000_000;
        fn since(&self, other: u32) -> u32 {
            self.wrapping_sub(other)
        }
    }

    #[cfg(feature = "u128")]
    impl RollingSince for u64 {
        const MILLIS_PER_SECOND: u64 = 1_000;
        const MICROS_PER_SECOND: u64 = 1_000_000;
        fn since(&self, other: u64) -> u64 {
            self.wrapping_sub(other)
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use core::sync::atomic::{AtomicU32, Ordering};

    struct TestTimer(&'static AtomicU32);
    impl RollingTimer for TestTimer {
        type Tick = u32;
        const TICKS_PER_SECOND: Self::Tick = 10;

        fn get_ticks(&self) -> u32 {
            self.0.load(Ordering::SeqCst)
        }
    }

    #[test]
    fn simple_wrap() {
        assert_eq!(0x10u32.since(0xFFFFFFF0), 0x20);
        assert_eq!(0xFFFFFFF0u32.since(0x10), 0xFFFFFFE0);
    }

    #[test]
    fn timer_test() {
        static TIMER: AtomicU32 = AtomicU32::new(20);
        let timer = TestTimer(&TIMER);

        assert_eq!(timer.ticks_since(0), 20);
        assert_eq!(timer.seconds_since(0), 2);
        assert_eq!(timer.millis_since(0), 2000);
        assert_eq!(timer.micros_since(0), 2000000);

        TIMER.store(0xFFFF_FFFF, Ordering::SeqCst);

        assert_eq!(timer.ticks_since(0), 0xFFFF_FFFF);
        assert_eq!(timer.seconds_since(0), 0xFFFF_FFFF / 10);

        // Out of range
        assert_eq!(timer.millis_since(0), 0xFFFF_FFFF);
        assert_eq!(timer.micros_since(0), 0xFFFF_FFFF);


        TIMER.store(0x4000_0000, Ordering::SeqCst);

        assert_eq!(timer.ticks_since(0xC000_0000), 0x8000_0000);
        assert_eq!(timer.seconds_since(0xC000_0000), 0x8000_0000 / 10);

        // Out of range
        assert_eq!(timer.millis_since(0xC000_0000), 0xFFFF_FFFF);
        assert_eq!(timer.micros_since(0xC000_0000), 0xFFFF_FFFF);
    }
}