-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement arithmetic assignment ops for Wrapping #30554
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,11 +157,106 @@ macro_rules! wrapping_impl { | |
Wrapping(self.0 & other.0) | ||
} | ||
} | ||
|
||
impl AddAssign for Wrapping<$t> { | ||
#[inline(always)] | ||
fn add_assign(&mut self, other: Wrapping<$t>) { | ||
*self = *self + other; | ||
} | ||
} | ||
|
||
impl SubAssign for Wrapping<$t> { | ||
#[inline(always)] | ||
fn sub_assign(&mut self, other: Wrapping<$t>) { | ||
*self = *self - other; | ||
} | ||
} | ||
|
||
impl MulAssign for Wrapping<$t> { | ||
#[inline(always)] | ||
fn mul_assign(&mut self, other: Wrapping<$t>) { | ||
*self = *self * other; | ||
} | ||
} | ||
|
||
impl DivAssign for Wrapping<$t> { | ||
#[inline(always)] | ||
fn div_assign(&mut self, other: Wrapping<$t>) { | ||
*self = *self / other; | ||
} | ||
} | ||
|
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this one disabled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT, we don't implement the remainder operator for Wrapping types. Could have been an oversight, though - I'll look more closely at why it failed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. I think we should be fine adding that impl as well - it can delegate to https://doc.rust-lang.org/stable/std/primitive.i32.html#method.wrapping_rem like the rest of the impls. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I'm planning on taking care of that soon. |
||
impl RemAssign for Wrapping<$t> { | ||
#[inline(always)] | ||
fn rem_assign(&mut self, other: Wrapping<$t>) { | ||
*self = *self % other; | ||
} | ||
} | ||
*/ | ||
|
||
impl BitAndAssign for Wrapping<$t> { | ||
#[inline(always)] | ||
fn bitand_assign(&mut self, other: Wrapping<$t>) { | ||
*self = *self & other; | ||
} | ||
} | ||
|
||
impl BitOrAssign for Wrapping<$t> { | ||
#[inline(always)] | ||
fn bitor_assign(&mut self, other: Wrapping<$t>) { | ||
*self = *self | other; | ||
} | ||
} | ||
|
||
impl BitXorAssign for Wrapping<$t> { | ||
#[inline(always)] | ||
fn bitxor_assign(&mut self, other: Wrapping<$t>) { | ||
*self = *self ^ other; | ||
} | ||
} | ||
)*) | ||
} | ||
|
||
wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 } | ||
|
||
macro_rules! wrapping_sh_impl { | ||
($($t:ty, $f:ty)*) => ($( | ||
impl ShlAssign<$f> for Wrapping<$t> { | ||
#[inline(always)] | ||
fn shl_assign(&mut self, other: $f) { | ||
*self = *self << other; | ||
} | ||
} | ||
|
||
impl ShrAssign<$f> for Wrapping<$t> { | ||
#[inline(always)] | ||
fn shr_assign(&mut self, other: $f) { | ||
*self = *self >> other; | ||
} | ||
} | ||
)*) | ||
} | ||
|
||
// FIXME (#23545): uncomment the remaining impls | ||
macro_rules! wrapping_sh_impl_all { | ||
($($t:ty)*) => ($( | ||
// wrapping_sh_impl! { $t, u8 } | ||
// wrapping_sh_impl! { $t, u16 } | ||
// wrapping_sh_impl! { $t, u32 } | ||
// wrapping_sh_impl! { $t, u64 } | ||
wrapping_sh_impl! { $t, usize } | ||
|
||
// wrapping_sh_impl! { $t, i8 } | ||
// wrapping_sh_impl! { $t, i16 } | ||
// wrapping_sh_impl! { $t, i32 } | ||
// wrapping_sh_impl! { $t, i64 } | ||
// wrapping_sh_impl! { $t, isize } | ||
)*) | ||
} | ||
|
||
wrapping_sh_impl_all! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 } | ||
|
||
mod shift_max { | ||
#![allow(non_upper_case_globals)] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll want to slap
#[inline(always)]
on these methods.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks.