Skip to content

Commit 80e5694

Browse files
committed
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.
2 parents 5608c7f + e7d3970 commit 80e5694

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Diff for: library/core/src/num/nonzero.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::fmt;
55
use crate::hash::{Hash, Hasher};
66
use crate::intrinsics;
77
use crate::marker::{Freeze, StructuralPartialEq};
8-
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
8+
use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign};
99
use crate::panic::{RefUnwindSafe, UnwindSafe};
1010
use crate::ptr;
1111
use crate::str::FromStr;
@@ -849,6 +849,16 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
849849
}
850850
}
851851

852+
#[stable(feature = "nonzero_div_assign", since = "CURRENT_RUSTC_VERSION")]
853+
impl DivAssign<$Ty> for $Int {
854+
/// This operation rounds towards zero,
855+
/// truncating any fractional part of the exact result, and cannot panic.
856+
#[inline]
857+
fn div_assign(&mut self, other: $Ty) {
858+
*self = *self / other;
859+
}
860+
}
861+
852862
#[stable(feature = "nonzero_div", since = "1.51.0")]
853863
impl Rem<$Ty> for $Int {
854864
type Output = $Int;
@@ -861,6 +871,15 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
861871
unsafe { intrinsics::unchecked_rem(self, other.get()) }
862872
}
863873
}
874+
875+
#[stable(feature = "nonzero_div_assign", since = "CURRENT_RUSTC_VERSION")]
876+
impl RemAssign<$Ty> for $Int {
877+
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
878+
#[inline]
879+
fn rem_assign(&mut self, other: $Ty) {
880+
*self = *self % other;
881+
}
882+
}
864883
};
865884

866885
// Impls for signed nonzero types only.

0 commit comments

Comments
 (0)