@@ -360,8 +360,7 @@ macro_rules! nonzero_unsigned_operations {
360360 /// Overflow is unchecked, and it is undefined behaviour to overflow
361361 /// *even if the result would wrap to a non-zero value*.
362362 /// 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`." ) ]
365364 ///
366365 /// # Examples
367366 ///
@@ -650,7 +649,7 @@ nonzero_signed_operations! {
650649
651650// A bunch of methods for both signed and unsigned nonzero types.
652651macro_rules! nonzero_unsigned_signed_operations {
653- ( $( $Ty: ident( $Int: ty) ; ) + ) => {
652+ ( $( $signedness : ident $ Ty: ident( $Int: ty) ; ) + ) => {
654653 $(
655654 impl $Ty {
656655 /// Multiply two non-zero integers together.
@@ -723,8 +722,16 @@ macro_rules! nonzero_unsigned_signed_operations {
723722 /// Overflow is unchecked, and it is undefined behaviour to overflow
724723 /// *even if the result would wrap to a non-zero value*.
725724 /// 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+ } ]
728735 ///
729736 /// # Examples
730737 ///
@@ -783,7 +790,16 @@ macro_rules! nonzero_unsigned_signed_operations {
783790 }
784791
785792 /// 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+ } ]
787803 ///
788804 /// # Examples
789805 ///
@@ -815,19 +831,29 @@ macro_rules! nonzero_unsigned_signed_operations {
815831 }
816832}
817833
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+
818844nonzero_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 ) ;
831857}
832858
833859macro_rules! nonzero_unsigned_is_power_of_two {
0 commit comments