Skip to content

Commit b186f40

Browse files
committed
feat: implement {Div,Rem}Assign<NonZero<X>> on X
Signed-off-by: Petr Portnov <[email protected]>
1 parent a0c20d5 commit b186f40

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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)