diff --git a/gstreamer/src/value.rs b/gstreamer/src/value.rs index 9e025f53b..349ee295f 100644 --- a/gstreamer/src/value.rs +++ b/gstreamer/src/value.rs @@ -66,85 +66,135 @@ impl AsRef for Fraction { } } -impl ops::Mul for Fraction { - type Output = Fraction; +macro_rules! impl_fraction_binop { + ($name:ident, $f:ident, $name_assign:ident, $f_assign:ident) => { + impl ops::$name for Fraction { + type Output = Fraction; - fn mul(self, other: Fraction) -> Fraction { - Fraction(self.0.mul(other.0)) - } + fn $f(self, other: Fraction) -> Fraction { + Fraction((self.0).$f(other.0)) + } + } + + impl ops::$name for &Fraction { + type Output = Fraction; + + fn $f(self, other: Fraction) -> Fraction { + Fraction((self.0).$f(other.0)) + } + } + + impl ops::$name<&Fraction> for Fraction { + type Output = Fraction; + + fn $f(self, other: &Fraction) -> Fraction { + Fraction((self.0).$f(other.0)) + } + } + + impl ops::$name<&Fraction> for &Fraction { + type Output = Fraction; + + fn $f(self, other: &Fraction) -> Fraction { + Fraction((self.0).$f(other.0)) + } + } + + impl ops::$name for Fraction { + type Output = Fraction; + + fn $f(self, other: i32) -> Fraction { + self.$f(Fraction::from(other)) + } + } + + impl ops::$name for &Fraction { + type Output = Fraction; + + fn $f(self, other: i32) -> Fraction { + self.$f(Fraction::from(other)) + } + } + + impl ops::$name<&i32> for Fraction { + type Output = Fraction; + + fn $f(self, other: &i32) -> Fraction { + self.$f(Fraction::from(*other)) + } + } + + impl ops::$name<&i32> for &Fraction { + type Output = Fraction; + + fn $f(self, other: &i32) -> Fraction { + self.$f(Fraction::from(*other)) + } + } + + impl ops::$name for i32 { + type Output = Fraction; + + fn $f(self, other: Fraction) -> Fraction { + Fraction::from(self).$f(other) + } + } + + impl ops::$name<&Fraction> for i32 { + type Output = Fraction; + + fn $f(self, other: &Fraction) -> Fraction { + Fraction::from(self).$f(other) + } + } + + impl ops::$name for &i32 { + type Output = Fraction; + + fn $f(self, other: Fraction) -> Fraction { + Fraction::from(*self).$f(other) + } + } + + impl ops::$name<&Fraction> for &i32 { + type Output = Fraction; + + fn $f(self, other: &Fraction) -> Fraction { + Fraction::from(*self).$f(other) + } + } + + impl ops::$name_assign for Fraction { + fn $f_assign(&mut self, other: Fraction) { + (self.0).$f_assign(other.0) + } + } + + impl ops::$name_assign<&Fraction> for Fraction { + fn $f_assign(&mut self, other: &Fraction) { + (self.0).$f_assign(other.0) + } + } + + impl ops::$name_assign for Fraction { + fn $f_assign(&mut self, other: i32) { + (self.0).$f_assign(other) + } + } + + impl ops::$name_assign<&i32> for Fraction { + fn $f_assign(&mut self, other: &i32) { + (self.0).$f_assign(other) + } + } + }; } -impl ops::Mul for Fraction { - type Output = Fraction; - - fn mul(self, other: i32) -> Fraction { - self.mul(Fraction::from(other)) - } -} - -impl ops::Div for Fraction { - type Output = Fraction; - - fn div(self, other: Fraction) -> Fraction { - Fraction(self.0.div(other.0)) - } -} - -impl ops::Div for Fraction { - type Output = Fraction; - - fn div(self, other: i32) -> Fraction { - self.div(Fraction::from(other)) - } -} - -impl ops::Add for Fraction { - type Output = Fraction; - - fn add(self, other: Fraction) -> Fraction { - Fraction(self.0.add(other.0)) - } -} - -impl ops::Add for Fraction { - type Output = Fraction; - - fn add(self, other: i32) -> Fraction { - self.add(Fraction::from(other)) - } -} - -impl ops::Sub for Fraction { - type Output = Fraction; - - fn sub(self, other: Fraction) -> Fraction { - Fraction(self.0.sub(other.0)) - } -} - -impl ops::Sub for Fraction { - type Output = Fraction; - - fn sub(self, other: i32) -> Fraction { - self.sub(Fraction::from(other)) - } -} - -impl ops::Rem for Fraction { - type Output = Fraction; - - fn rem(self, other: Fraction) -> Fraction { - Fraction(self.0.rem(other.0)) - } -} - -impl ops::Rem for Fraction { - type Output = Fraction; - - fn rem(self, other: i32) -> Fraction { - self.rem(Fraction::from(other)) - } -} +impl_fraction_binop!(Add, add, AddAssign, add_assign); +impl_fraction_binop!(Sub, sub, SubAssign, sub_assign); +impl_fraction_binop!(Div, div, DivAssign, div_assign); +impl_fraction_binop!(Mul, mul, MulAssign, mul_assign); +impl_fraction_binop!(Rem, rem, RemAssign, rem_assign); impl ops::Neg for Fraction { type Output = Fraction; @@ -154,6 +204,14 @@ impl ops::Neg for Fraction { } } +impl ops::Neg for &Fraction { + type Output = Fraction; + + fn neg(self) -> Fraction { + Fraction(self.0.neg()) + } +} + impl From for Fraction { fn from(x: i32) -> Fraction { assert_initialized_main_thread!(); @@ -842,3 +900,20 @@ impl GstValueExt for glib::Value { } } } + +#[cfg(test)] +mod tests { + #[test] + fn test_fraction() { + ::init().unwrap(); + + let f1 = ::Fraction::new(1, 2); + let f2 = ::Fraction::new(2, 3); + let mut f3 = f1 * f2; + let f4 = f1 * &f2; + f3 *= f2; + f3 *= &f4; + + assert_eq!(f3, ::Fraction::new(2, 27)); + } +}