Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement {Div,Rem}Assign<NonZero<X>> on X #121952

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
workingjubilee marked this conversation as resolved.
Show resolved Hide resolved
*self = *self / other;
}
}

#[stable(feature = "nonzero_div", since = "1.51.0")]
impl Rem<$Ty> for $Int {
type Output = $Int;
Expand All @@ -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) {
workingjubilee marked this conversation as resolved.
Show resolved Hide resolved
*self = *self % other;
}
}
};

// Impls for signed nonzero types only.
Expand Down
Loading