Skip to main content

temporal_control/
ticks.rs

1//! Integer time ticks.
2
3/// A point or duration in time, in integer ticks.
4///
5/// Integer ticks keep arithmetic deterministic across targets. The tick length (for example one
6/// millisecond) is fixed by the integrator, not by this crate. All arithmetic on ticks inside
7/// this crate is checked; overflow is reported as [`Error::Overflow`](crate::Error::Overflow).
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
9pub struct Ticks(pub i64);
10
11impl Ticks {
12    /// Zero ticks.
13    pub const ZERO: Self = Self(0);
14
15    /// Adds two tick values, returning `None` on overflow.
16    #[must_use]
17    pub const fn checked_add(self, rhs: Self) -> Option<Self> {
18        match self.0.checked_add(rhs.0) {
19            Some(v) => Some(Self(v)),
20            None => None,
21        }
22    }
23
24    /// Subtracts `rhs`, returning `None` on overflow.
25    #[must_use]
26    pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
27        match self.0.checked_sub(rhs.0) {
28            Some(v) => Some(Self(v)),
29            None => None,
30        }
31    }
32
33    /// Negates a tick value, returning `None` for `i64::MIN`.
34    #[must_use]
35    pub const fn checked_neg(self) -> Option<Self> {
36        match self.0.checked_neg() {
37            Some(v) => Some(Self(v)),
38            None => None,
39        }
40    }
41}