From b186f40b8bdbe94682a70290973620ca2b462bc5 Mon Sep 17 00:00:00 2001 From: Petr Portnov Date: Sun, 3 Mar 2024 23:56:38 +0300 Subject: [PATCH 1/3] feat: implement `{Div,Rem}Assign>` on `X` Signed-off-by: Petr Portnov --- library/core/src/num/nonzero.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 1f7a4e276f553..a8f637280df67 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. From 5ebed0ba4b057ade343a9390c39490c599c41d86 Mon Sep 17 00:00:00 2001 From: Petr Portnov Date: Mon, 4 Mar 2024 00:08:25 +0300 Subject: [PATCH 2/3] chore(121952): remove redundant comments These were only relevant for the unsafe-containing implementations Signed-off-by: Petr Portnov --- library/core/src/num/nonzero.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index a8f637280df67..d025cea6cb9d6 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -851,8 +851,6 @@ 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; @@ -874,7 +872,6 @@ macro_rules! nonzero_integer_signedness_dependent_impls { #[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; From e7d397024f6eebdd36bfa31ea8cf56496d348f7f Mon Sep 17 00:00:00 2001 From: Petr Portnov Date: Sun, 17 Mar 2024 17:06:12 +0300 Subject: [PATCH 3/3] chore(121952): echo comments on the `*_assign` methods --- library/core/src/num/nonzero.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index d025cea6cb9d6..a8f637280df67 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -851,6 +851,8 @@ 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; @@ -872,6 +874,7 @@ macro_rules! nonzero_integer_signedness_dependent_impls { #[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;