1use crate::errors::ParclMathErrorCode;
2use anchor_lang::prelude::*;
3
4pub trait TrySub: Sized {
5 fn try_sub(self, rhs: Self) -> Result<Self>;
6}
7
8pub trait TryAdd: Sized {
9 fn try_add(self, rhs: Self) -> Result<Self>;
10}
11
12pub trait TryDiv<T>: Sized {
13 fn try_div(self, rhs: T) -> Result<Self>;
14}
15
16pub trait TryMul<T>: Sized {
17 fn try_mul(self, rhs: T) -> Result<Self>;
18}
19
20pub trait TryRem<T>: Sized {
21 fn try_rem(self, rhs: T) -> Result<Self>;
22}
23
24impl TryAdd for u128 {
25 fn try_add(self, rhs: Self) -> Result<Self> {
26 let val = self
27 .checked_add(rhs)
28 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
29 Ok(val)
30 }
31}
32
33impl TrySub for u128 {
34 fn try_sub(self, rhs: Self) -> Result<Self> {
35 let val = self
36 .checked_sub(rhs)
37 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
38 Ok(val)
39 }
40}
41
42impl TryMul<u128> for u128 {
43 fn try_mul(self, rhs: u128) -> Result<Self> {
44 let val = self
45 .checked_mul(rhs)
46 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
47 Ok(val)
48 }
49}
50
51impl TryDiv<u128> for u128 {
52 fn try_div(self, rhs: u128) -> Result<Self> {
53 let val = self
54 .checked_div(rhs)
55 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
56 Ok(val)
57 }
58}
59
60impl TryRem<u128> for u128 {
61 fn try_rem(self, rhs: u128) -> Result<Self> {
62 let val = self
63 .checked_rem(rhs)
64 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
65 Ok(val)
66 }
67}
68
69impl TryAdd for u64 {
70 fn try_add(self, rhs: Self) -> Result<Self> {
71 let val = self
72 .checked_add(rhs)
73 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
74 Ok(val)
75 }
76}
77
78impl TrySub for u64 {
79 fn try_sub(self, rhs: Self) -> Result<Self> {
80 let val = self
81 .checked_sub(rhs)
82 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
83 Ok(val)
84 }
85}
86
87impl TryMul<u64> for u64 {
88 fn try_mul(self, rhs: u64) -> Result<Self> {
89 let val = self
90 .checked_mul(rhs)
91 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
92 Ok(val)
93 }
94}
95
96impl TryDiv<u64> for u64 {
97 fn try_div(self, rhs: u64) -> Result<Self> {
98 let val = self
99 .checked_div(rhs)
100 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
101 Ok(val)
102 }
103}
104
105impl TryRem<u64> for u64 {
106 fn try_rem(self, rhs: u64) -> Result<Self> {
107 let val = self
108 .checked_rem(rhs)
109 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
110 Ok(val)
111 }
112}
113
114impl TryAdd for u32 {
115 fn try_add(self, rhs: Self) -> Result<Self> {
116 let val = self
117 .checked_add(rhs)
118 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
119 Ok(val)
120 }
121}
122
123impl TrySub for u32 {
124 fn try_sub(self, rhs: Self) -> Result<Self> {
125 let val = self
126 .checked_sub(rhs)
127 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
128 Ok(val)
129 }
130}
131
132impl TryMul<u32> for u32 {
133 fn try_mul(self, rhs: u32) -> Result<Self> {
134 let val = self
135 .checked_mul(rhs)
136 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
137 Ok(val)
138 }
139}
140
141impl TryDiv<u32> for u32 {
142 fn try_div(self, rhs: u32) -> Result<Self> {
143 let val = self
144 .checked_div(rhs)
145 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
146 Ok(val)
147 }
148}
149
150impl TryRem<u32> for u32 {
151 fn try_rem(self, rhs: u32) -> Result<Self> {
152 let val = self
153 .checked_rem(rhs)
154 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
155 Ok(val)
156 }
157}
158
159impl TryAdd for i128 {
160 fn try_add(self, rhs: Self) -> Result<Self> {
161 let val = self
162 .checked_add(rhs)
163 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
164 Ok(val)
165 }
166}
167
168impl TrySub for i128 {
169 fn try_sub(self, rhs: Self) -> Result<Self> {
170 let val = self
171 .checked_sub(rhs)
172 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
173 Ok(val)
174 }
175}
176
177impl TryMul<i128> for i128 {
178 fn try_mul(self, rhs: i128) -> Result<Self> {
179 let val = self
180 .checked_mul(rhs)
181 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
182 Ok(val)
183 }
184}
185
186impl TryDiv<i128> for i128 {
187 fn try_div(self, rhs: i128) -> Result<Self> {
188 let val = self
189 .checked_div(rhs)
190 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
191 Ok(val)
192 }
193}
194
195impl TryRem<i128> for i128 {
196 fn try_rem(self, rhs: i128) -> Result<Self> {
197 let val = self
198 .checked_rem(rhs)
199 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
200 Ok(val)
201 }
202}
203
204impl TryAdd for i64 {
205 fn try_add(self, rhs: Self) -> Result<Self> {
206 let val = self
207 .checked_add(rhs)
208 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
209 Ok(val)
210 }
211}
212
213impl TrySub for i64 {
214 fn try_sub(self, rhs: Self) -> Result<Self> {
215 let val = self
216 .checked_sub(rhs)
217 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
218 Ok(val)
219 }
220}
221
222impl TryMul<i64> for i64 {
223 fn try_mul(self, rhs: i64) -> Result<Self> {
224 let val = self
225 .checked_mul(rhs)
226 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
227 Ok(val)
228 }
229}
230
231impl TryDiv<i64> for i64 {
232 fn try_div(self, rhs: i64) -> Result<Self> {
233 let val = self
234 .checked_div(rhs)
235 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
236 Ok(val)
237 }
238}
239
240impl TryRem<i64> for i64 {
241 fn try_rem(self, rhs: i64) -> Result<Self> {
242 let val = self
243 .checked_rem(rhs)
244 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
245 Ok(val)
246 }
247}
248
249impl TryAdd for i32 {
250 fn try_add(self, rhs: Self) -> Result<Self> {
251 let val = self
252 .checked_add(rhs)
253 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
254 Ok(val)
255 }
256}
257
258impl TrySub for i32 {
259 fn try_sub(self, rhs: Self) -> Result<Self> {
260 let val = self
261 .checked_sub(rhs)
262 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
263 Ok(val)
264 }
265}
266
267impl TryMul<i32> for i32 {
268 fn try_mul(self, rhs: i32) -> Result<Self> {
269 let val = self
270 .checked_mul(rhs)
271 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
272 Ok(val)
273 }
274}
275
276impl TryDiv<i32> for i32 {
277 fn try_div(self, rhs: i32) -> Result<Self> {
278 let val = self
279 .checked_div(rhs)
280 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
281 Ok(val)
282 }
283}
284
285impl TryRem<i32> for i32 {
286 fn try_rem(self, rhs: i32) -> Result<Self> {
287 let val = self
288 .checked_rem(rhs)
289 .ok_or(ParclMathErrorCode::IntegerOverflow)?;
290 Ok(val)
291 }
292}