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
use crate::TSpan;
use core::cmp::Ordering;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
impl Add<TSpan> for TSpan {
type Output = Self;
/// Adds two `TSpan`s.
#[inline]
fn add(self, rhs: TSpan) -> Self {
self.add(rhs)
}
}
impl AddAssign<TSpan> for TSpan {
/// Adds a `TSpan` to this one in place.
#[inline]
fn add_assign(&mut self, rhs: TSpan) {
*self = self.add(rhs);
}
}
impl Sub<TSpan> for TSpan {
type Output = Self;
/// Subtracts a `TSpan` from this one.
#[inline]
fn sub(self, rhs: TSpan) -> Self {
self.sub(rhs)
}
}
impl SubAssign<TSpan> for TSpan {
/// Subtracts a `TSpan` from this one in place.
#[inline]
fn sub_assign(&mut self, rhs: TSpan) {
*self = self.sub(rhs);
}
}
impl Neg for TSpan {
type Output = Self;
/// Negates this `TSpan` (returns the additive inverse).
#[inline]
fn neg(self) -> Self {
self.neg()
}
}
impl Mul<i64> for TSpan {
type Output = Self;
/// Multiplies this `TSpan` by an integer scalar.
#[inline]
fn mul(self, rhs: i64) -> Self {
self.mul(rhs)
}
}
impl MulAssign<i64> for TSpan {
/// Multiplies this `TSpan` by an integer scalar in place.
#[inline]
fn mul_assign(&mut self, rhs: i64) {
*self = self.mul(rhs);
}
}
impl Div<i64> for TSpan {
type Output = Self;
/// Divides this `TSpan` by an integer scalar.
#[inline]
fn div(self, rhs: i64) -> Self {
self.div(rhs)
}
}
impl DivAssign<i64> for TSpan {
/// Divides this `TSpan` by an integer scalar in place.
#[inline]
fn div_assign(&mut self, rhs: i64) {
*self = self.div(rhs);
}
}
impl TSpan {
/// Compares two `TSpan`s by their `(sec, attos)` representation.
///
/// This is a `const fn` so it can be used in const contexts.
pub const fn cmp(self, other: Self) -> Ordering {
if self.sec < other.sec {
Ordering::Less
} else if self.sec > other.sec {
Ordering::Greater
} else if self.attos < other.attos {
Ordering::Less
} else if self.attos > other.attos {
Ordering::Greater
} else {
Ordering::Equal
}
}
/// Returns the smaller of two `TSpan`s.
///
/// This is a `const fn`.
pub const fn min(self, other: Self) -> Self {
match self.cmp(other) {
Ordering::Greater => other,
_ => self,
}
}
/// Returns the larger of two `TSpan`s.
///
/// This is a `const fn`.
pub const fn max(self, other: Self) -> Self {
match self.cmp(other) {
Ordering::Less => other,
_ => self,
}
}
/// Returns `true` if this `TSpan` is less than the other.
///
/// This is a `const fn` so it can be used in const contexts.
pub const fn lt(self, other: Self) -> bool {
matches!(self.cmp(other), Ordering::Less)
}
/// Returns `true` if this `TSpan` is greater than the other.
///
/// This is a `const fn` so it can be used in const contexts.
pub const fn gt(self, other: Self) -> bool {
matches!(self.cmp(other), Ordering::Greater)
}
/// Returns `true` if this `TSpan` is less than or equal to the other.
///
/// This is a `const fn` so it can be used in const contexts.
pub const fn le(self, other: Self) -> bool {
!matches!(self.cmp(other), Ordering::Greater)
}
/// Returns `true` if this `TSpan` is greater than or equal to the other.
///
/// This is a `const fn` so it can be used in const contexts.
pub const fn ge(self, other: Self) -> bool {
!matches!(self.cmp(other), Ordering::Less)
}
}