Skip to content

Commit

Permalink
Rollup merge of rust-lang#136019 - scottmcm:alias-unchecked-div, r=Ma…
Browse files Browse the repository at this point in the history
…rk-Simulacrum

Add an `unchecked_div` alias to the `Div<NonZero<_>>` impls

Inspired by rust-lang/libs-team#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).

![image](https://github.com/user-attachments/assets/cf2a3c06-4876-49c1-8e33-64cd431c772a)
  • Loading branch information
jhpratt authored Jan 26, 2025
2 parents 530c695 + 2d11559 commit 24a9bab
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]
Expand Down

0 comments on commit 24a9bab

Please sign in to comment.