@@ -26,6 +26,7 @@ macro_rules! nonzero_integer {
26
26
(
27
27
Self = $Ty: ident,
28
28
Primitive = $signedness: ident $Int: ident,
29
+ $( UnsignedNonZero = $UnsignedNonZero: ident, ) ?
29
30
UnsignedPrimitive = $UnsignedPrimitive: ty,
30
31
feature = $feature: literal,
31
32
original_stabilization = $since: literal,
@@ -173,6 +174,13 @@ macro_rules! nonzero_integer {
173
174
// SAFETY: since `self` cannot be zero, it is safe to call `cttz_nonzero`.
174
175
unsafe { intrinsics:: cttz_nonzero( self . get( ) as $UnsignedPrimitive) as u32 }
175
176
}
177
+
178
+ nonzero_integer_signedness_dependent_methods! {
179
+ Self = $Ty,
180
+ Primitive = $signedness $Int,
181
+ $( UnsignedNonZero = $UnsignedNonZero, ) ?
182
+ UnsignedPrimitive = $UnsignedPrimitive,
183
+ }
176
184
}
177
185
178
186
#[ stable( feature = "from_nonzero" , since = "1.31.0" ) ]
@@ -304,11 +312,13 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
304
312
} ;
305
313
}
306
314
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
+ ) => {
312
322
/// Adds an unsigned integer to a non-zero value.
313
323
/// Checks for overflow and returns [`None`] on overflow.
314
324
/// As a consequence, the result cannot wrap to zero.
@@ -538,25 +548,15 @@ macro_rules! nonzero_unsigned_operations {
538
548
// never being 0.
539
549
unsafe { $Ty:: new_unchecked( self . get( ) . midpoint( rhs. get( ) ) ) }
540
550
}
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
+ } ;
554
552
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
+ ) => {
560
560
/// Computes the absolute value of self.
561
561
#[ doc = concat!( "See [`" , stringify!( $Int) , "::abs`]" ) ]
562
562
/// for documentation on overflow behaviour.
@@ -934,18 +934,7 @@ macro_rules! nonzero_signed_operations {
934
934
// SAFETY: negation of nonzero cannot yield zero values.
935
935
unsafe { $Ty:: new_unchecked( result) }
936
936
}
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
+ } ;
949
938
}
950
939
951
940
// A bunch of methods for both signed and unsigned nonzero types.
@@ -1410,6 +1399,7 @@ nonzero_integer! {
1410
1399
nonzero_integer ! {
1411
1400
Self = NonZeroI8 ,
1412
1401
Primitive = signed i8 ,
1402
+ UnsignedNonZero = NonZeroU8 ,
1413
1403
UnsignedPrimitive = u8 ,
1414
1404
feature = "signed_nonzero" ,
1415
1405
original_stabilization = "1.34.0" ,
@@ -1419,6 +1409,7 @@ nonzero_integer! {
1419
1409
nonzero_integer ! {
1420
1410
Self = NonZeroI16 ,
1421
1411
Primitive = signed i16 ,
1412
+ UnsignedNonZero = NonZeroU16 ,
1422
1413
UnsignedPrimitive = u16 ,
1423
1414
feature = "signed_nonzero" ,
1424
1415
original_stabilization = "1.34.0" ,
@@ -1428,6 +1419,7 @@ nonzero_integer! {
1428
1419
nonzero_integer ! {
1429
1420
Self = NonZeroI32 ,
1430
1421
Primitive = signed i32 ,
1422
+ UnsignedNonZero = NonZeroU32 ,
1431
1423
UnsignedPrimitive = u32 ,
1432
1424
feature = "signed_nonzero" ,
1433
1425
original_stabilization = "1.34.0" ,
@@ -1437,6 +1429,7 @@ nonzero_integer! {
1437
1429
nonzero_integer ! {
1438
1430
Self = NonZeroI64 ,
1439
1431
Primitive = signed i64 ,
1432
+ UnsignedNonZero = NonZeroU64 ,
1440
1433
UnsignedPrimitive = u64 ,
1441
1434
feature = "signed_nonzero" ,
1442
1435
original_stabilization = "1.34.0" ,
@@ -1446,6 +1439,7 @@ nonzero_integer! {
1446
1439
nonzero_integer ! {
1447
1440
Self = NonZeroI128 ,
1448
1441
Primitive = signed i128 ,
1442
+ UnsignedNonZero = NonZeroU128 ,
1449
1443
UnsignedPrimitive = u128 ,
1450
1444
feature = "signed_nonzero" ,
1451
1445
original_stabilization = "1.34.0" ,
@@ -1455,6 +1449,7 @@ nonzero_integer! {
1455
1449
nonzero_integer ! {
1456
1450
Self = NonZeroIsize ,
1457
1451
Primitive = signed isize ,
1452
+ UnsignedNonZero = NonZeroUsize ,
1458
1453
UnsignedPrimitive = usize ,
1459
1454
feature = "signed_nonzero" ,
1460
1455
original_stabilization = "1.34.0" ,
0 commit comments