diff options
| author | Jacob Pratt <jacob@jhpratt.dev> | 2025-01-26 01:51:18 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-26 01:51:18 -0500 |
| commit | 359c5047aba8e11112a30e2573643ac9d767eefa (patch) | |
| tree | 50d7d832b03b00b561e0ea05e3d376b972d2f608 | |
| parent | c40936fc2fdb1cbf39bde262388304d9dc438b8f (diff) | |
| parent | 2d11559f56bbe112b13d2fe43fbfd40b7258f34e (diff) | |
| download | rust-359c5047aba8e11112a30e2573643ac9d767eefa.tar.gz rust-359c5047aba8e11112a30e2573643ac9d767eefa.zip | |
Rollup merge of #136019 - scottmcm:alias-unchecked-div, r=Mark-Simulacrum
Add an `unchecked_div` alias to the `Div<NonZero<_>>` impls Inspired by https://github.com/rust-lang/libs-team/issues/526, if people are looking for `unchecked_div`, point them to `u32: Div<NonZero<u32>>` and friends which do no runtime checks -- and are safe! -- rather than today's behaviour of [the intrinsic being the top result](https://doc.rust-lang.org/std/?search=unchecked_div). 
| -rw-r--r-- | library/core/src/num/nonzero.rs | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 8089d616409..61e83ebfad7 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -1185,8 +1185,12 @@ macro_rules! nonzero_integer_signedness_dependent_impls { impl Div<NonZero<$Int>> for $Int { type Output = $Int; + /// Same as `self / other.get()`, but because `other` is a `NonZero<_>`, + /// there's never a runtime check for division-by-zero. + /// /// This operation rounds towards zero, truncating any fractional /// part of the exact result, and cannot panic. + #[doc(alias = "unchecked_div")] #[inline] fn div(self, other: NonZero<$Int>) -> $Int { // SAFETY: Division by zero is checked because `other` is non-zero, @@ -1197,6 +1201,9 @@ macro_rules! nonzero_integer_signedness_dependent_impls { #[stable(feature = "nonzero_div_assign", since = "1.79.0")] impl DivAssign<NonZero<$Int>> for $Int { + /// Same as `self /= other.get()`, but because `other` is a `NonZero<_>`, + /// there's never a runtime check for division-by-zero. + /// /// This operation rounds towards zero, truncating any fractional /// part of the exact result, and cannot panic. #[inline] |
