Skip to main content

tempa/frame_index/
impl_ops.rs

1use core::ops::{Add, AddAssign, Sub, SubAssign};
2
3use rinia::numeric::{SaturatingAdd, SaturatingSub};
4
5use crate::FrameIndex;
6
7// FrameIndex + FrameIndex = FrameIndex
8impl<T> Add for FrameIndex<T>
9where
10    T: Add<Output = T>,
11{
12    type Output = Self;
13
14    #[inline]
15    fn add(self, rhs: Self) -> Self::Output {
16        Self(self.0 + rhs.0)
17    }
18}
19
20// FrameIndex += FrameIndex
21impl<T> AddAssign for FrameIndex<T>
22where
23    T: AddAssign,
24{
25    #[inline]
26    fn add_assign(&mut self, rhs: Self) {
27        self.0 += rhs.0;
28    }
29}
30
31// FrameIndex - FrameIndex = FrameIndex
32impl<T> Sub for FrameIndex<T>
33where
34    T: Sub<Output = T>,
35{
36    type Output = Self;
37
38    #[inline]
39    fn sub(self, rhs: Self) -> Self::Output {
40        Self(self.0 - rhs.0)
41    }
42}
43
44// FrameIndex -= FrameIndex
45impl<T> SubAssign for FrameIndex<T>
46where
47    T: SubAssign,
48{
49    #[inline]
50    fn sub_assign(&mut self, rhs: Self) {
51        self.0 -= rhs.0;
52    }
53}
54
55// FrameIndex + T = FrameIndex
56impl<T> Add<T> for FrameIndex<T>
57where
58    T: Add<Output = T>,
59{
60    type Output = Self;
61
62    #[inline]
63    fn add(self, rhs: T) -> Self::Output {
64        Self(self.0 + rhs)
65    }
66}
67
68// FrameIndex += T
69impl<T> AddAssign<T> for FrameIndex<T>
70where
71    T: AddAssign,
72{
73    #[inline]
74    fn add_assign(&mut self, rhs: T) {
75        self.0 += rhs;
76    }
77}
78
79// FrameIndex - T = FrameIndex
80impl<T> Sub<T> for FrameIndex<T>
81where
82    T: Sub<Output = T>,
83{
84    type Output = Self;
85
86    #[inline]
87    fn sub(self, rhs: T) -> Self::Output {
88        Self(self.0 - rhs)
89    }
90}
91
92// FrameIndex -= T
93impl<T> SubAssign<T> for FrameIndex<T>
94where
95    T: SubAssign,
96{
97    #[inline]
98    fn sub_assign(&mut self, rhs: T) {
99        self.0 -= rhs;
100    }
101}
102
103// SaturatingAdd inherent
104impl<T> FrameIndex<T>
105where
106    T: SaturatingAdd<Output = T>,
107{
108    /// Returns the result of saturating addition of two [FrameIndex] values.
109    #[inline]
110    pub fn saturating_add(self, rhs: Self) -> Self {
111        let value = self.0.saturating_add(rhs.0);
112        Self(value)
113    }
114
115    /// Returns the result of saturating addition of a [FrameIndex] value and a raw frame value.
116    #[inline]
117    pub fn saturating_add_t(self, rhs: T) -> Self {
118        let value = self.0.saturating_add(rhs);
119        Self(value)
120    }
121}
122
123// FrameIndex<T>.saturating_add(FrameIndex<T>) = FrameIndex<T>
124impl<T> SaturatingAdd for FrameIndex<T>
125where
126    T: SaturatingAdd<Output = T>,
127{
128    type Output = Self;
129
130    #[inline]
131    fn saturating_add(self, rhs: Self) -> Self::Output {
132        FrameIndex::saturating_add(self, rhs)
133    }
134}
135
136// FrameIndex<T>.saturating_add(FrameIndex<T>) = FrameIndex<T>
137impl<T> SaturatingAdd<T> for FrameIndex<T>
138where
139    T: SaturatingAdd<Output = T>,
140{
141    type Output = Self;
142
143    #[inline]
144    fn saturating_add(self, rhs: T) -> Self::Output {
145        FrameIndex::saturating_add_t(self, rhs)
146    }
147}
148
149// SaturatingSub inherent
150impl<T> FrameIndex<T>
151where
152    T: SaturatingSub<Output = T>,
153{
154    /// Returns the result of saturating subtraction of two [FrameIndex] values.
155    #[inline]
156    pub fn saturating_sub(self, rhs: Self) -> Self {
157        let value = self.0.saturating_sub(rhs.0);
158        Self(value)
159    }
160
161    /// Returns the result of saturating subtraction of a [FrameIndex] value and a raw frame value.
162    #[inline]
163    pub fn saturating_sub_t(self, rhs: T) -> Self {
164        let value = self.0.saturating_sub(rhs);
165        Self(value)
166    }
167}
168
169// FrameIndex<T>.saturating_sub(FrameIndex<T>) = FrameIndex<T>
170impl<T> SaturatingSub for FrameIndex<T>
171where
172    T: SaturatingSub<Output = T>,
173{
174    type Output = Self;
175
176    #[inline]
177    fn saturating_sub(self, rhs: Self) -> Self::Output {
178        FrameIndex::saturating_sub(self, rhs)
179    }
180}
181
182// FrameIndex<T>.saturating_sub(T) = FrameIndex<T>
183impl<T> SaturatingSub<T> for FrameIndex<T>
184where
185    T: SaturatingSub<Output = T>,
186{
187    type Output = Self;
188
189    #[inline]
190    fn saturating_sub(self, rhs: T) -> Self::Output {
191        FrameIndex::saturating_sub_t(self, rhs)
192    }
193}