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
/// Provides traits for implements a checked numeric types.
pub mod checked {
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
use std::fmt::{Debug, Display};
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use std::str::FromStr;
/// A base trait for numeric types, provides `0` and `1` values,
/// comparisons, conversions from and to string, and checked numeric operations.
///
/// This trait is implemented for all the types that implements the traits.
pub trait CheckedNum:
CheckedNumOps
+ Zero
+ One
+ PartialOrd
+ ToPrimitive
+ FromPrimitive
+ FromStr
+ Clone
+ Debug
+ Display
{
}
impl<T: Sized> CheckedNum for T where
T: CheckedNumOps
+ Zero
+ One
+ PartialOrd
+ FromPrimitive
+ ToPrimitive
+ FromStr
+ Clone
+ Debug
+ Display
{
}
/// Traits for basic checked numeric operations that returns `None` on overflow.
///
/// This trait is implemented for all the types that implements the traits.
pub trait CheckedNumOps:
CheckedAdd + CheckedSub + CheckedMul + CheckedDiv + CheckedRem + CheckedNeg
{
}
impl<T: Sized> CheckedNumOps for T where
T: CheckedAdd + CheckedSub + CheckedMul + CheckedDiv + CheckedRem + CheckedNeg
{
}
/// Performs an addiction that returns `None` if the result overflow/underflow.
pub trait CheckedAdd: Sized + Add<Output = Self> {
/// Adds two values and returns `None` if the result overflow/underflow.
fn checked_add(&self, other: &Self) -> Option<Self>;
}
/// Performs a subtraction that returns `None` if the result overflow/underflow.
pub trait CheckedSub: Sized + Sub<Output = Self> {
/// Subtracts two values and returns `None` if the result overflow/underflow.
fn checked_sub(&self, other: &Self) -> Option<Self>;
}
/// Performs a multiplication that returns `None` if the result overflow/underflow.
pub trait CheckedMul: Sized + Mul<Output = Self> {
/// Multiplies two values and returns `None` if the result overflow/underflow.
fn checked_mul(&self, other: &Self) -> Option<Self>;
}
/// Performs a division that returns `None` if the divisor is zero or the result overflow/underflow.
pub trait CheckedDiv: Sized + Div<Output = Self> {
/// Divides two values and returns `None` if the divisor is zero or the result overflow/underflow.
fn checked_div(&self, other: &Self) -> Option<Self>;
}
/// Performs an integral remainder that returns `None` if the divisor is zero or the result overflow/underflow.
pub trait CheckedRem: Sized + Rem<Output = Self> {
/// Gets the remainder two values and returns `None` if the divisor is zero or the result overflow/underflow.
fn checked_rem(&self, other: &Self) -> Option<Self>;
}
/// Performs a negation that returns `None` if the result overflow/underflow.
pub trait CheckedNeg: Sized + Neg<Output = Self> {
/// Negates a value and returns`None` if the divisor is zero or the result overflow/underflow.
fn checked_neg(&self) -> Option<Self>;
}
//////////////////////// Macros ////////////////////////
#[macro_export]
macro_rules! unsafe_impl_checked_ops {
($($type:ty), *) => ($(
impl CheckedAdd for $type{
#[inline]
fn checked_add(&self, other: &Self) -> Option<Self>{
Some(self + other)
}
}
impl CheckedSub for $type{
#[inline]
fn checked_sub(&self, other: &Self) -> Option<Self>{
Some(self - other)
}
}
impl CheckedMul for $type{
#[inline]
fn checked_mul(&self, other: &Self) -> Option<Self>{
Some(self * other)
}
}
impl CheckedDiv for $type{
#[inline]
fn checked_div(&self, other: &Self) -> Option<Self>{
if num_traits::Zero::is_zero(other){
return None;
}
Some(self / other)
}
}
impl CheckedRem for $type{
#[inline]
fn checked_rem(&self, other: &Self) -> Option<Self>{
if num_traits::Zero::is_zero(other){
return None;
}
Some(self % other)
}
}
impl CheckedNeg for $type{
#[inline]
fn checked_neg(&self) -> Option<Self>{
Some(-*self)
}
}
)*)
}
#[macro_export]
macro_rules! impl_checked_ops {
($($type:ty), *) => ($(
impl CheckedAdd for $type{
#[inline]
fn checked_add(&self, other: &Self) -> Option<Self>{
<$type>::checked_add(*self, *other)
}
}
impl CheckedSub for $type{
#[inline]
fn checked_sub(&self, other: &Self) -> Option<Self>{
<$type>::checked_sub(*self, *other)
}
}
impl CheckedMul for $type{
#[inline]
fn checked_mul(&self, other: &Self) -> Option<Self>{
<$type>::checked_mul(*self, *other)
}
}
impl CheckedDiv for $type{
#[inline]
fn checked_div(&self, other: &Self) -> Option<Self>{
<$type>::checked_div(*self, *other)
}
}
impl CheckedRem for $type{
#[inline]
fn checked_rem(&self, other: &Self) -> Option<Self>{
<$type>::checked_rem(*self, *other)
}
}
impl CheckedNeg for $type{
#[inline]
fn checked_neg(&self) -> Option<Self>{
<$type>::checked_neg(*self)
}
}
)*)
}
#[macro_export]
macro_rules! impl_checked_binary {
($($type:ty), *) => ($(
impl CheckedAdd for $type{
#[inline]
fn checked_add(&self, other: &Self) -> Option<Self>{
<$type>::checked_add(*self, *other)
}
}
impl CheckedSub for $type{
#[inline]
fn checked_sub(&self, other: &Self) -> Option<Self>{
<$type>::checked_sub(*self, *other)
}
}
impl CheckedMul for $type{
#[inline]
fn checked_mul(&self, other: &Self) -> Option<Self>{
<$type>::checked_mul(*self, *other)
}
}
impl CheckedDiv for $type{
#[inline]
fn checked_div(&self, other: &Self) -> Option<Self>{
<$type>::checked_div(*self, *other)
}
}
impl CheckedRem for $type{
#[inline]
fn checked_rem(&self, other: &Self) -> Option<Self>{
<$type>::checked_rem(*self, *other)
}
}
)*)
}
#[macro_export]
macro_rules! impl_checked_unary{
($($type:ty), *) => ($(
impl CheckedNeg for $type{
#[inline]
fn checked_neg(&self) -> Option<Self>{
<$type>::checked_neg(*self)
}
}
)*)
}
// Implementing all the Checked operations
impl_checked_ops!(i8, i16, i32, i64, i128, isize);
// Implementing only all the Checked binary operations
impl_checked_binary!(u8, u16, u32, u64, u128, usize);
// Implementing all the Checked operations by forwarding to the corresponding std::ops,
// necessary to have compatibility with f32 y f64.
unsafe_impl_checked_ops!(f32, f64);
}
/// Provides traits for implements a unchecked numeric types.
pub mod unchecked {
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
use std::fmt::{Debug, Display};
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use std::str::FromStr;
/// Traits for basic unchecked numeric operations.
///
/// This trait is implemented for all the types that implements the traits.
pub trait UncheckedNum:
UncheckedNumOps
+ Zero
+ One
+ PartialOrd
+ FromStr
+ ToPrimitive
+ FromPrimitive
+ Clone
+ Display
+ Debug
{
}
impl<T> UncheckedNum for T where
T: UncheckedNumOps
+ Zero
+ One
+ PartialOrd
+ FromStr
+ ToPrimitive
+ FromPrimitive
+ Clone
+ Display
+ Debug
{
}
/// Traits for basic unchecked numeric operations.
///
/// This trait is implemented for all the types that implements the traits.
pub trait UncheckedNumOps:
Sized
+ Add<Self, Output = Self>
+ Sub<Self, Output = Self>
+ Mul<Self, Output = Self>
+ Div<Self, Output = Self>
+ Rem<Self, Output = Self>
+ Neg<Output = Self>
{
}
impl<T> UncheckedNumOps for T where
T: Add<Self, Output = Self>
+ Sub<Self, Output = Self>
+ Mul<Self, Output = Self>
+ Div<Self, Output = Self>
+ Rem<Self, Output = Self>
+ Neg<Output = Self>
{
}
}