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
use crate::conversion::Cast;
use std::any::type_name;
macro_rules! impl_overflow_arithmetic {
($target: ty) => {
#[allow(clippy::use_self)]
impl OverflowArithmetic for $target {
#[inline]
fn overflow_add(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_add(other);
assert!(
!overflow,
"number = {}({}) add number = {}({}) overflowed",
self,
type_name::<Self>(),
other,
type_name::<Self>()
);
res
}
#[inline]
fn overflow_sub(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_sub(other);
assert!(
!overflow,
"number = {}({}) substract number = {}({}) overflowed",
self,
type_name::<Self>(),
other,
type_name::<Self>()
);
res
}
#[inline]
fn overflow_mul(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_mul(other);
assert!(
!overflow,
"number = {}({}) multiply number = {}({}) overflowed",
self,
type_name::<Self>(),
other,
type_name::<Self>()
);
res
}
#[inline]
fn overflow_div(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_div(other);
assert!(
!overflow,
"number = {}({}) divide number = {}({}) overflowed",
self,
type_name::<Self>(),
other,
type_name::<Self>()
);
res
}
#[inline]
fn overflow_shl(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_shl(other.cast());
assert!(
!overflow,
"number = {}({}) left shift number = {}({}) overflowed",
self,
type_name::<Self>(),
other,
type_name::<Self>()
);
res
}
#[inline]
fn overflow_shr(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_shr(other.cast());
assert!(
!overflow,
"number = {}({}) right shift number = {}({}) overflowed",
self,
type_name::<Self>(),
other,
type_name::<Self>()
);
res
}
#[inline]
fn overflow_neg(self) -> Self {
let (res, overflow) = self.overflowing_neg();
assert!(
!overflow,
"number = {}({}) negate overflowed",
self,
type_name::<Self>(),
);
res
}
#[inline]
fn overflow_rem(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_rem(other.cast());
assert!(
!overflow,
"number = {}({}) remainder number = {}({}) overflowed",
self,
type_name::<Self>(),
other,
type_name::<Self>()
);
res
}
}
};
}
impl_overflow_arithmetic!(u8);
impl_overflow_arithmetic!(u16);
impl_overflow_arithmetic!(u32);
impl_overflow_arithmetic!(u64);
impl_overflow_arithmetic!(u128);
impl_overflow_arithmetic!(i8);
impl_overflow_arithmetic!(i16);
impl_overflow_arithmetic!(i32);
impl_overflow_arithmetic!(i64);
impl_overflow_arithmetic!(i128);
impl_overflow_arithmetic!(usize);
impl_overflow_arithmetic!(isize);
pub trait OverflowArithmetic {
fn overflow_add(self, other: Self) -> Self;
fn overflow_sub(self, other: Self) -> Self;
fn overflow_mul(self, other: Self) -> Self;
fn overflow_div(self, other: Self) -> Self;
fn overflow_shl(self, other: Self) -> Self;
fn overflow_shr(self, other: Self) -> Self;
fn overflow_neg(self) -> Self;
fn overflow_rem(self, other: Self) -> Self;
}