@@ -353,8 +353,13 @@ macro_rules! nonzero_unsigned_operations {
353353 #[ inline]
354354 pub const fn checked_add( self , other: $Int) -> Option <$Ty> {
355355 if let Some ( result) = self . get( ) . checked_add( other) {
356- // SAFETY: $Int::checked_add returns None on overflow
357- // so the result cannot be zero.
356+ // SAFETY:
357+ // - `checked_add` returns `None` on overflow
358+ // - `self` and `other` are non-zero
359+ // - the only way to get zero from an addition without overflow is for both
360+ // sides to be zero
361+ //
362+ // So the result cannot be zero.
358363 Some ( unsafe { $Ty:: new_unchecked( result) } )
359364 } else {
360365 None
@@ -386,8 +391,13 @@ macro_rules! nonzero_unsigned_operations {
386391 without modifying the original"]
387392 #[ inline]
388393 pub const fn saturating_add( self , other: $Int) -> $Ty {
389- // SAFETY: $Int::saturating_add returns $Int::MAX on overflow
390- // so the result cannot be zero.
394+ // SAFETY:
395+ // - `saturating_add` returns `u*::MAX` on overflow, which is non-zero
396+ // - `self` and `other` are non-zero
397+ // - the only way to get zero from an addition without overflow is for both
398+ // sides to be zero
399+ //
400+ // So the result cannot be zero.
391401 unsafe { $Ty:: new_unchecked( self . get( ) . saturating_add( other) ) }
392402 }
393403
@@ -1000,9 +1010,13 @@ macro_rules! nonzero_unsigned_signed_operations {
10001010 #[ inline]
10011011 pub const fn checked_mul( self , other: $Ty) -> Option <$Ty> {
10021012 if let Some ( result) = self . get( ) . checked_mul( other. get( ) ) {
1003- // SAFETY: checked_mul returns None on overflow
1004- // and `other` is also non-null
1005- // so the result cannot be zero.
1013+ // SAFETY:
1014+ // - `checked_mul` returns `None` on overflow
1015+ // - `self` and `other` are non-zero
1016+ // - the only way to get zero from a multiplication without overflow is for one
1017+ // of the sides to be zero
1018+ //
1019+ // So the result cannot be zero.
10061020 Some ( unsafe { $Ty:: new_unchecked( result) } )
10071021 } else {
10081022 None
@@ -1034,9 +1048,14 @@ macro_rules! nonzero_unsigned_signed_operations {
10341048 without modifying the original"]
10351049 #[ inline]
10361050 pub const fn saturating_mul( self , other: $Ty) -> $Ty {
1037- // SAFETY: saturating_mul returns u*::MAX on overflow
1038- // and `other` is also non-null
1039- // so the result cannot be zero.
1051+ // SAFETY:
1052+ // - `saturating_mul` returns `u*::MAX`/`i*::MAX`/`i*::MIN` on overflow/underflow,
1053+ // all of which are non-zero
1054+ // - `self` and `other` are non-zero
1055+ // - the only way to get zero from a multiplication without overflow is for one
1056+ // of the sides to be zero
1057+ //
1058+ // So the result cannot be zero.
10401059 unsafe { $Ty:: new_unchecked( self . get( ) . saturating_mul( other. get( ) ) ) }
10411060 }
10421061
@@ -1107,8 +1126,13 @@ macro_rules! nonzero_unsigned_signed_operations {
11071126 #[ inline]
11081127 pub const fn checked_pow( self , other: u32 ) -> Option <$Ty> {
11091128 if let Some ( result) = self . get( ) . checked_pow( other) {
1110- // SAFETY: checked_pow returns None on overflow
1111- // so the result cannot be zero.
1129+ // SAFETY:
1130+ // - `checked_pow` returns `None` on overflow/underflow
1131+ // - `self` is non-zero
1132+ // - the only way to get zero from an exponentiation without overflow is
1133+ // for base to be zero
1134+ //
1135+ // So the result cannot be zero.
11121136 Some ( unsafe { $Ty:: new_unchecked( result) } )
11131137 } else {
11141138 None
@@ -1149,8 +1173,14 @@ macro_rules! nonzero_unsigned_signed_operations {
11491173 without modifying the original"]
11501174 #[ inline]
11511175 pub const fn saturating_pow( self , other: u32 ) -> $Ty {
1152- // SAFETY: saturating_pow returns u*::MAX on overflow
1153- // so the result cannot be zero.
1176+ // SAFETY:
1177+ // - `saturating_pow` returns `u*::MAX`/`i*::MAX`/`i*::MIN` on overflow/underflow,
1178+ // all of which are non-zero
1179+ // - `self` is non-zero
1180+ // - the only way to get zero from an exponentiation without overflow is
1181+ // for base to be zero
1182+ //
1183+ // So the result cannot be zero.
11541184 unsafe { $Ty:: new_unchecked( self . get( ) . saturating_pow( other) ) }
11551185 }
11561186 }
0 commit comments