@@ -360,8 +360,7 @@ macro_rules! nonzero_unsigned_operations {
360
360
/// Overflow is unchecked, and it is undefined behaviour to overflow
361
361
/// *even if the result would wrap to a non-zero value*.
362
362
/// The behaviour is undefined as soon as
363
- #[ doc = concat!( "`self + rhs > " , stringify!( $Int) , "::MAX`" ) ]
364
- #[ doc = concat!( " or `self + rhs < " , stringify!( $Int) , "::MIN`." ) ]
363
+ #[ doc = concat!( "`self + rhs > " , stringify!( $Int) , "::MAX`." ) ]
365
364
///
366
365
/// # Examples
367
366
///
@@ -650,7 +649,7 @@ nonzero_signed_operations! {
650
649
651
650
// A bunch of methods for both signed and unsigned nonzero types.
652
651
macro_rules! nonzero_unsigned_signed_operations {
653
- ( $( $Ty: ident( $Int: ty) ; ) + ) => {
652
+ ( $( $signedness : ident $ Ty: ident( $Int: ty) ; ) + ) => {
654
653
$(
655
654
impl $Ty {
656
655
/// Multiply two non-zero integers together.
@@ -723,8 +722,16 @@ macro_rules! nonzero_unsigned_signed_operations {
723
722
/// Overflow is unchecked, and it is undefined behaviour to overflow
724
723
/// *even if the result would wrap to a non-zero value*.
725
724
/// The behaviour is undefined as soon as
726
- #[ doc = concat!( "`self * rhs > " , stringify!( $Int) , "::MAX`, " ) ]
727
- #[ doc = concat!( "or `self * rhs < " , stringify!( $Int) , "::MIN`." ) ]
725
+ #[ doc = sign_dependent_expr!{
726
+ $signedness ?
727
+ if signed {
728
+ concat!( "`self * rhs > " , stringify!( $Int) , "::MAX`, " ,
729
+ "or `self * rhs < " , stringify!( $Int) , "::MIN`." )
730
+ }
731
+ if unsigned {
732
+ concat!( "`self * rhs > " , stringify!( $Int) , "::MAX`." )
733
+ }
734
+ } ]
728
735
///
729
736
/// # Examples
730
737
///
@@ -783,7 +790,16 @@ macro_rules! nonzero_unsigned_signed_operations {
783
790
}
784
791
785
792
/// Raise non-zero value to an integer power.
786
- #[ doc = concat!( "Return [`" , stringify!( $Int) , "::MAX`] on overflow." ) ]
793
+ #[ doc = sign_dependent_expr!{
794
+ $signedness ?
795
+ if signed {
796
+ concat!( "Return [`" , stringify!( $Int) , "::MIN`] " ,
797
+ "or [`" , stringify!( $Int) , "::MAX`] on overflow." )
798
+ }
799
+ if unsigned {
800
+ concat!( "Return [`" , stringify!( $Int) , "::MAX`] on overflow." )
801
+ }
802
+ } ]
787
803
///
788
804
/// # Examples
789
805
///
@@ -815,19 +831,29 @@ macro_rules! nonzero_unsigned_signed_operations {
815
831
}
816
832
}
817
833
834
+ // Use this when the generated code should differ between signed and unsigned types.
835
+ macro_rules! sign_dependent_expr {
836
+ ( signed ? if signed { $signed_case: expr } if unsigned { $unsigned_case: expr } ) => {
837
+ $signed_case
838
+ } ;
839
+ ( unsigned ? if signed { $signed_case: expr } if unsigned { $unsigned_case: expr } ) => {
840
+ $unsigned_case
841
+ } ;
842
+ }
843
+
818
844
nonzero_unsigned_signed_operations ! {
819
- NonZeroU8 ( u8 ) ;
820
- NonZeroU16 ( u16 ) ;
821
- NonZeroU32 ( u32 ) ;
822
- NonZeroU64 ( u64 ) ;
823
- NonZeroU128 ( u128 ) ;
824
- NonZeroUsize ( usize ) ;
825
- NonZeroI8 ( i8 ) ;
826
- NonZeroI16 ( i16 ) ;
827
- NonZeroI32 ( i32 ) ;
828
- NonZeroI64 ( i64 ) ;
829
- NonZeroI128 ( i128 ) ;
830
- NonZeroIsize ( isize ) ;
845
+ unsigned NonZeroU8 ( u8 ) ;
846
+ unsigned NonZeroU16 ( u16 ) ;
847
+ unsigned NonZeroU32 ( u32 ) ;
848
+ unsigned NonZeroU64 ( u64 ) ;
849
+ unsigned NonZeroU128 ( u128 ) ;
850
+ unsigned NonZeroUsize ( usize ) ;
851
+ signed NonZeroI8 ( i8 ) ;
852
+ signed NonZeroI16 ( i16 ) ;
853
+ signed NonZeroI32 ( i32 ) ;
854
+ signed NonZeroI64 ( i64 ) ;
855
+ signed NonZeroI128 ( i128 ) ;
856
+ signed NonZeroIsize ( isize ) ;
831
857
}
832
858
833
859
macro_rules! nonzero_unsigned_is_power_of_two {
0 commit comments