From f423fe0159640c4faf63d002f620045b88d0179c Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Tue, 20 Aug 2019 17:07:13 +0200 Subject: [PATCH] impl {Shl,Shr}{,Assign} for Wrapping --- src/wrapping.rs | 53 +++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/wrapping.rs b/src/wrapping.rs index ae5d1ba..3918a07 100644 --- a/src/wrapping.rs +++ b/src/wrapping.rs @@ -361,78 +361,81 @@ macro_rules! op_bitwise { } macro_rules! op_shift { - ($Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => { - impl $Op for Wrapping + ( + $Op:ident $op:ident, $OpAssign:ident $op_assign:ident; + $($Rhs:ident),* + ) => { $( + impl $Op<$Rhs> for Wrapping where F: $Op, { type Output = Wrapping; #[allow(clippy::suspicious_op_assign_impl)] #[inline] - fn $op(self, other: u32) -> Wrapping { + fn $op(self, other: $Rhs) -> Wrapping { let nbits = mem::size_of::() as u32 * 8; - Wrapping((self.0).$op(other % nbits)) + Wrapping((self.0).$op(other as u32 % nbits)) } } - impl<'a, F> $Op for &'a Wrapping + impl<'a, F> $Op<$Rhs> for &'a Wrapping where &'a F: $Op, { type Output = Wrapping; #[allow(clippy::suspicious_op_assign_impl)] #[inline] - fn $op(self, other: u32) -> Wrapping { + fn $op(self, other: $Rhs) -> Wrapping { let nbits = mem::size_of::() as u32 * 8; - Wrapping((self.0).$op(other % nbits)) + Wrapping((self.0).$op(other as u32 % nbits)) } } - impl<'a, F> $Op<&'a u32> for Wrapping + impl<'a, F> $Op<&'a $Rhs> for Wrapping where F: $Op, { type Output = Wrapping; #[allow(clippy::suspicious_op_assign_impl)] #[inline] - fn $op(self, other: &u32) -> Wrapping { + fn $op(self, other: &$Rhs) -> Wrapping { let nbits = mem::size_of::() as u32 * 8; - Wrapping((self.0).$op(*other % nbits)) + Wrapping((self.0).$op(*other as u32 % nbits)) } } - impl<'a, 'b, F> $Op<&'a u32> for &'b Wrapping + impl<'a, 'b, F> $Op<&'a $Rhs> for &'b Wrapping where &'b F: $Op, { type Output = Wrapping; #[allow(clippy::suspicious_op_assign_impl)] #[inline] - fn $op(self, other: &u32) -> Wrapping { + fn $op(self, other: &$Rhs) -> Wrapping { let nbits = mem::size_of::() as u32 * 8; - Wrapping((self.0).$op(*other % nbits)) + Wrapping((self.0).$op(*other as u32 % nbits)) } } - impl $OpAssign for Wrapping + impl $OpAssign<$Rhs> for Wrapping where F: $OpAssign, { #[allow(clippy::suspicious_op_assign_impl)] #[inline] - fn $op_assign(&mut self, other: u32) { + fn $op_assign(&mut self, other: $Rhs) { let nbits = mem::size_of::() as u32 * 8; - (self.0).$op_assign(other % nbits); + (self.0).$op_assign(other as u32 % nbits); } } - impl<'a, F> $OpAssign<&'a u32> for Wrapping + impl<'a, F> $OpAssign<&'a $Rhs> for Wrapping where F: $OpAssign, { #[allow(clippy::suspicious_op_assign_impl)] #[inline] - fn $op_assign(&mut self, other: &u32) { + fn $op_assign(&mut self, other: &$Rhs) { let nbits = mem::size_of::() as u32 * 8; - (self.0).$op_assign(*other % nbits); + (self.0).$op_assign(*other as u32 % nbits); } } - }; + )* }; } impl Neg for Wrapping { @@ -479,8 +482,14 @@ op_bitwise! { BitAnd bitand, BitAndAssign bitand_assign } op_bitwise! { BitOr bitor, BitOrAssign bitor_assign } op_bitwise! { BitXor bitxor, BitXorAssign bitxor_assign } -op_shift! { Shl shl, ShlAssign shl_assign } -op_shift! { Shr shr, ShrAssign shr_assign } +op_shift! { + Shl shl, ShlAssign shl_assign; + i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize +} +op_shift! { + Shr shr, ShrAssign shr_assign; + i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize +} impl Sum> for Wrapping { fn sum(iter: I) -> Wrapping