Skip to content

Commit 8fea4a8

Browse files
authored
Rollup merge of #120244 - reitermarkus:nonzero-self, r=dtolnay
Use `Self` in `NonZero*` implementations. This slightly reduces the size of the eventual diff when making these generic, since this can be merged independently.
2 parents 09c31b8 + a77cc2c commit 8fea4a8

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

library/core/src/num/nonzero.rs

+48-43
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

@@ -508,29 +508,32 @@ macro_rules! nonzero_integer {
508508
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
509509
impl BitOr for $Ty {
510510
type Output = Self;
511+
511512
#[inline]
512513
fn bitor(self, rhs: Self) -> Self::Output {
513514
// SAFETY: since `self` and `rhs` are both nonzero, the
514515
// result of the bitwise-or will be nonzero.
515-
unsafe { $Ty::new_unchecked(self.get() | rhs.get()) }
516+
unsafe { Self::new_unchecked(self.get() | rhs.get()) }
516517
}
517518
}
518519

519520
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
520521
impl BitOr<$Int> for $Ty {
521522
type Output = Self;
523+
522524
#[inline]
523525
fn bitor(self, rhs: $Int) -> Self::Output {
524526
// SAFETY: since `self` is nonzero, the result of the
525527
// bitwise-or will be nonzero regardless of the value of
526528
// `rhs`.
527-
unsafe { $Ty::new_unchecked(self.get() | rhs) }
529+
unsafe { Self::new_unchecked(self.get() | rhs) }
528530
}
529531
}
530532

531533
#[stable(feature = "nonzero_bitor", since = "1.45.0")]
532534
impl BitOr<$Ty> for $Int {
533535
type Output = $Ty;
536+
534537
#[inline]
535538
fn bitor(self, rhs: $Ty) -> Self::Output {
536539
// SAFETY: since `rhs` is nonzero, the result of the
@@ -603,6 +606,7 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
603606
#[stable(feature = "nonzero_div", since = "1.51.0")]
604607
impl Div<$Ty> for $Int {
605608
type Output = $Int;
609+
606610
/// This operation rounds towards zero,
607611
/// truncating any fractional part of the exact result, and cannot panic.
608612
#[inline]
@@ -616,6 +620,7 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
616620
#[stable(feature = "nonzero_div", since = "1.51.0")]
617621
impl Rem<$Ty> for $Int {
618622
type Output = $Int;
623+
619624
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
620625
#[inline]
621626
fn rem(self, other: $Ty) -> $Int {
@@ -630,12 +635,12 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
630635
($Ty:ident signed $Int:ty) => {
631636
#[stable(feature = "signed_nonzero_neg", since = "1.71.0")]
632637
impl Neg for $Ty {
633-
type Output = $Ty;
638+
type Output = Self;
634639

635640
#[inline]
636-
fn neg(self) -> $Ty {
641+
fn neg(self) -> Self {
637642
// SAFETY: negation of nonzero cannot yield zero values.
638-
unsafe { $Ty::new_unchecked(self.get().neg()) }
643+
unsafe { Self::new_unchecked(self.get().neg()) }
639644
}
640645
}
641646

@@ -703,7 +708,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
703708
#[must_use = "this returns the result of the operation, \
704709
without modifying the original"]
705710
#[inline]
706-
pub const fn checked_add(self, other: $Int) -> Option<$Ty> {
711+
pub const fn checked_add(self, other: $Int) -> Option<Self> {
707712
if let Some(result) = self.get().checked_add(other) {
708713
// SAFETY:
709714
// - `checked_add` returns `None` on overflow
@@ -712,7 +717,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
712717
// sides to be zero
713718
//
714719
// So the result cannot be zero.
715-
Some(unsafe { $Ty::new_unchecked(result) })
720+
Some(unsafe { Self::new_unchecked(result) })
716721
} else {
717722
None
718723
}
@@ -742,15 +747,15 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
742747
#[must_use = "this returns the result of the operation, \
743748
without modifying the original"]
744749
#[inline]
745-
pub const fn saturating_add(self, other: $Int) -> $Ty {
750+
pub const fn saturating_add(self, other: $Int) -> Self {
746751
// SAFETY:
747752
// - `saturating_add` returns `u*::MAX` on overflow, which is non-zero
748753
// - `self` is non-zero
749754
// - the only way to get zero from an addition without overflow is for both
750755
// sides to be zero
751756
//
752757
// So the result cannot be zero.
753-
unsafe { $Ty::new_unchecked(self.get().saturating_add(other)) }
758+
unsafe { Self::new_unchecked(self.get().saturating_add(other)) }
754759
}
755760

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

787792
/// Returns the smallest power of two greater than or equal to n.
@@ -812,11 +817,11 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
812817
#[must_use = "this returns the result of the operation, \
813818
without modifying the original"]
814819
#[inline]
815-
pub const fn checked_next_power_of_two(self) -> Option<$Ty> {
820+
pub const fn checked_next_power_of_two(self) -> Option<Self> {
816821
if let Some(nz) = self.get().checked_next_power_of_two() {
817822
// SAFETY: The next power of two is positive
818823
// and overflow is checked.
819-
Some(unsafe { $Ty::new_unchecked(nz) })
824+
Some(unsafe { Self::new_unchecked(nz) })
820825
} else {
821826
None
822827
}
@@ -902,9 +907,9 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
902907
pub const fn midpoint(self, rhs: Self) -> Self {
903908
// SAFETY: The only way to get `0` with midpoint is to have two opposite or
904909
// 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
910+
// of the unsignedness of this number and also because `Self` is guaranteed to
906911
// never being 0.
907-
unsafe { $Ty::new_unchecked(self.get().midpoint(rhs.get())) }
912+
unsafe { Self::new_unchecked(self.get().midpoint(rhs.get())) }
908913
}
909914

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

10081013
/// Checked absolute value.
@@ -1031,10 +1036,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
10311036
#[must_use = "this returns the result of the operation, \
10321037
without modifying the original"]
10331038
#[inline]
1034-
pub const fn checked_abs(self) -> Option<$Ty> {
1039+
pub const fn checked_abs(self) -> Option<Self> {
10351040
if let Some(nz) = self.get().checked_abs() {
10361041
// SAFETY: absolute value of nonzero cannot yield zero values.
1037-
Some(unsafe { $Ty::new_unchecked(nz) })
1042+
Some(unsafe { Self::new_unchecked(nz) })
10381043
} else {
10391044
None
10401045
}
@@ -1066,11 +1071,11 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
10661071
#[must_use = "this returns the result of the operation, \
10671072
without modifying the original"]
10681073
#[inline]
1069-
pub const fn overflowing_abs(self) -> ($Ty, bool) {
1074+
pub const fn overflowing_abs(self) -> (Self, bool) {
10701075
let (nz, flag) = self.get().overflowing_abs();
10711076
(
10721077
// SAFETY: absolute value of nonzero cannot yield zero values.
1073-
unsafe { $Ty::new_unchecked(nz) },
1078+
unsafe { Self::new_unchecked(nz) },
10741079
flag,
10751080
)
10761081
}
@@ -1105,9 +1110,9 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
11051110
#[must_use = "this returns the result of the operation, \
11061111
without modifying the original"]
11071112
#[inline]
1108-
pub const fn saturating_abs(self) -> $Ty {
1113+
pub const fn saturating_abs(self) -> Self {
11091114
// SAFETY: absolute value of nonzero cannot yield zero values.
1110-
unsafe { $Ty::new_unchecked(self.get().saturating_abs()) }
1115+
unsafe { Self::new_unchecked(self.get().saturating_abs()) }
11111116
}
11121117

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

11461151
/// Computes the absolute value of self
@@ -1250,10 +1255,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
12501255
#[inline]
12511256
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
12521257
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
1253-
pub const fn checked_neg(self) -> Option<$Ty> {
1258+
pub const fn checked_neg(self) -> Option<Self> {
12541259
if let Some(result) = self.get().checked_neg() {
12551260
// SAFETY: negation of nonzero cannot yield zero values.
1256-
return Some(unsafe { $Ty::new_unchecked(result) });
1261+
return Some(unsafe { Self::new_unchecked(result) });
12571262
}
12581263
None
12591264
}
@@ -1282,10 +1287,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
12821287
#[inline]
12831288
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
12841289
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
1285-
pub const fn overflowing_neg(self) -> ($Ty, bool) {
1290+
pub const fn overflowing_neg(self) -> (Self, bool) {
12861291
let (result, overflow) = self.get().overflowing_neg();
12871292
// SAFETY: negation of nonzero cannot yield zero values.
1288-
((unsafe { $Ty::new_unchecked(result) }), overflow)
1293+
((unsafe { Self::new_unchecked(result) }), overflow)
12891294
}
12901295

12911296
/// Saturating negation. Computes `-self`,
@@ -1317,11 +1322,11 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
13171322
#[inline]
13181323
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
13191324
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
1320-
pub const fn saturating_neg(self) -> $Ty {
1325+
pub const fn saturating_neg(self) -> Self {
13211326
if let Some(result) = self.checked_neg() {
13221327
return result;
13231328
}
1324-
$Ty::MAX
1329+
Self::MAX
13251330
}
13261331

13271332
/// Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary
@@ -1349,10 +1354,10 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
13491354
#[inline]
13501355
#[stable(feature = "nonzero_negation_ops", since = "1.71.0")]
13511356
#[rustc_const_stable(feature = "nonzero_negation_ops", since = "1.71.0")]
1352-
pub const fn wrapping_neg(self) -> $Ty {
1357+
pub const fn wrapping_neg(self) -> Self {
13531358
let result = self.get().wrapping_neg();
13541359
// SAFETY: negation of nonzero cannot yield zero values.
1355-
unsafe { $Ty::new_unchecked(result) }
1360+
unsafe { Self::new_unchecked(result) }
13561361
}
13571362
};
13581363
}

0 commit comments

Comments
 (0)