@@ -353,8 +353,13 @@ macro_rules! nonzero_unsigned_operations {
353
353
#[ inline]
354
354
pub const fn checked_add( self , other: $Int) -> Option <$Ty> {
355
355
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.
358
363
Some ( unsafe { $Ty:: new_unchecked( result) } )
359
364
} else {
360
365
None
@@ -386,8 +391,13 @@ macro_rules! nonzero_unsigned_operations {
386
391
without modifying the original"]
387
392
#[ inline]
388
393
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.
391
401
unsafe { $Ty:: new_unchecked( self . get( ) . saturating_add( other) ) }
392
402
}
393
403
@@ -1000,9 +1010,13 @@ macro_rules! nonzero_unsigned_signed_operations {
1000
1010
#[ inline]
1001
1011
pub const fn checked_mul( self , other: $Ty) -> Option <$Ty> {
1002
1012
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.
1006
1020
Some ( unsafe { $Ty:: new_unchecked( result) } )
1007
1021
} else {
1008
1022
None
@@ -1034,9 +1048,14 @@ macro_rules! nonzero_unsigned_signed_operations {
1034
1048
without modifying the original"]
1035
1049
#[ inline]
1036
1050
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.
1040
1059
unsafe { $Ty:: new_unchecked( self . get( ) . saturating_mul( other. get( ) ) ) }
1041
1060
}
1042
1061
@@ -1107,8 +1126,13 @@ macro_rules! nonzero_unsigned_signed_operations {
1107
1126
#[ inline]
1108
1127
pub const fn checked_pow( self , other: u32 ) -> Option <$Ty> {
1109
1128
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.
1112
1136
Some ( unsafe { $Ty:: new_unchecked( result) } )
1113
1137
} else {
1114
1138
None
@@ -1149,8 +1173,14 @@ macro_rules! nonzero_unsigned_signed_operations {
1149
1173
without modifying the original"]
1150
1174
#[ inline]
1151
1175
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.
1154
1184
unsafe { $Ty:: new_unchecked( self . get( ) . saturating_pow( other) ) }
1155
1185
}
1156
1186
}
0 commit comments