diff options
| author | bors <bors@rust-lang.org> | 2024-03-18 04:15:14 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-03-18 04:15:14 +0000 |
| commit | 80e5694d6fd38bfa7900c0fd8971b2e7bebf66fa (patch) | |
| tree | 22fcb3cb7d4f1c3aaeb628aa5c85402efae94125 | |
| parent | 5608c7f9aaf380bd0c49bbcc350ecf8c5eb4b68c (diff) | |
| parent | e7d397024f6eebdd36bfa31ea8cf56496d348f7f (diff) | |
| download | rust-80e5694d6fd38bfa7900c0fd8971b2e7bebf66fa.tar.gz rust-80e5694d6fd38bfa7900c0fd8971b2e7bebf66fa.zip | |
Auto merge of #121952 - JarvisCraft:master, r=workingjubilee
feat: implement `{Div,Rem}Assign<NonZero<X>>` on `X`
# Description
This PR implements `DivAssign<X>` and `RemAssign<X>` on `X` as suggested in rust-lang/libs-team#346.
Since this is just a trait implementation on an already stable type, for which non-assign operator traits are already stable, I suggest that it is an insta-stable feature.
| -rw-r--r-- | library/core/src/num/nonzero.rs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 1f7a4e276f5..a8f637280df 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -5,7 +5,7 @@ use crate::fmt; use crate::hash::{Hash, Hasher}; use crate::intrinsics; use crate::marker::{Freeze, StructuralPartialEq}; -use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem}; +use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign}; use crate::panic::{RefUnwindSafe, UnwindSafe}; use crate::ptr; use crate::str::FromStr; @@ -849,6 +849,16 @@ macro_rules! nonzero_integer_signedness_dependent_impls { } } + #[stable(feature = "nonzero_div_assign", since = "CURRENT_RUSTC_VERSION")] + impl DivAssign<$Ty> for $Int { + /// This operation rounds towards zero, + /// truncating any fractional part of the exact result, and cannot panic. + #[inline] + fn div_assign(&mut self, other: $Ty) { + *self = *self / other; + } + } + #[stable(feature = "nonzero_div", since = "1.51.0")] impl Rem<$Ty> for $Int { type Output = $Int; @@ -861,6 +871,15 @@ macro_rules! nonzero_integer_signedness_dependent_impls { unsafe { intrinsics::unchecked_rem(self, other.get()) } } } + + #[stable(feature = "nonzero_div_assign", since = "CURRENT_RUSTC_VERSION")] + impl RemAssign<$Ty> for $Int { + /// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic. + #[inline] + fn rem_assign(&mut self, other: $Ty) { + *self = *self % other; + } + } }; // Impls for signed nonzero types only. |
