Skip to content

Commit ae56454

Browse files
committed
Move Unsigned & Signed associated types into Integer trait
1 parent 4f6b17d commit ae56454

File tree

4 files changed

+86
-64
lines changed

4 files changed

+86
-64
lines changed

crates/utils/src/number.rs

Lines changed: 82 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ pub trait Integer:
6565
+ ShrAssign<u32>
6666
+ TryInto<i128>
6767
{
68+
type Unsigned: UnsignedInteger;
69+
type Signed: SignedInteger;
70+
71+
#[must_use]
72+
fn abs_diff(self, rhs: Self) -> Self::Unsigned;
6873
#[must_use]
6974
fn checked_add(self, rhs: Self) -> Option<Self>;
7075
#[must_use]
@@ -75,27 +80,22 @@ pub trait Integer:
7580
fn trailing_ones(self) -> u32;
7681
#[must_use]
7782
fn trailing_zeros(self) -> u32;
83+
#[must_use]
84+
fn unsigned_abs(self) -> Self::Unsigned;
7885
}
7986

8087
/// Trait implemented by the primitive unsigned integer types.
81-
pub trait UnsignedInteger: Integer + From<u8> {
82-
type Signed: SignedInteger;
83-
88+
pub trait UnsignedInteger: Integer<Unsigned = Self> + From<u8> {
8489
#[must_use]
8590
fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
8691
}
8792

8893
/// Trait implemented by the primitive signed integer types.
89-
pub trait SignedInteger: Integer + Signed {
90-
type Unsigned: UnsignedInteger;
91-
92-
#[must_use]
93-
fn unsigned_abs(self) -> Self::Unsigned;
94-
}
94+
pub trait SignedInteger: Integer<Signed = Self> + Signed {}
9595

9696
macro_rules! number_impl {
97-
(uint => $($t:ty: $signed:ty),+) => {
98-
$(impl Number for $t {
97+
(int => $($u:ty: $s:ty ),+) => {
98+
$(impl Number for $u {
9999
const ZERO: Self = 0;
100100
const ONE: Self = 1;
101101
const MIN: Self = Self::MIN;
@@ -112,19 +112,48 @@ macro_rules! number_impl {
112112
}
113113
})+
114114

115-
number_impl! {@common integer => $($t),+}
115+
$(impl Integer for $u {
116+
type Unsigned = $u;
117+
type Signed = $s;
116118

117-
$(impl UnsignedInteger for $t {
118-
type Signed = $signed;
119+
#[inline]
120+
fn abs_diff(self, rhs: Self) -> Self::Unsigned {
121+
self.abs_diff(rhs)
122+
}
123+
#[inline]
124+
fn checked_add(self, rhs: Self) -> Option<Self> {
125+
self.checked_add(rhs)
126+
}
127+
#[inline]
128+
fn checked_sub(self, rhs: Self) -> Option<Self> {
129+
self.checked_sub(rhs)
130+
}
131+
#[inline]
132+
fn checked_mul(self, rhs: Self) -> Option<Self> {
133+
self.checked_mul(rhs)
134+
}
135+
#[inline]
136+
fn trailing_ones(self) -> u32 {
137+
self.trailing_ones()
138+
}
139+
#[inline]
140+
fn trailing_zeros(self) -> u32 {
141+
self.trailing_zeros()
142+
}
143+
#[inline]
144+
fn unsigned_abs(self) -> Self::Unsigned {
145+
self // no-op for unsigned integers
146+
}
147+
})+
119148

149+
$(impl UnsignedInteger for $u {
120150
#[inline]
121151
fn wrapping_add_signed(self, rhs: Self::Signed) -> Self {
122152
self.wrapping_add_signed(rhs)
123153
}
124154
})+
125-
};
126-
(int => $($t:ty: $unsigned:ty ),+) => {
127-
$(impl Number for $t {
155+
156+
$(impl Number for $s {
128157
const ZERO: Self = 0;
129158
const ONE: Self = 1;
130159
const MIN: Self = Self::MIN;
@@ -141,45 +170,18 @@ macro_rules! number_impl {
141170
}
142171
})+
143172

144-
number_impl! {@common integer => $($t),+}
145-
number_impl! {@common signed => $($t),+}
146-
147-
$(impl SignedInteger for $t {
148-
type Unsigned = $unsigned;
149-
150-
#[inline]
151-
fn unsigned_abs(self) -> Self::Unsigned {
152-
self.unsigned_abs()
153-
}
173+
$(impl Signed for $s {
174+
const MINUS_ONE: Self = -Self::ONE;
154175
})+
155-
};
156-
(float => $($t:ty),+) => {
157-
$(impl Number for $t {
158-
const ZERO: Self = 0.0;
159-
const ONE: Self = 1.0;
160-
const MIN: Self = Self::NEG_INFINITY;
161-
const MAX: Self = Self::INFINITY;
162176

163-
#[inline]
164-
fn abs(self) -> Self {
165-
self.abs()
166-
}
177+
$(impl Integer for $s {
178+
type Unsigned = $u;
179+
type Signed = $s;
167180

168181
#[inline]
169-
fn rem_euclid(self, rhs: Self) -> Self {
170-
self.rem_euclid(rhs)
182+
fn abs_diff(self, rhs: Self) -> Self::Unsigned {
183+
self.abs_diff(rhs)
171184
}
172-
})+
173-
174-
number_impl! {@common signed => $($t),+}
175-
};
176-
(@common signed => $($t:ty),+) => {
177-
$(impl Signed for $t {
178-
const MINUS_ONE: Self = -Self::ONE;
179-
})+
180-
};
181-
(@common integer => $($t:ty),+) => {
182-
$(impl Integer for $t {
183185
#[inline]
184186
fn checked_add(self, rhs: Self) -> Option<Self> {
185187
self.checked_add(rhs)
@@ -200,11 +202,38 @@ macro_rules! number_impl {
200202
fn trailing_zeros(self) -> u32 {
201203
self.trailing_zeros()
202204
}
205+
#[inline]
206+
fn unsigned_abs(self) -> Self::Unsigned {
207+
self.unsigned_abs()
208+
}
203209
})+
210+
211+
$(impl SignedInteger for $s {})+
204212
};
213+
(float => $($t:ty),+) => {$(
214+
impl Number for $t {
215+
const ZERO: Self = 0.0;
216+
const ONE: Self = 1.0;
217+
const MIN: Self = Self::NEG_INFINITY;
218+
const MAX: Self = Self::INFINITY;
219+
220+
#[inline]
221+
fn abs(self) -> Self {
222+
self.abs()
223+
}
224+
225+
#[inline]
226+
fn rem_euclid(self, rhs: Self) -> Self {
227+
self.rem_euclid(rhs)
228+
}
229+
}
230+
231+
impl Signed for $t {
232+
const MINUS_ONE: Self = -Self::ONE;
233+
}
234+
)+};
205235
}
206-
number_impl! {uint => u8: i8, u16: i16, u32: i32, u64: i64, u128: i128, usize: isize}
207-
number_impl! {int => i8: u8, i16: u16, i32: u32, i64: u64, i128: u128, isize: usize}
236+
number_impl! {int => u8: i8, u16: i16, u32: i32, u64: i64, u128: i128, usize: isize}
208237
number_impl! {float => f32, f64}
209238

210239
/// Checks if the provided unsigned integer `n` is a prime number.

crates/utils/src/point.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ macro_rules! point_impl {
2323
/// Returns the manhattan distance from the origin.
2424
#[inline]
2525
#[must_use]
26-
pub fn manhattan_distance(self) -> T {
27-
T::ZERO $(+ self.$f.abs())+
28-
}
29-
30-
/// Returns the manhattan distance from the origin.
31-
#[inline]
32-
#[must_use]
33-
pub fn manhattan_distance_unsigned(self) -> T::Unsigned
26+
pub fn manhattan_distance(self) -> T::Unsigned
3427
where
3528
T: SignedInteger
3629
{

crates/year2016/src/day01.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Day01 {
3737
pos += dir * i32::from(steps);
3838
}
3939

40-
pos.manhattan_distance_unsigned()
40+
pos.manhattan_distance()
4141
}
4242

4343
#[must_use]
@@ -54,7 +54,7 @@ impl Day01 {
5454
for _ in 0..steps {
5555
pos += dir;
5656
if !visited.insert(pos) {
57-
return pos.manhattan_distance_unsigned();
57+
return pos.manhattan_distance();
5858
}
5959
}
6060
}

crates/year2017/src/day20.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Day20 {
4040
self.particles
4141
.iter()
4242
.enumerate()
43-
.min_by_key(|&(_, p)| p.position_at_time(1_000_000).manhattan_distance_unsigned())
43+
.min_by_key(|&(_, p)| p.position_at_time(1_000_000).manhattan_distance())
4444
.unwrap()
4545
.0
4646
}

0 commit comments

Comments
 (0)