Skip to content

Commit 5656fa8

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

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::StructuralPartialEq;
8-
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
8+
use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign};
99
use crate::str::FromStr;
1010

1111
use super::from_str_radix;
@@ -800,6 +800,16 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
800800
}
801801
}
802802

803+
#[stable(feature = "nonzero_div_assign", since = "CURRENT_RUSTC_VERSION")]
804+
impl DivAssign<$Ty> for $Int {
805+
/// This operation rounds towards zero,
806+
/// truncating any fractional part of the exact result, and cannot panic.
807+
#[inline]
808+
fn div_assign(&mut self, other: $Ty) {
809+
*self = *self / other;
810+
}
811+
}
812+
803813
#[stable(feature = "nonzero_div", since = "1.51.0")]
804814
impl Rem<$Ty> for $Int {
805815
type Output = $Int;
@@ -812,6 +822,15 @@ macro_rules! nonzero_integer_signedness_dependent_impls {
812822
unsafe { intrinsics::unchecked_rem(self, other.get()) }
813823
}
814824
}
825+
826+
#[stable(feature = "nonzero_div_assign", since = "CURRENT_RUSTC_VERSION")]
827+
impl RemAssign<$Ty> for $Int {
828+
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
829+
#[inline]
830+
fn rem_assign(&mut self, other: $Ty) {
831+
*self = *self % other;
832+
}
833+
}
815834
};
816835

817836
// Impls for signed nonzero types only.

0 commit comments

Comments
 (0)