Skip to content

Commit 0cd4762

Browse files
committed
Remove duplication by adding a macro
1 parent 3efae7f commit 0cd4762

File tree

1 file changed

+26
-63
lines changed

1 file changed

+26
-63
lines changed

src/int/mod.rs

+26-63
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,15 @@ fn unwrap<T>(t: Option<T>) -> T {
8484
}
8585
}
8686

87-
macro_rules! int_impl {
88-
($ity:ty, $uty:ty, $bits:expr) => {
89-
impl Int for $uty {
90-
type OtherSign = $ity;
91-
type UnsignedInt = $uty;
92-
87+
macro_rules! int_impl_common {
88+
($ty:ty, $bits:expr) => {
9389
const BITS: u32 = $bits;
9490

9591
const ZERO: Self = 0;
9692
const ONE: Self = 1;
9793

98-
fn extract_sign(self) -> (bool, $uty) {
99-
(false, self)
100-
}
101-
102-
fn unsigned(self) -> $uty {
103-
self
104-
}
105-
106-
fn from_unsigned(me: $uty) -> Self {
107-
me
108-
}
109-
11094
fn from_bool(b: bool) -> Self {
111-
b as $uty
95+
b as $ty
11296
}
11397

11498
fn max_value() -> Self {
@@ -146,17 +130,34 @@ macro_rules! int_impl {
146130
fn leading_zeros(self) -> u32 {
147131
<Self>::leading_zeros(self)
148132
}
133+
}
134+
}
135+
136+
macro_rules! int_impl {
137+
($ity:ty, $uty:ty, $bits:expr) => {
138+
impl Int for $uty {
139+
type OtherSign = $ity;
140+
type UnsignedInt = $uty;
141+
142+
fn extract_sign(self) -> (bool, $uty) {
143+
(false, self)
144+
}
145+
146+
fn unsigned(self) -> $uty {
147+
self
148+
}
149+
150+
fn from_unsigned(me: $uty) -> Self {
151+
me
152+
}
153+
154+
int_impl_common!($uty, $bits);
149155
}
150156

151157
impl Int for $ity {
152158
type OtherSign = $uty;
153159
type UnsignedInt = $uty;
154160

155-
const BITS: u32 = $bits;
156-
157-
const ZERO: Self = 0;
158-
const ONE: Self = 1;
159-
160161
fn extract_sign(self) -> (bool, $uty) {
161162
if self < 0 {
162163
(true, (!(self as $uty)).wrapping_add(1))
@@ -173,45 +174,7 @@ macro_rules! int_impl {
173174
me as $ity
174175
}
175176

176-
fn from_bool(b: bool) -> Self {
177-
b as $ity
178-
}
179-
180-
fn max_value() -> Self {
181-
<Self>::max_value()
182-
}
183-
184-
fn min_value() -> Self {
185-
<Self>::min_value()
186-
}
187-
188-
fn wrapping_add(self, other: Self) -> Self {
189-
<Self>::wrapping_add(self, other)
190-
}
191-
192-
fn wrapping_mul(self, other: Self) -> Self {
193-
<Self>::wrapping_mul(self, other)
194-
}
195-
196-
fn wrapping_sub(self, other: Self) -> Self {
197-
<Self>::wrapping_sub(self, other)
198-
}
199-
200-
fn wrapping_shl(self, other: u32) -> Self {
201-
<Self>::wrapping_shl(self, other)
202-
}
203-
204-
fn aborting_div(self, other: Self) -> Self {
205-
unwrap(<Self>::checked_div(self, other))
206-
}
207-
208-
fn aborting_rem(self, other: Self) -> Self {
209-
unwrap(<Self>::checked_rem(self, other))
210-
}
211-
212-
fn leading_zeros(self) -> u32 {
213-
<Self>::leading_zeros(self)
214-
}
177+
int_impl_common!($ity, $bits);
215178
}
216179
}
217180
}

0 commit comments

Comments
 (0)