@@ -21,24 +21,49 @@ impl Mul<Sign> for Sign {
2121 }
2222}
2323
24- forward_all_binop_to_ref_ref ! ( impl Mul for BigInt , mul) ;
25-
26- impl < ' a , ' b > Mul < & ' b BigInt > for & ' a BigInt {
27- type Output = BigInt ;
28-
29- #[ inline]
30- fn mul ( self , other : & BigInt ) -> BigInt {
31- BigInt :: from_biguint ( self . sign * other. sign , & self . data * & other. data )
32- }
24+ macro_rules! impl_mul {
25+ ( $( impl <$( $a: lifetime) ,* > Mul <$Other: ty> for $Self: ty; ) * ) => { $(
26+ impl <$( $a) ,* > Mul <$Other> for $Self {
27+ type Output = BigInt ;
28+
29+ #[ inline]
30+ fn mul( self , other: $Other) -> BigInt {
31+ // automatically match value/ref
32+ let BigInt { data: x, .. } = self ;
33+ let BigInt { data: y, .. } = other;
34+ BigInt :: from_biguint( self . sign * other. sign, x * y)
35+ }
36+ }
37+ ) * }
38+ }
39+ impl_mul ! {
40+ impl <> Mul <BigInt > for BigInt ;
41+ impl <' b> Mul <& ' b BigInt > for BigInt ;
42+ impl <' a> Mul <BigInt > for & ' a BigInt ;
43+ impl <' a, ' b> Mul <& ' b BigInt > for & ' a BigInt ;
44+ }
45+
46+ macro_rules! impl_mul_assign {
47+ ( $( impl <$( $a: lifetime) ,* > MulAssign <$Other: ty> for BigInt ; ) * ) => { $(
48+ impl <$( $a) ,* > MulAssign <$Other> for BigInt {
49+ #[ inline]
50+ fn mul_assign( & mut self , other: $Other) {
51+ // automatically match value/ref
52+ let BigInt { data: y, .. } = other;
53+ self . data *= y;
54+ if self . data. is_zero( ) {
55+ self . sign = NoSign ;
56+ } else {
57+ self . sign = self . sign * other. sign;
58+ }
59+ }
60+ }
61+ ) * }
3362}
34-
35- impl < ' a > MulAssign < & ' a BigInt > for BigInt {
36- #[ inline]
37- fn mul_assign ( & mut self , other : & BigInt ) {
38- * self = & * self * other;
39- }
63+ impl_mul_assign ! {
64+ impl <> MulAssign <BigInt > for BigInt ;
65+ impl <' a> MulAssign <& ' a BigInt > for BigInt ;
4066}
41- forward_val_assign ! ( impl MulAssign for BigInt , mul_assign) ;
4267
4368promote_all_scalars ! ( impl Mul for BigInt , mul) ;
4469promote_all_scalars_assign ! ( impl MulAssign for BigInt , mul_assign) ;
0 commit comments