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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use super::*;

use std::ops::Add;
use std::ops::AddAssign;
use std::ops::BitAnd;
use std::ops::BitAndAssign;
use std::ops::BitOr;
use std::ops::BitOrAssign;
use std::ops::Div;
use std::ops::Mul;
use std::ops::Not;
use std::ops::Rem;
use std::ops::Shl;
use std::ops::ShlAssign;
use std::ops::Shr;
use std::ops::ShrAssign;
use std::ops::Sub;
use std::ops::SubAssign;

pub trait IntOps: IntOpsRust + IntOpsSafe + IntOpsTry {
}

impl <Val> IntOps for Val where Val: IntOpsRust + IntOpsSafe + IntOpsTry {
}

pub trait IntOpsRust: Sized + Add <Output = Self> + AddAssign + BitAnd <Output = Self>
	+ BitAndAssign + BitOr <Output = Self> + BitOrAssign + Div <Output = Self>
	+ Mul <Output = Self> + Not <Output = Self> + Rem <Output = Self> + Shl <u32, Output = Self>
	+ ShlAssign <u32> + Shr <u32, Output = Self> + ShrAssign <u32> + Sub <Output = Self>
	+ SubAssign {
}

impl <Val> IntOpsRust for Val where Val: Sized + Add <Output = Self> + AddAssign
	+ BitAnd <Output = Self> + BitAndAssign + BitOr <Output = Self> + BitOrAssign
	+ Div <Output = Self> + Mul <Output = Self> + Not <Output = Self> + Rem <Output = Self>
	+ Shl <u32, Output = Self> + ShlAssign <u32> + Shr <u32, Output = Self> + ShrAssign <u32>
	+ Sub <Output = Self> + SubAssign {
}

pub trait IntOpsSafe: Sized {

	#[ must_use ]
	fn safe_add (self, arg: Self) -> Self;

	#[ must_use ]
	fn safe_sub (self, arg: Self) -> Self;

}

pub trait IntOpsTry: Sized +
	TryAdd <Output = Self> + TryAddAssign +
	TryDiv <Output = Self> + TryDivAssign +
	TryMul <Output = Self> + TryMulAssign +
	TryRem <Output = Self> + TryRemAssign +
	TrySub <Output = Self> + TrySubAssign {
}

impl <Val> IntOpsTry for Val
	where Val: Sized +
		TryAdd <Output = Self> + TryAddAssign +
		TryDiv <Output = Self> + TryDivAssign +
		TryMul <Output = Self> + TryMulAssign +
		TryRem <Output = Self> + TryRemAssign +
		TrySub <Output = Self> + TrySubAssign {
}

pub trait TryAdd <Arg = Self> {
	type Output;
	fn try_add (self, arg: Arg) -> Result <Self::Output, Overflow>;
}

pub trait TryAddAssign <Arg = Self> {
	fn try_add_assign (& mut self, arg: Arg) -> Result <(), Overflow>;
}

pub trait TryDiv <Arg = Self> {
	type Output;
	fn try_div (self, arg: Arg) -> Result <Self::Output, Overflow>;
}

pub trait TryDivAssign <Arg = Self> {
	fn try_div_assign (& mut self, arg: Arg) -> Result <(), Overflow>;
}

pub trait TryMul <Arg = Self> {
	type Output;
	fn try_mul (self, arg: Arg) -> Result <Self::Output, Overflow>;
}

pub trait TryMulAssign <Arg = Self> {
	fn try_mul_assign (& mut self, arg: Arg) -> Result <(), Overflow>;
}

pub trait TryRem <Arg = Self> {
	type Output;
	fn try_rem (self, arg: Arg) -> Result <Self::Output, Overflow>;
}

pub trait TryRemAssign <Arg = Self> {
	fn try_rem_assign (& mut self, arg: Arg) -> Result <(), Overflow>;
}

pub trait TrySub <Arg = Self> {
	type Output;
	fn try_sub (self, arg: Arg) -> Result <Self::Output, Overflow>;
}

pub trait TrySubAssign <Arg = Self> {
	fn try_sub_assign (& mut self, arg: Arg) -> Result <(), Overflow>;
}

macro_rules! int_ops_impl {
	( $signed:ident , $unsigned:ident, $bits:literal ) => {

		impl TryAdd <Self> for $signed {
			type Output = Self;

			#[ inline (always) ]
			fn try_add (self, arg: Self) -> NumResult <Self> {
				self.checked_add (arg).ok_or (Overflow)
			}

		}

		impl TryAdd <Self> for $unsigned {
			type Output = Self;

			#[ inline (always) ]
			fn try_add (self, arg: Self) -> NumResult <Self> {
				self.checked_add (arg).ok_or (Overflow)
			}

		}

		impl TryAdd <$signed> for $unsigned {
			type Output = Self;

			#[ inline (always) ]
			fn try_add (self, arg: $signed) -> NumResult <Self> {
				self.add_signed (arg)
			}

		}

		impl TryAddAssign <Self> for $signed {

			#[ inline (always) ]
			fn try_add_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_add (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TryAddAssign <Self> for $unsigned {

			#[ inline (always) ]
			fn try_add_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_add (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TryDiv <Self> for $signed {
			type Output = Self;

			#[ inline (always) ]
			fn try_div (self, arg: Self) -> NumResult <Self> {
				self.checked_div (arg).ok_or (Overflow)
			}

		}

		impl TryDiv <Self> for $unsigned {
			type Output = Self;

			#[ inline (always) ]
			fn try_div (self, arg: Self) -> NumResult <Self> {
				self.checked_div (arg).ok_or (Overflow)
			}

		}

		impl TryDivAssign <Self> for $signed {

			#[ inline (always) ]
			fn try_div_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_div (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TryDivAssign <Self> for $unsigned {

			#[ inline (always) ]
			fn try_div_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_div (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TryMul <Self> for $signed {
			type Output = Self;

			#[ inline (always) ]
			fn try_mul (self, arg: Self) -> NumResult <Self> {
				self.checked_mul (arg).ok_or (Overflow)
			}

		}

		impl TryMul <Self> for $unsigned {
			type Output = Self;

			#[ inline (always) ]
			fn try_mul (self, arg: Self) -> NumResult <Self> {
				self.checked_mul (arg).ok_or (Overflow)
			}

		}

		impl TryMulAssign <Self> for $signed {

			#[ inline (always) ]
			fn try_mul_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_mul (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TryMulAssign <Self> for $unsigned {

			#[ inline (always) ]
			fn try_mul_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_mul (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TryRem <Self> for $signed {
			type Output = Self;

			#[ inline (always) ]
			fn try_rem (self, arg: Self) -> NumResult <Self> {
				self.checked_rem (arg).ok_or (Overflow)
			}

		}

		impl TryRem <Self> for $unsigned {
			type Output = Self;

			#[ inline (always) ]
			fn try_rem (self, arg: Self) -> NumResult <Self> {
				self.checked_rem (arg).ok_or (Overflow)
			}

		}

		impl TryRemAssign <Self> for $signed {

			#[ inline (always) ]
			fn try_rem_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_rem (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TryRemAssign <Self> for $unsigned {

			#[ inline (always) ]
			fn try_rem_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_rem (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TrySub <Self> for $signed {
			type Output = Self;

			#[ inline (always) ]
			fn try_sub (self, arg: Self) -> NumResult <Self> {
				self.checked_sub (arg).ok_or (Overflow)
			}

		}

		impl TrySub <Self> for $unsigned {
			type Output = Self;

			#[ inline (always) ]
			fn try_sub (self, arg: Self) -> NumResult <Self> {
				self.checked_sub (arg).ok_or (Overflow)
			}

		}

		impl TrySubAssign <Self> for $signed {

			#[ inline (always) ]
			fn try_sub_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_sub (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

		impl TrySubAssign <Self> for $unsigned {

			#[ inline (always) ]
			fn try_sub_assign (& mut self, arg: Self) -> NumResult <()> {
				* self = self.checked_sub (arg).ok_or (Overflow) ?;
				Ok (())
			}

		}

	};
}

int_ops_impl! (i8, u8, 8);
int_ops_impl! (i16, u16, 16);
int_ops_impl! (i32, u32, 32);
int_ops_impl! (i64, u64, 64);
int_ops_impl! (i128, u128, 128);
int_ops_impl! (isize, usize, 128);