diff --git a/complex/src/lib.rs b/complex/src/lib.rs index cb605a2..9ab4ef9 100644 --- a/complex/src/lib.rs +++ b/complex/src/lib.rs @@ -516,6 +516,88 @@ impl Div> for Complex { } } +// Op Assign + +mod opassign { + use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign}; + + use traits::Num; + + use Complex; + + impl AddAssign for Complex { + fn add_assign(&mut self, other: Complex) { + *self = self.clone() + other; + } + } + + impl SubAssign for Complex { + fn sub_assign(&mut self, other: Complex) { + *self = self.clone() - other; + } + } + + impl MulAssign for Complex { + fn mul_assign(&mut self, other: Complex) { + *self = self.clone() * other; + } + } + + impl DivAssign for Complex { + fn div_assign(&mut self, other: Complex) { + *self = self.clone() / other; + } + } + + impl AddAssign for Complex { + fn add_assign(&mut self, other: T) { + self.re += other; + } + } + + impl SubAssign for Complex { + fn sub_assign(&mut self, other: T) { + self.re -= other; + } + } + + impl MulAssign for Complex { + fn mul_assign(&mut self, other: T) { + self.re *= other.clone(); + self.im *= other; + } + } + + impl DivAssign for Complex { + fn div_assign(&mut self, other: T) { + self.re /= other.clone(); + self.im /= other; + } + } + + macro_rules! forward_op_assign { + (impl $imp:ident, $method:ident) => { + impl<'a, T: Clone + Num> $imp<&'a Complex> for Complex { + #[inline] + fn $method(&mut self, other: &Complex) { + self.$method(other.clone()) + } + } + impl<'a, T: Clone + Num + $imp> $imp<&'a T> for Complex { + #[inline] + fn $method(&mut self, other: &T) { + self.$method(other.clone()) + } + } + } + } + + forward_op_assign!(impl AddAssign, add_assign); + forward_op_assign!(impl SubAssign, sub_assign); + forward_op_assign!(impl MulAssign, mul_assign); + forward_op_assign!(impl DivAssign, div_assign); +} + impl> Neg for Complex { type Output = Complex;