Skip to content

Commit 4291b3f

Browse files
committed
Move signedness dependent methods into the omnibus impl block
1 parent 757ed25 commit 4291b3f

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

library/core/src/num/nonzero.rs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ macro_rules! nonzero_integer {
2626
(
2727
Self = $Ty:ident,
2828
Primitive = $signedness:ident $Int:ident,
29+
$(UnsignedNonZero = $UnsignedNonZero:ident,)?
2930
UnsignedPrimitive = $UnsignedPrimitive:ty,
3031
feature = $feature:literal,
3132
original_stabilization = $since:literal,
@@ -173,6 +174,13 @@ macro_rules! nonzero_integer {
173174
// SAFETY: since `self` cannot be zero, it is safe to call `cttz_nonzero`.
174175
unsafe { intrinsics::cttz_nonzero(self.get() as $UnsignedPrimitive) as u32 }
175176
}
177+
178+
nonzero_integer_signedness_dependent_methods! {
179+
Self = $Ty,
180+
Primitive = $signedness $Int,
181+
$(UnsignedNonZero = $UnsignedNonZero,)?
182+
UnsignedPrimitive = $UnsignedPrimitive,
183+
}
176184
}
177185

178186
#[stable(feature = "from_nonzero", since = "1.31.0")]
@@ -304,11 +312,13 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
304312
};
305313
}
306314

307-
// A bunch of methods for unsigned nonzero types only.
308-
macro_rules! nonzero_unsigned_operations {
309-
( $( $Ty: ident($Int: ident); )+ ) => {
310-
$(
311-
impl $Ty {
315+
macro_rules! nonzero_integer_signedness_dependent_methods {
316+
// Methods for unsigned nonzero types only.
317+
(
318+
Self = $Ty:ident,
319+
Primitive = unsigned $Int:ident,
320+
UnsignedPrimitive = $Uint:ty,
321+
) => {
312322
/// Adds an unsigned integer to a non-zero value.
313323
/// Checks for overflow and returns [`None`] on overflow.
314324
/// As a consequence, the result cannot wrap to zero.
@@ -538,25 +548,15 @@ macro_rules! nonzero_unsigned_operations {
538548
// never being 0.
539549
unsafe { $Ty::new_unchecked(self.get().midpoint(rhs.get())) }
540550
}
541-
}
542-
)+
543-
}
544-
}
545-
546-
nonzero_unsigned_operations! {
547-
NonZeroU8(u8);
548-
NonZeroU16(u16);
549-
NonZeroU32(u32);
550-
NonZeroU64(u64);
551-
NonZeroU128(u128);
552-
NonZeroUsize(usize);
553-
}
551+
};
554552

555-
// A bunch of methods for signed nonzero types only.
556-
macro_rules! nonzero_signed_operations {
557-
( $( $Ty: ident($Int: ty) -> $Uty: ident($Uint: ty); )+ ) => {
558-
$(
559-
impl $Ty {
553+
// Methods for signed nonzero types only.
554+
(
555+
Self = $Ty:ident,
556+
Primitive = signed $Int:ident,
557+
UnsignedNonZero = $Uty:ident,
558+
UnsignedPrimitive = $Uint:ty,
559+
) => {
560560
/// Computes the absolute value of self.
561561
#[doc = concat!("See [`", stringify!($Int), "::abs`]")]
562562
/// for documentation on overflow behaviour.
@@ -934,18 +934,7 @@ macro_rules! nonzero_signed_operations {
934934
// SAFETY: negation of nonzero cannot yield zero values.
935935
unsafe { $Ty::new_unchecked(result) }
936936
}
937-
}
938-
)+
939-
}
940-
}
941-
942-
nonzero_signed_operations! {
943-
NonZeroI8(i8) -> NonZeroU8(u8);
944-
NonZeroI16(i16) -> NonZeroU16(u16);
945-
NonZeroI32(i32) -> NonZeroU32(u32);
946-
NonZeroI64(i64) -> NonZeroU64(u64);
947-
NonZeroI128(i128) -> NonZeroU128(u128);
948-
NonZeroIsize(isize) -> NonZeroUsize(usize);
937+
};
949938
}
950939

951940
// A bunch of methods for both signed and unsigned nonzero types.
@@ -1410,6 +1399,7 @@ nonzero_integer! {
14101399
nonzero_integer! {
14111400
Self = NonZeroI8,
14121401
Primitive = signed i8,
1402+
UnsignedNonZero = NonZeroU8,
14131403
UnsignedPrimitive = u8,
14141404
feature = "signed_nonzero",
14151405
original_stabilization = "1.34.0",
@@ -1419,6 +1409,7 @@ nonzero_integer! {
14191409
nonzero_integer! {
14201410
Self = NonZeroI16,
14211411
Primitive = signed i16,
1412+
UnsignedNonZero = NonZeroU16,
14221413
UnsignedPrimitive = u16,
14231414
feature = "signed_nonzero",
14241415
original_stabilization = "1.34.0",
@@ -1428,6 +1419,7 @@ nonzero_integer! {
14281419
nonzero_integer! {
14291420
Self = NonZeroI32,
14301421
Primitive = signed i32,
1422+
UnsignedNonZero = NonZeroU32,
14311423
UnsignedPrimitive = u32,
14321424
feature = "signed_nonzero",
14331425
original_stabilization = "1.34.0",
@@ -1437,6 +1429,7 @@ nonzero_integer! {
14371429
nonzero_integer! {
14381430
Self = NonZeroI64,
14391431
Primitive = signed i64,
1432+
UnsignedNonZero = NonZeroU64,
14401433
UnsignedPrimitive = u64,
14411434
feature = "signed_nonzero",
14421435
original_stabilization = "1.34.0",
@@ -1446,6 +1439,7 @@ nonzero_integer! {
14461439
nonzero_integer! {
14471440
Self = NonZeroI128,
14481441
Primitive = signed i128,
1442+
UnsignedNonZero = NonZeroU128,
14491443
UnsignedPrimitive = u128,
14501444
feature = "signed_nonzero",
14511445
original_stabilization = "1.34.0",
@@ -1455,6 +1449,7 @@ nonzero_integer! {
14551449
nonzero_integer! {
14561450
Self = NonZeroIsize,
14571451
Primitive = signed isize,
1452+
UnsignedNonZero = NonZeroUsize,
14581453
UnsignedPrimitive = usize,
14591454
feature = "signed_nonzero",
14601455
original_stabilization = "1.34.0",

0 commit comments

Comments
 (0)