Improve some comments for non-zero ops
This commit is contained in:
parent
274b5249eb
commit
c860ba1994
@ -353,8 +353,13 @@ impl $Ty {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub const fn checked_add(self, other: $Int) -> Option<$Ty> {
|
pub const fn checked_add(self, other: $Int) -> Option<$Ty> {
|
||||||
if let Some(result) = self.get().checked_add(other) {
|
if let Some(result) = self.get().checked_add(other) {
|
||||||
// SAFETY: $Int::checked_add returns None on overflow
|
// SAFETY:
|
||||||
// so the result cannot be zero.
|
// - `checked_add` returns `None` on overflow
|
||||||
|
// - `self` and `other` are non-zero
|
||||||
|
// - the only way to get zero from an addition without overflow is for both
|
||||||
|
// sides to be zero
|
||||||
|
//
|
||||||
|
// So the result cannot be zero.
|
||||||
Some(unsafe { $Ty::new_unchecked(result) })
|
Some(unsafe { $Ty::new_unchecked(result) })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -386,8 +391,13 @@ pub const fn checked_add(self, other: $Int) -> Option<$Ty> {
|
|||||||
without modifying the original"]
|
without modifying the original"]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn saturating_add(self, other: $Int) -> $Ty {
|
pub const fn saturating_add(self, other: $Int) -> $Ty {
|
||||||
// SAFETY: $Int::saturating_add returns $Int::MAX on overflow
|
// SAFETY:
|
||||||
// so the result cannot be zero.
|
// - `saturating_add` returns `u*::MAX` on overflow, which is non-zero
|
||||||
|
// - `self` and `other` are non-zero
|
||||||
|
// - the only way to get zero from an addition without overflow is for both
|
||||||
|
// sides to be zero
|
||||||
|
//
|
||||||
|
// So the result cannot be zero.
|
||||||
unsafe { $Ty::new_unchecked(self.get().saturating_add(other)) }
|
unsafe { $Ty::new_unchecked(self.get().saturating_add(other)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1000,9 +1010,13 @@ impl $Ty {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub const fn checked_mul(self, other: $Ty) -> Option<$Ty> {
|
pub const fn checked_mul(self, other: $Ty) -> Option<$Ty> {
|
||||||
if let Some(result) = self.get().checked_mul(other.get()) {
|
if let Some(result) = self.get().checked_mul(other.get()) {
|
||||||
// SAFETY: checked_mul returns None on overflow
|
// SAFETY:
|
||||||
// and `other` is also non-null
|
// - `checked_mul` returns `None` on overflow
|
||||||
// so the result cannot be zero.
|
// - `self` and `other` are non-zero
|
||||||
|
// - the only way to get zero from a multiplication without overflow is for one
|
||||||
|
// of the sides to be zero
|
||||||
|
//
|
||||||
|
// So the result cannot be zero.
|
||||||
Some(unsafe { $Ty::new_unchecked(result) })
|
Some(unsafe { $Ty::new_unchecked(result) })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -1034,9 +1048,14 @@ pub const fn checked_mul(self, other: $Ty) -> Option<$Ty> {
|
|||||||
without modifying the original"]
|
without modifying the original"]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn saturating_mul(self, other: $Ty) -> $Ty {
|
pub const fn saturating_mul(self, other: $Ty) -> $Ty {
|
||||||
// SAFETY: saturating_mul returns u*::MAX on overflow
|
// SAFETY:
|
||||||
// and `other` is also non-null
|
// - `saturating_mul` returns `u*::MAX`/`i*::MAX`/`i*::MIN` on overflow/underflow,
|
||||||
// so the result cannot be zero.
|
// all of which are non-zero
|
||||||
|
// - `self` and `other` are non-zero
|
||||||
|
// - the only way to get zero from a multiplication without overflow is for one
|
||||||
|
// of the sides to be zero
|
||||||
|
//
|
||||||
|
// So the result cannot be zero.
|
||||||
unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) }
|
unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1107,8 +1126,13 @@ pub const fn saturating_mul(self, other: $Ty) -> $Ty {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub const fn checked_pow(self, other: u32) -> Option<$Ty> {
|
pub const fn checked_pow(self, other: u32) -> Option<$Ty> {
|
||||||
if let Some(result) = self.get().checked_pow(other) {
|
if let Some(result) = self.get().checked_pow(other) {
|
||||||
// SAFETY: checked_pow returns None on overflow
|
// SAFETY:
|
||||||
// so the result cannot be zero.
|
// - `checked_pow` returns `None` on overflow/underflow
|
||||||
|
// - `self` is non-zero
|
||||||
|
// - the only way to get zero from an exponentiation without overflow is
|
||||||
|
// for base to be zero
|
||||||
|
//
|
||||||
|
// So the result cannot be zero.
|
||||||
Some(unsafe { $Ty::new_unchecked(result) })
|
Some(unsafe { $Ty::new_unchecked(result) })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -1149,8 +1173,14 @@ pub const fn checked_pow(self, other: u32) -> Option<$Ty> {
|
|||||||
without modifying the original"]
|
without modifying the original"]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn saturating_pow(self, other: u32) -> $Ty {
|
pub const fn saturating_pow(self, other: u32) -> $Ty {
|
||||||
// SAFETY: saturating_pow returns u*::MAX on overflow
|
// SAFETY:
|
||||||
// so the result cannot be zero.
|
// - `saturating_pow` returns `u*::MAX`/`i*::MAX`/`i*::MIN` on overflow/underflow,
|
||||||
|
// all of which are non-zero
|
||||||
|
// - `self` is non-zero
|
||||||
|
// - the only way to get zero from an exponentiation without overflow is
|
||||||
|
// for base to be zero
|
||||||
|
//
|
||||||
|
// So the result cannot be zero.
|
||||||
unsafe { $Ty::new_unchecked(self.get().saturating_pow(other)) }
|
unsafe { $Ty::new_unchecked(self.get().saturating_pow(other)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user