瀏覽代碼

Forward by-value biguint and scalars to {Add,Mul,Sub,Div}Assign

Alice Ryhl 7 年之前
父節點
當前提交
e85ab24567
共有 2 個文件被更改,包括 39 次插入2 次删除
  1. 8 1
      bigint/src/biguint.rs
  2. 31 1
      bigint/src/macros.rs

+ 8 - 1
bigint/src/biguint.rs

@@ -377,6 +377,7 @@ impl One for BigUint {
 impl Unsigned for BigUint {}
 
 forward_all_binop_to_val_ref_commutative!(impl Add for BigUint, add);
+forward_val_assign!(impl AddAssign for BigUint, add_assign);
 
 impl<'a> Add<&'a BigUint> for BigUint {
     type Output = BigUint;
@@ -386,7 +387,6 @@ impl<'a> Add<&'a BigUint> for BigUint {
         self
     }
 }
-
 impl<'a> AddAssign<&'a BigUint> for BigUint {
     #[inline]
     fn add_assign(&mut self, other: &BigUint) {
@@ -403,6 +403,7 @@ impl<'a> AddAssign<&'a BigUint> for BigUint {
 }
 
 promote_unsigned_scalars!(impl Add for BigUint, add);
+promote_unsigned_scalars_assign!(impl AddAssign for BigUint, add_assign);
 forward_all_scalar_binop_to_val_val_commutative!(impl Add<BigDigit> for BigUint, add);
 forward_all_scalar_binop_to_val_val_commutative!(impl Add<DoubleBigDigit> for BigUint, add);
 
@@ -461,6 +462,7 @@ impl AddAssign<DoubleBigDigit> for BigUint {
 
 forward_val_val_binop!(impl Sub for BigUint, sub);
 forward_ref_ref_binop!(impl Sub for BigUint, sub);
+forward_val_assign!(impl SubAssign for BigUint, sub_assign);
 
 impl<'a> Sub<&'a BigUint> for BigUint {
     type Output = BigUint;
@@ -492,6 +494,7 @@ impl<'a> Sub<BigUint> for &'a BigUint {
 }
 
 promote_unsigned_scalars!(impl Sub for BigUint, sub);
+promote_unsigned_scalars_assign!(impl SubAssign for BigUint, sub_assign);
 forward_all_scalar_binop_to_val_val!(impl Sub<BigDigit> for BigUint, sub);
 forward_all_scalar_binop_to_val_val!(impl Sub<DoubleBigDigit> for BigUint, sub);
 
@@ -558,6 +561,7 @@ impl Sub<BigUint> for DoubleBigDigit {
 }
 
 forward_all_binop_to_ref_ref!(impl Mul for BigUint, mul);
+forward_val_assign!(impl MulAssign for BigUint, mul_assign);
 
 impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint {
     type Output = BigUint;
@@ -575,6 +579,7 @@ impl<'a> MulAssign<&'a BigUint> for BigUint {
 }
 
 promote_unsigned_scalars!(impl Mul for BigUint, mul);
+promote_unsigned_scalars_assign!(impl MulAssign for BigUint, mul_assign);
 forward_all_scalar_binop_to_val_val_commutative!(impl Mul<BigDigit> for BigUint, mul);
 forward_all_scalar_binop_to_val_val_commutative!(impl Mul<DoubleBigDigit> for BigUint, mul);
 
@@ -625,6 +630,7 @@ impl MulAssign<DoubleBigDigit> for BigUint {
 }
 
 forward_all_binop_to_ref_ref!(impl Div for BigUint, div);
+forward_val_assign!(impl DivAssign for BigUint, div_assign);
 
 impl<'a, 'b> Div<&'b BigUint> for &'a BigUint {
     type Output = BigUint;
@@ -643,6 +649,7 @@ impl<'a> DivAssign<&'a BigUint> for BigUint {
 }
 
 promote_unsigned_scalars!(impl Div for BigUint, div);
+promote_unsigned_scalars_assign!(impl DivAssign for BigUint, div_assign);
 forward_all_scalar_binop_to_val_val!(impl Div<BigDigit> for BigUint, div);
 forward_all_scalar_binop_to_val_val!(impl Div<DoubleBigDigit> for BigUint, div);
 

+ 31 - 1
bigint/src/macros.rs

@@ -105,6 +105,17 @@ macro_rules! forward_ref_ref_binop_commutative {
     }
 }
 
+macro_rules! forward_val_assign {
+    (impl $imp:ident for $res:ty, $method:ident) => {
+        impl<'a> $imp<$res> for $res {
+            #[inline]
+            fn $method(&mut self, other: $res) {
+                self.$method(&other);
+            }
+        }
+    }
+}
+
 macro_rules! forward_scalar_val_val_binop_commutative {
     (impl $imp:ident<$scalar:ty> for $res:ty, $method: ident) => {
         impl $imp<$res> for $scalar {
@@ -209,6 +220,18 @@ macro_rules! promote_scalars {
         )*
     }
 }
+macro_rules! promote_scalars_assign {
+    (impl $imp:ident<$promo:ty> for $res:ty, $method:ident, $( $scalar:ty ),*) => {
+        $(
+            impl $imp<$scalar> for $res {
+                #[inline]
+                fn $method(&mut self, other: $scalar) {
+                    self.$method(other as $promo);
+                }
+            }
+        )*
+    }
+}
 
 macro_rules! promote_unsigned_scalars {
     (impl $imp:ident for $res:ty, $method:ident) => {
@@ -217,6 +240,13 @@ macro_rules! promote_unsigned_scalars {
     }
 }
 
+macro_rules! promote_unsigned_scalars_assign {
+    (impl $imp:ident for $res:ty, $method:ident) => {
+        promote_scalars_assign!(impl $imp<u32> for $res, $method, u8, u16);
+        promote_scalars_assign!(impl $imp<UsizePromotion> for $res, $method, usize);
+    }
+}
+
 macro_rules! promote_signed_scalars {
     (impl $imp:ident for $res:ty, $method:ident) => {
         promote_scalars!(impl $imp<i32> for $res, $method, i8, i16);
@@ -271,4 +301,4 @@ macro_rules! promote_all_scalars {
         promote_unsigned_scalars!(impl $imp for $res, $method);
         promote_signed_scalars!(impl $imp for $res, $method);
     }
-}
+}