Skip to content

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

Closed
wants to merge 3 commits into from
Closed
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
95 changes: 95 additions & 0 deletions src/libcore/num/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,106 @@ macro_rules! wrapping_impl {
Wrapping(self.0 & other.0)
}
}

impl AddAssign for Wrapping<$t> {
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks.

#[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;
}
}

/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this one disabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)]

Expand Down