Skip to main content

deep_time/t_span/
ops.rs

1use crate::TSpan;
2use core::cmp::Ordering;
3use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
4
5impl Add<TSpan> for TSpan {
6    type Output = Self;
7
8    /// Adds two `TSpan`s.
9    #[inline]
10    fn add(self, rhs: TSpan) -> Self {
11        self.add(rhs)
12    }
13}
14
15impl AddAssign<TSpan> for TSpan {
16    /// Adds a `TSpan` to this one in place.
17    #[inline]
18    fn add_assign(&mut self, rhs: TSpan) {
19        *self = self.add(rhs);
20    }
21}
22
23impl Sub<TSpan> for TSpan {
24    type Output = Self;
25
26    /// Subtracts a `TSpan` from this one.
27    #[inline]
28    fn sub(self, rhs: TSpan) -> Self {
29        self.sub(rhs)
30    }
31}
32
33impl SubAssign<TSpan> for TSpan {
34    /// Subtracts a `TSpan` from this one in place.
35    #[inline]
36    fn sub_assign(&mut self, rhs: TSpan) {
37        *self = self.sub(rhs);
38    }
39}
40
41impl Neg for TSpan {
42    type Output = Self;
43
44    /// Negates this `TSpan` (returns the additive inverse).
45    #[inline]
46    fn neg(self) -> Self {
47        self.neg()
48    }
49}
50
51impl Mul<i64> for TSpan {
52    type Output = Self;
53
54    /// Multiplies this `TSpan` by an integer scalar.
55    #[inline]
56    fn mul(self, rhs: i64) -> Self {
57        self.mul(rhs)
58    }
59}
60
61impl MulAssign<i64> for TSpan {
62    /// Multiplies this `TSpan` by an integer scalar in place.
63    #[inline]
64    fn mul_assign(&mut self, rhs: i64) {
65        *self = self.mul(rhs);
66    }
67}
68
69impl Div<i64> for TSpan {
70    type Output = Self;
71
72    /// Divides this `TSpan` by an integer scalar.
73    #[inline]
74    fn div(self, rhs: i64) -> Self {
75        self.div(rhs)
76    }
77}
78
79impl DivAssign<i64> for TSpan {
80    /// Divides this `TSpan` by an integer scalar in place.
81    #[inline]
82    fn div_assign(&mut self, rhs: i64) {
83        *self = self.div(rhs);
84    }
85}
86
87impl TSpan {
88    /// Compares two `TSpan`s by their `(sec, attos)` representation.
89    ///
90    /// This is a `const fn` so it can be used in const contexts.
91    pub const fn cmp(self, other: Self) -> Ordering {
92        if self.sec < other.sec {
93            Ordering::Less
94        } else if self.sec > other.sec {
95            Ordering::Greater
96        } else if self.attos < other.attos {
97            Ordering::Less
98        } else if self.attos > other.attos {
99            Ordering::Greater
100        } else {
101            Ordering::Equal
102        }
103    }
104
105    /// Returns the smaller of two `TSpan`s.
106    ///
107    /// This is a `const fn`.
108    pub const fn min(self, other: Self) -> Self {
109        match self.cmp(other) {
110            Ordering::Greater => other,
111            _ => self,
112        }
113    }
114
115    /// Returns the larger of two `TSpan`s.
116    ///
117    /// This is a `const fn`.
118    pub const fn max(self, other: Self) -> Self {
119        match self.cmp(other) {
120            Ordering::Less => other,
121            _ => self,
122        }
123    }
124
125    /// Returns `true` if this `TSpan` is less than the other.
126    ///
127    /// This is a `const fn` so it can be used in const contexts.
128    pub const fn lt(self, other: Self) -> bool {
129        matches!(self.cmp(other), Ordering::Less)
130    }
131
132    /// Returns `true` if this `TSpan` is greater than the other.
133    ///
134    /// This is a `const fn` so it can be used in const contexts.
135    pub const fn gt(self, other: Self) -> bool {
136        matches!(self.cmp(other), Ordering::Greater)
137    }
138
139    /// Returns `true` if this `TSpan` is less than or equal to the other.
140    ///
141    /// This is a `const fn` so it can be used in const contexts.
142    pub const fn le(self, other: Self) -> bool {
143        !matches!(self.cmp(other), Ordering::Greater)
144    }
145
146    /// Returns `true` if this `TSpan` is greater than or equal to the other.
147    ///
148    /// This is a `const fn` so it can be used in const contexts.
149    pub const fn ge(self, other: Self) -> bool {
150        !matches!(self.cmp(other), Ordering::Less)
151    }
152}