Skip to content

Commit 02eaa0f

Browse files
committed
Use Self in NonZero* implementations.
1 parent 3066253 commit 02eaa0f

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

library/core/src/num/nonzero.rs

+46-46
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ macro_rules! nonzero_integer {
317317
#[must_use = "this returns the result of the operation, \
318318
without modifying the original"]
319319
#[inline]
320-
pub const fn checked_mul(self, other: $Ty) -> Option<$Ty> {
320+
pub const fn checked_mul(self, other: Self) -> Option<Self> {
321321
if let Some(result) = self.get().checked_mul(other.get()) {
322322
// SAFETY:
323323
// - `checked_mul` returns `None` on overflow
@@ -326,7 +326,7 @@ macro_rules! nonzero_integer {
326326
// of the sides to be zero
327327
//
328328
// So the result cannot be zero.
329-
Some(unsafe { $Ty::new_unchecked(result) })
329+
Some(unsafe { Self::new_unchecked(result) })
330330
} else {
331331
None
332332
}
@@ -356,7 +356,7 @@ macro_rules! nonzero_integer {
356356
#[must_use = "this returns the result of the operation, \
357357
without modifying the original"]
358358
#[inline]
359-
pub const fn saturating_mul(self, other: $Ty) -> $Ty {
359+
pub const fn saturating_mul(self, other: Self) -> Self {
360360
// SAFETY:
361361
// - `saturating_mul` returns `u*::MAX`/`i*::MAX`/`i*::MIN` on overflow/underflow,
362362
// all of which are non-zero
@@ -365,7 +365,7 @@ macro_rules! nonzero_integer {
365365
// of the sides to be zero
366366
//
367367
// So the result cannot be zero.
368-
unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) }
368+
unsafe { Self::new_unchecked(self.get().saturating_mul(other.get())) }
369369
}
370370

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

411411
/// Raises non-zero value to an integer power.
@@ -433,7 +433,7 @@ macro_rules! nonzero_integer {
433433
#[must_use = "this returns the result of the operation, \
434434
without modifying the original"]
435435
#[inline]
436-
pub const fn checked_pow(self, other: u32) -> Option<$Ty> {
436+
pub const fn checked_pow(self, other: u32) -> Option<Self> {
437437
if let Some(result) = self.get().checked_pow(other) {
438438
// SAFETY:
439439
// - `checked_pow` returns `None` on overflow/underflow
@@ -442,7 +442,7 @@ macro_rules! nonzero_integer {
442442
// for base to be zero
443443
//
444444
// So the result cannot be zero.
445-
Some(unsafe { $Ty::new_unchecked(result) })
445+
Some(unsafe { Self::new_unchecked(result) })
446446
} else {
447447
None
448448
}
@@ -481,7 +481,7 @@ macro_rules! nonzero_integer {
481481
#[must_use = "this returns the result of the operation, \
482482
without modifying the original"]
483483
#[inline]
484-
pub const fn saturating_pow(self, other: u32) -> $Ty {
484+
pub const fn saturating_pow(self, other: u32) -> Self {
485485
// SAFETY:
486486
// - `saturating_pow` returns `u*::MAX`/`i*::MAX`/`i*::MIN` on overflow/underflow,
487487
// all of which are non-zero
@@ -490,7 +490,7 @@ macro_rules! nonzero_integer {
490490
// for base to be zero
491491
//
492492
// So the result cannot be zero.
493-
unsafe { $Ty::new_unchecked(self.get().saturating_pow(other)) }
493+
unsafe { Self::new_unchecked(self.get().saturating_pow(other)) }
494494
}
495495
}
496496

@@ -512,7 +512,7 @@ macro_rules! nonzero_integer {
512512
fn bitor(self, rhs: Self) -> Self::Output {
513513
// SAFETY: since `self` and `rhs` are both nonzero, the
514514
// result of the bitwise-or will be nonzero.
515-
unsafe { $Ty::new_unchecked(self.get() | rhs.get()) }
515+
unsafe { Self::new_unchecked(self.get() | rhs.get()) }
516516
}
517517
}
518518

@@ -524,7 +524,7 @@ macro_rules! nonzero_integer {
524524
// SAFETY: since `self` is nonzero, the result of the
525525
// bitwise-or will be nonzero regardless of the value of
526526
// `rhs`.
527-
unsafe { $Ty::new_unchecked(self.get() | rhs) }
527+
unsafe { Self::new_unchecked(self.get() | rhs) }
528528
}
529529
}
530530

@@ -536,7 +536,7 @@ macro_rules! nonzero_integer {
536536
// SAFETY: since `rhs` is nonzero, the result of the
537537
// bitwise-or will be nonzero regardless of the value of
538538
// `self`.
539-
unsafe { $Ty::new_unchecked(self | rhs.get()) }
539+
unsafe { Self::new_unchecked(self | rhs.get()) }
540540
}
541541
}
542542

@@ -606,7 +606,7 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
606606
/// This operation rounds towards zero,
607607
/// truncating any fractional part of the exact result, and cannot panic.
608608
#[inline]
609-
fn div(self, other: $Ty) -> $Int {
609+
fn div(self, other: Self) -> $Int {
610610
// SAFETY: div by zero is checked because `other` is a nonzero,
611611
// and MIN/-1 is checked because `self` is an unsigned int.
612612
unsafe { crate::intrinsics::unchecked_div(self, other.get()) }
@@ -618,7 +618,7 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
618618
type Output = $Int;
619619
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
620620
#[inline]
621-
fn rem(self, other: $Ty) -> $Int {
621+
fn rem(self, other: Self) -> $Int {
622622
// SAFETY: rem by zero is checked because `other` is a nonzero,
623623
// and MIN/-1 is checked because `self` is an unsigned int.
624624
unsafe { crate::intrinsics::unchecked_rem(self, other.get()) }
@@ -630,12 +630,12 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
630630
($Ty:ident signed $Int:ty) => {
631631
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
632632
impl Neg for $Ty {
633-
type Output = $Ty;
633+
type Output = Self;
634634

635635
#[inline]
636-
fn neg(self) -> $Ty {
636+
fn neg(self) -> Self {
637637
// SAFETY: negation of nonzero cannot yield zero values.
638-
unsafe { $Ty::new_unchecked(self.get().neg()) }
638+
unsafe { Self::new_unchecked(self.get().neg()) }
639639
}
640640
}
641641

@@ -703,7 +703,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
703703
#[must_use = "this returns the result of the operation, \
704704
without modifying the original"]
705705
#[inline]
706-
pub const fn checked_add(self, other: $Int) -> Option<$Ty> {
706+
pub const fn checked_add(self, other: $Int) -> Option<Self> {
707707
if let Some(result) = self.get().checked_add(other) {
708708
// SAFETY:
709709
// - `checked_add` returns `None` on overflow
@@ -712,7 +712,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
712712
// sides to be zero
713713
//
714714
// So the result cannot be zero.
715-
Some(unsafe { $Ty::new_unchecked(result) })
715+
Some(unsafe { Self::new_unchecked(result) })
716716
} else {
717717
None
718718
}
@@ -742,15 +742,15 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
742742
#[must_use = "this returns the result of the operation, \
743743
without modifying the original"]
744744
#[inline]
745-
pub const fn saturating_add(self, other: $Int) -> $Ty {
745+
pub const fn saturating_add(self, other: $Int) -> Self {
746746
// SAFETY:
747747
// - `saturating_add` returns `u*::MAX` on overflow, which is non-zero
748748
// - `self` is non-zero
749749
// - the only way to get zero from an addition without overflow is for both
750750
// sides to be zero
751751
//
752752
// So the result cannot be zero.
753-
unsafe { $Ty::new_unchecked(self.get().saturating_add(other)) }
753+
unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
754754
}
755755

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

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

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

10081008
/// Checked absolute value.
@@ -1031,10 +1031,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
10311031
#[must_use = "this returns the result of the operation, \
10321032
without modifying the original"]
10331033
#[inline]
1034-
pub const fn checked_abs(self) -> Option<$Ty> {
1034+
pub const fn checked_abs(self) -> Option<Self> {
10351035
if let Some(nz) = self.get().checked_abs() {
10361036
// SAFETY: absolute value of nonzero cannot yield zero values.
1037-
Some(unsafe { $Ty::new_unchecked(nz) })
1037+
Some(unsafe { Self::new_unchecked(nz) })
10381038
} else {
10391039
None
10401040
}
@@ -1066,11 +1066,11 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
10661066
#[must_use = "this returns the result of the operation, \
10671067
without modifying the original"]
10681068
#[inline]
1069-
pub const fn overflowing_abs(self) -> ($Ty, bool) {
1069+
pub const fn overflowing_abs(self) -> (Self, bool) {
10701070
let (nz, flag) = self.get().overflowing_abs();
10711071
(
10721072
// SAFETY: absolute value of nonzero cannot yield zero values.
1073-
unsafe { $Ty::new_unchecked(nz) },
1073+
unsafe { Self::new_unchecked(nz) },
10741074
flag,
10751075
)
10761076
}
@@ -1105,9 +1105,9 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
11051105
#[must_use = "this returns the result of the operation, \
11061106
without modifying the original"]
11071107
#[inline]
1108-
pub const fn saturating_abs(self) -> $Ty {
1108+
pub const fn saturating_abs(self) -> Self {
11091109
// SAFETY: absolute value of nonzero cannot yield zero values.
1110-
unsafe { $Ty::new_unchecked(self.get().saturating_abs()) }
1110+
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
11111111
}
11121112

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

11461146
/// Computes the absolute value of self
@@ -1250,10 +1250,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
12501250
#[inline]
12511251
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
12521252
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
1253-
pub const fn checked_neg(self) -> Option<$Ty> {
1253+
pub const fn checked_neg(self) -> Option<Self> {
12541254
if let Some(result) = self.get().checked_neg() {
12551255
// SAFETY: negation of nonzero cannot yield zero values.
1256-
return Some(unsafe { $Ty::new_unchecked(result) });
1256+
return Some(unsafe { Self::new_unchecked(result) });
12571257
}
12581258
None
12591259
}
@@ -1282,10 +1282,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
12821282
#[inline]
12831283
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
12841284
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
1285-
pub const fn overflowing_neg(self) -> ($Ty, bool) {
1285+
pub const fn overflowing_neg(self) -> (Self, bool) {
12861286
let (result, overflow) = self.get().overflowing_neg();
12871287
// SAFETY: negation of nonzero cannot yield zero values.
1288-
((unsafe { $Ty::new_unchecked(result) }), overflow)
1288+
((unsafe { Self::new_unchecked(result) }), overflow)
12891289
}
12901290

12911291
/// Saturating negation. Computes `-self`,
@@ -1317,11 +1317,11 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
13171317
#[inline]
13181318
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
13191319
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
1320-
pub const fn saturating_neg(self) -> $Ty {
1320+
pub const fn saturating_neg(self) -> Self {
13211321
if let Some(result) = self.checked_neg() {
13221322
return result;
13231323
}
1324-
$Ty::MAX
1324+
Self::MAX
13251325
}
13261326

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

0 commit comments

Comments
 (0)