Skip to content

Commit

Permalink
Rollup merge of rust-lang#120244 - reitermarkus:nonzero-self, r=dtolnay
Browse files Browse the repository at this point in the history
Use `Self` in `NonZero*` implementations.

This slightly reduces the size of the eventual diff when making these generic, since this can be merged independently.
  • Loading branch information
fmease committed Jan 23, 2024
2 parents db91b88 + a77cc2c commit 3e94cc7
Showing 1 changed file with 48 additions and 43 deletions.
91 changes: 48 additions & 43 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ macro_rules! nonzero_integer {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_mul(self, other: $Ty) -> Option<$Ty> {
pub const fn checked_mul(self, other: Self) -> Option<Self> {
if let Some(result) = self.get().checked_mul(other.get()) {
// SAFETY:
// - `checked_mul` returns `None` on overflow
Expand All @@ -326,7 +326,7 @@ macro_rules! nonzero_integer {
// of the sides to be zero
//
// So the result cannot be zero.
Some(unsafe { $Ty::new_unchecked(result) })
Some(unsafe { Self::new_unchecked(result) })
} else {
None
}
Expand Down Expand Up @@ -356,7 +356,7 @@ macro_rules! nonzero_integer {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_mul(self, other: $Ty) -> $Ty {
pub const fn saturating_mul(self, other: Self) -> Self {
// SAFETY:
// - `saturating_mul` returns `u*::MAX`/`i*::MAX`/`i*::MIN` on overflow/underflow,
// all of which are non-zero
Expand All @@ -365,7 +365,7 @@ macro_rules! nonzero_integer {
// of the sides to be zero
//
// So the result cannot be zero.
unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) }
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
}

/// Multiplies two non-zero integers together,
Expand Down Expand Up @@ -403,9 +403,9 @@ macro_rules! nonzero_integer {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_mul(self, other: $Ty) -> $Ty {
pub const unsafe fn unchecked_mul(self, other: Self) -> Self {
// SAFETY: The caller ensures there is no overflow.
unsafe { $Ty::new_unchecked(self.get().unchecked_mul(other.get())) }
unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) }
}

/// Raises non-zero value to an integer power.
Expand Down Expand Up @@ -433,7 +433,7 @@ macro_rules! nonzero_integer {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_pow(self, other: u32) -> Option<$Ty> {
pub const fn checked_pow(self, other: u32) -> Option<Self> {
if let Some(result) = self.get().checked_pow(other) {
// SAFETY:
// - `checked_pow` returns `None` on overflow/underflow
Expand All @@ -442,7 +442,7 @@ macro_rules! nonzero_integer {
// for base to be zero
//
// So the result cannot be zero.
Some(unsafe { $Ty::new_unchecked(result) })
Some(unsafe { Self::new_unchecked(result) })
} else {
None
}
Expand Down Expand Up @@ -481,7 +481,7 @@ macro_rules! nonzero_integer {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_pow(self, other: u32) -> $Ty {
pub const fn saturating_pow(self, other: u32) -> Self {
// SAFETY:
// - `saturating_pow` returns `u*::MAX`/`i*::MAX`/`i*::MIN` on overflow/underflow,
// all of which are non-zero
Expand All @@ -490,7 +490,7 @@ macro_rules! nonzero_integer {
// for base to be zero
//
// So the result cannot be zero.
unsafe { $Ty::new_unchecked(self.get().saturating_pow(other)) }
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
}
}

Expand All @@ -508,29 +508,32 @@ macro_rules! nonzero_integer {
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
impl BitOr for $Ty {
type Output = Self;

#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
// SAFETY: since `self` and `rhs` are both nonzero, the
// result of the bitwise-or will be nonzero.
unsafe { $Ty::new_unchecked(self.get() | rhs.get()) }
unsafe { Self::new_unchecked(self.get() | rhs.get()) }
}
}

#[stable(feature = "nonzero_bitor", since = "1.45.0")]
impl BitOr<$Int> for $Ty {
type Output = Self;

#[inline]
fn bitor(self, rhs: $Int) -> Self::Output {
// SAFETY: since `self` is nonzero, the result of the
// bitwise-or will be nonzero regardless of the value of
// `rhs`.
unsafe { $Ty::new_unchecked(self.get() | rhs) }
unsafe { Self::new_unchecked(self.get() | rhs) }
}
}

#[stable(feature = "nonzero_bitor", since = "1.45.0")]
impl BitOr<$Ty> for $Int {
type Output = $Ty;

#[inline]
fn bitor(self, rhs: $Ty) -> Self::Output {
// SAFETY: since `rhs` is nonzero, the result of the
Expand Down Expand Up @@ -603,6 +606,7 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
#[stable(feature = "nonzero_div", since = "1.51.0")]
impl Div<$Ty> for $Int {
type Output = $Int;

/// This operation rounds towards zero,
/// truncating any fractional part of the exact result, and cannot panic.
#[inline]
Expand All @@ -616,6 +620,7 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
#[stable(feature = "nonzero_div", since = "1.51.0")]
impl Rem<$Ty> for $Int {
type Output = $Int;

/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
#[inline]
fn rem(self, other: $Ty) -> $Int {
Expand All @@ -630,12 +635,12 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
($Ty:ident signed $Int:ty) => {
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
impl Neg for $Ty {
type Output = $Ty;
type Output = Self;

#[inline]
fn neg(self) -> $Ty {
fn neg(self) -> Self {
// SAFETY: negation of nonzero cannot yield zero values.
unsafe { $Ty::new_unchecked(self.get().neg()) }
unsafe { Self::new_unchecked(self.get().neg()) }
}
}

Expand Down Expand Up @@ -703,7 +708,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_add(self, other: $Int) -> Option<$Ty> {
pub const fn checked_add(self, other: $Int) -> Option<Self> {
if let Some(result) = self.get().checked_add(other) {
// SAFETY:
// - `checked_add` returns `None` on overflow
Expand All @@ -712,7 +717,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
// sides to be zero
//
// So the result cannot be zero.
Some(unsafe { $Ty::new_unchecked(result) })
Some(unsafe { Self::new_unchecked(result) })
} else {
None
}
Expand Down Expand Up @@ -742,15 +747,15 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_add(self, other: $Int) -> $Ty {
pub const fn saturating_add(self, other: $Int) -> Self {
// SAFETY:
// - `saturating_add` returns `u*::MAX` on overflow, which is non-zero
// - `self` is 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 { Self::new_unchecked(self.get().saturating_add(other)) }
}

/// Adds an unsigned integer to a non-zero value,
Expand Down Expand Up @@ -779,9 +784,9 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const unsafe fn unchecked_add(self, other: $Int) -> $Ty {
pub const unsafe fn unchecked_add(self, other: $Int) -> Self {
// SAFETY: The caller ensures there is no overflow.
unsafe { $Ty::new_unchecked(self.get().unchecked_add(other)) }
unsafe { Self::new_unchecked(self.get().unchecked_add(other)) }
}

/// Returns the smallest power of two greater than or equal to n.
Expand Down Expand Up @@ -812,11 +817,11 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_next_power_of_two(self) -> Option<$Ty> {
pub const fn checked_next_power_of_two(self) -> Option<Self> {
if let Some(nz) = self.get().checked_next_power_of_two() {
// SAFETY: The next power of two is positive
// and overflow is checked.
Some(unsafe { $Ty::new_unchecked(nz) })
Some(unsafe { Self::new_unchecked(nz) })
} else {
None
}
Expand Down Expand Up @@ -902,9 +907,9 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
pub const fn midpoint(self, rhs: Self) -> Self {
// SAFETY: The only way to get `0` with midpoint is to have two opposite or
// near opposite numbers: (-5, 5), (0, 1), (0, 0) which is impossible because
// of the unsignedness of this number and also because $Ty is guaranteed to
// of the unsignedness of this number and also because `Self` is guaranteed to
// never being 0.
unsafe { $Ty::new_unchecked(self.get().midpoint(rhs.get())) }
unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
}

/// Returns `true` if and only if `self == (1 << k)` for some `k`.
Expand Down Expand Up @@ -1000,9 +1005,9 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn abs(self) -> $Ty {
pub const fn abs(self) -> Self {
// SAFETY: This cannot overflow to zero.
unsafe { $Ty::new_unchecked(self.get().abs()) }
unsafe { Self::new_unchecked(self.get().abs()) }
}

/// Checked absolute value.
Expand Down Expand Up @@ -1031,10 +1036,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_abs(self) -> Option<$Ty> {
pub const fn checked_abs(self) -> Option<Self> {
if let Some(nz) = self.get().checked_abs() {
// SAFETY: absolute value of nonzero cannot yield zero values.
Some(unsafe { $Ty::new_unchecked(nz) })
Some(unsafe { Self::new_unchecked(nz) })
} else {
None
}
Expand Down Expand Up @@ -1066,11 +1071,11 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn overflowing_abs(self) -> ($Ty, bool) {
pub const fn overflowing_abs(self) -> (Self, bool) {
let (nz, flag) = self.get().overflowing_abs();
(
// SAFETY: absolute value of nonzero cannot yield zero values.
unsafe { $Ty::new_unchecked(nz) },
unsafe { Self::new_unchecked(nz) },
flag,
)
}
Expand Down Expand Up @@ -1105,9 +1110,9 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_abs(self) -> $Ty {
pub const fn saturating_abs(self) -> Self {
// SAFETY: absolute value of nonzero cannot yield zero values.
unsafe { $Ty::new_unchecked(self.get().saturating_abs()) }
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
}

/// Wrapping absolute value, see
Expand Down Expand Up @@ -1138,9 +1143,9 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn wrapping_abs(self) -> $Ty {
pub const fn wrapping_abs(self) -> Self {
// SAFETY: absolute value of nonzero cannot yield zero values.
unsafe { $Ty::new_unchecked(self.get().wrapping_abs()) }
unsafe { Self::new_unchecked(self.get().wrapping_abs()) }
}

/// Computes the absolute value of self
Expand Down Expand Up @@ -1250,10 +1255,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn checked_neg(self) -> Option<$Ty> {
pub const fn checked_neg(self) -> Option<Self> {
if let Some(result) = self.get().checked_neg() {
// SAFETY: negation of nonzero cannot yield zero values.
return Some(unsafe { $Ty::new_unchecked(result) });
return Some(unsafe { Self::new_unchecked(result) });
}
None
}
Expand Down Expand Up @@ -1282,10 +1287,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn overflowing_neg(self) -> ($Ty, bool) {
pub const fn overflowing_neg(self) -> (Self, bool) {
let (result, overflow) = self.get().overflowing_neg();
// SAFETY: negation of nonzero cannot yield zero values.
((unsafe { $Ty::new_unchecked(result) }), overflow)
((unsafe { Self::new_unchecked(result) }), overflow)
}

/// Saturating negation. Computes `-self`,
Expand Down Expand Up @@ -1317,11 +1322,11 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn saturating_neg(self) -> $Ty {
pub const fn saturating_neg(self) -> Self {
if let Some(result) = self.checked_neg() {
return result;
}
$Ty::MAX
Self::MAX
}

/// Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary
Expand Down Expand Up @@ -1349,10 +1354,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
#[inline]
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
pub const fn wrapping_neg(self) -> $Ty {
pub const fn wrapping_neg(self) -> Self {
let result = self.get().wrapping_neg();
// SAFETY: negation of nonzero cannot yield zero values.
unsafe { $Ty::new_unchecked(result) }
unsafe { Self::new_unchecked(result) }
}
};
}
Expand Down

0 comments on commit 3e94cc7

Please sign in to comment.