From 1c7c8899214973e400c360e829ed0b77bfe0c0f6 Mon Sep 17 00:00:00 2001 From: Norbert Nemec Date: Sun, 1 Sep 2019 20:24:03 +0200 Subject: [PATCH] drop redundancy of impl_operator_default and impl_assignment_operator_default --- src/macros.rs | 154 +++++++--------------------------------------- src/quaternion.rs | 20 +++--- src/vector.rs | 22 +++---- 3 files changed, 42 insertions(+), 154 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index d76622c..8f9ba5d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -36,17 +36,17 @@ macro_rules! impl_operator { impl<$S: $Constraint> $Op for $Lhs { type Output = $Output; #[inline] - fn $op(self) -> $Output { + default_fn!($op(self) -> $Output { let $x = self; $body - } + }); } impl<'a, $S: $Constraint> $Op for &'a $Lhs { type Output = $Output; #[inline] - fn $op(self) -> $Output { + default_fn!($op(self) -> $Output { let $x = self; $body - } + }); } }; // When the right operand is a scalar @@ -56,17 +56,17 @@ macro_rules! impl_operator { impl<$S: $Constraint> $Op<$Rhs> for $Lhs { type Output = $Output; #[inline] - fn $op(self, other: $Rhs) -> $Output { + default_fn!($op(self, other: $Rhs) -> $Output { let ($lhs, $rhs) = (self, other); $body - } + }); } impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs { type Output = $Output; #[inline] - fn $op(self, other: $Rhs) -> $Output { + default_fn!($op(self, other: $Rhs) -> $Output { let ($lhs, $rhs) = (self, other); $body - } + }); } }; // When the right operand is a compound type @@ -76,33 +76,33 @@ macro_rules! impl_operator { impl<$S: $Constraint> $Op<$Rhs> for $Lhs { type Output = $Output; #[inline] - fn $op(self, other: $Rhs) -> $Output { + default_fn!( $op(self, other: $Rhs) -> $Output { let ($lhs, $rhs) = (self, other); $body - } + }); } impl<'a, $S: $Constraint> $Op<&'a $Rhs> for $Lhs { type Output = $Output; #[inline] - fn $op(self, other: &'a $Rhs) -> $Output { + default_fn!( $op(self, other: &'a $Rhs) -> $Output { let ($lhs, $rhs) = (self, other); $body - } + }); } impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs { type Output = $Output; #[inline] - fn $op(self, other: $Rhs) -> $Output { + default_fn!( $op(self, other: $Rhs) -> $Output { let ($lhs, $rhs) = (self, other); $body - } + }); } impl<'a, 'b, $S: $Constraint> $Op<&'a $Rhs> for &'b $Lhs { type Output = $Output; #[inline] - fn $op(self, other: &'a $Rhs) -> $Output { + default_fn!( $op(self, other: &'a $Rhs) -> $Output { let ($lhs, $rhs) = (self, other); $body - } + }); } }; // When the left operand is a scalar @@ -112,17 +112,17 @@ macro_rules! impl_operator { impl $Op<$Rhs<$S>> for $Lhs { type Output = $Output; #[inline] - fn $op(self, other: $Rhs<$S>) -> $Output { + default_fn!( $op(self, other: $Rhs<$S>) -> $Output { let ($lhs, $rhs) = (self, other); $body - } + }); } impl<'a> $Op<&'a $Rhs<$S>> for $Lhs { type Output = $Output; #[inline] - fn $op(self, other: &'a $Rhs<$S>) -> $Output { + default_fn!( $op(self, other: &'a $Rhs<$S>) -> $Output { let ($lhs, $rhs) = (self, other); $body - } + }); } }; } @@ -133,7 +133,7 @@ macro_rules! impl_assignment_operator { }) => { impl<$S: $Constraint + $Op<$S>> $Op<$Rhs> for $Lhs { #[inline] - fn $op(&mut $lhs, $rhs: $Rhs) $body + default_fn!( $op(&mut $lhs, $rhs: $Rhs) $body ); } }; } @@ -265,118 +265,6 @@ macro_rules! impl_index_operators { } } -#[cfg(feature = "simd")] -macro_rules! impl_operator_default { - // When it is an unary operator - (<$S:ident: $Constraint:ident> $Op:ident for $Lhs:ty { - fn $op:ident($x:ident) -> $Output:ty { $body:expr } - }) => { - impl<$S: $Constraint> $Op for $Lhs { - type Output = $Output; - #[inline] - default fn $op(self) -> $Output { - let $x = self; $body - } - } - - impl<'a, $S: $Constraint> $Op for &'a $Lhs { - type Output = $Output; - #[inline] - default fn $op(self) -> $Output { - let $x = self; $body - } - } - }; - // When the right operand is a scalar - (<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ident> for $Lhs:ty { - fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr } - }) => { - impl<$S: $Constraint> $Op<$Rhs> for $Lhs { - type Output = $Output; - #[inline] - default fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs) = (self, other); $body - } - } - - impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs { - type Output = $Output; - #[inline] - default fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs) = (self, other); $body - } - } - }; - // When the right operand is a compound type - (<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ty> for $Lhs:ty { - fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr } - }) => { - impl<$S: $Constraint> $Op<$Rhs> for $Lhs { - type Output = $Output; - #[inline] - default fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs) = (self, other); $body - } - } - - impl<'a, $S: $Constraint> $Op<&'a $Rhs> for $Lhs { - type Output = $Output; - #[inline] - default fn $op(self, other: &'a $Rhs) -> $Output { - let ($lhs, $rhs) = (self, other); $body - } - } - - impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs { - type Output = $Output; - #[inline] - default fn $op(self, other: $Rhs) -> $Output { - let ($lhs, $rhs) = (self, other); $body - } - } - - impl<'a, 'b, $S: $Constraint> $Op<&'a $Rhs> for &'b $Lhs { - type Output = $Output; - #[inline] - default fn $op(self, other: &'a $Rhs) -> $Output { - let ($lhs, $rhs) = (self, other); $body - } - } - }; - // When the left operand is a scalar - ($Op:ident<$Rhs:ident<$S:ident>> for $Lhs:ty { - fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr } - }) => { - impl $Op<$Rhs<$S>> for $Lhs { - type Output = $Output; - #[inline] - default fn $op(self, other: $Rhs<$S>) -> $Output { - let ($lhs, $rhs) = (self, other); $body - } - } - - impl<'a> $Op<&'a $Rhs<$S>> for $Lhs { - type Output = $Output; - #[inline] - default fn $op(self, other: &'a $Rhs<$S>) -> $Output { - let ($lhs, $rhs) = (self, other); $body - } - } - }; -} - -#[cfg(feature = "simd")] -macro_rules! impl_assignment_operator_default { - (<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ty> for $Lhs:ty { - fn $op:ident(&mut $lhs:ident, $rhs:ident) $body:block - }) => { - impl<$S: $Constraint + $Op<$S>> $Op<$Rhs> for $Lhs { - #[inline] - default fn $op(&mut $lhs, $rhs: $Rhs) $body - } - }; -} - /// Generates a binary operator implementation for the permutations of by-ref and by-val, for simd #[cfg(feature = "simd")] macro_rules! impl_operator_simd { diff --git a/src/quaternion.rs b/src/quaternion.rs index 3bd6b42..b54149d 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -319,7 +319,7 @@ impl_operator!( Neg for Quaternion { }); #[cfg(feature = "simd")] -impl_operator_default!( Neg for Quaternion { +impl_operator!( Neg for Quaternion { fn neg(quat) -> Quaternion { Quaternion::from_sv(-quat.s, -quat.v) } @@ -342,7 +342,7 @@ impl_operator!( Mul for Quaternion { }); #[cfg(feature = "simd")] -impl_operator_default!( Mul for Quaternion { +impl_operator!( Mul for Quaternion { fn mul(lhs, rhs) -> Quaternion { Quaternion::from_sv(lhs.s * rhs, lhs.v * rhs) } @@ -363,7 +363,7 @@ impl_assignment_operator!( MulAssign for Quaternion { }); #[cfg(feature = "simd")] -impl_assignment_operator_default!( MulAssign for Quaternion { +impl_assignment_operator!( MulAssign for Quaternion { fn mul_assign(&mut self, scalar) { self.s *= scalar; self.v *= scalar; } }); @@ -384,7 +384,7 @@ impl_operator!( Div for Quaternion { }); #[cfg(feature = "simd")] -impl_operator_default!( Div for Quaternion { +impl_operator!( Div for Quaternion { fn div(lhs, rhs) -> Quaternion { Quaternion::from_sv(lhs.s / rhs, lhs.v / rhs) } @@ -405,7 +405,7 @@ impl_assignment_operator!( DivAssign for Quaternion { }); #[cfg(feature = "simd")] -impl_assignment_operator_default!( DivAssign for Quaternion { +impl_assignment_operator!( DivAssign for Quaternion { fn div_assign(&mut self, scalar) { self.s /= scalar; self.v /= scalar; } }); @@ -445,7 +445,7 @@ impl_operator!( Add > for Quaternion { }); #[cfg(feature = "simd")] -impl_operator_default!( Add > for Quaternion { +impl_operator!( Add > for Quaternion { fn add(lhs, rhs) -> Quaternion { Quaternion::from_sv(lhs.s + rhs.s, lhs.v + rhs.v) } @@ -466,7 +466,7 @@ impl_assignment_operator!( AddAssign > for Quaternio }); #[cfg(feature = "simd")] -impl_assignment_operator_default!( AddAssign > for Quaternion { +impl_assignment_operator!( AddAssign > for Quaternion { fn add_assign(&mut self, other) { self.s += other.s; self.v += other.v; } }); @@ -488,7 +488,7 @@ impl_operator!( Sub > for Quaternion { }); #[cfg(feature = "simd")] -impl_operator_default!( Sub > for Quaternion { +impl_operator!( Sub > for Quaternion { fn sub(lhs, rhs) -> Quaternion { Quaternion::from_sv(lhs.s - rhs.s, lhs.v - rhs.v) } @@ -509,7 +509,7 @@ impl_assignment_operator!( SubAssign > for Quaternio }); #[cfg(feature = "simd")] -impl_assignment_operator_default!( SubAssign > for Quaternion { +impl_assignment_operator!( SubAssign > for Quaternion { fn sub_assign(&mut self, other) { self.s -= other.s; self.v -= other.v; } }); @@ -536,7 +536,7 @@ impl_operator!( Mul > for Quaternion { }); #[cfg(feature = "simd")] -impl_operator_default!( Mul > for Quaternion { +impl_operator!( Mul > for Quaternion { fn mul(lhs, rhs) -> Quaternion { Quaternion::new( lhs.s * rhs.s - lhs.v.x * rhs.v.x - lhs.v.y * rhs.v.y - lhs.v.z * rhs.v.z, diff --git a/src/vector.rs b/src/vector.rs index 9d35811..e456e78 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -526,35 +526,35 @@ macro_rules! impl_vector_default { } } - impl_operator_default!( Add<$VectorN > for $VectorN { + impl_operator!( Add<$VectorN > for $VectorN { fn add(lhs, rhs) -> $VectorN { $VectorN::new($(lhs.$field + rhs.$field),+) } }); - impl_assignment_operator_default!( AddAssign<$VectorN > for $VectorN { + impl_assignment_operator!( AddAssign<$VectorN > for $VectorN { fn add_assign(&mut self, other) { $(self.$field += other.$field);+ } }); - impl_operator_default!( Sub<$VectorN > for $VectorN { + impl_operator!( Sub<$VectorN > for $VectorN { fn sub(lhs, rhs) -> $VectorN { $VectorN::new($(lhs.$field - rhs.$field),+) } }); - impl_assignment_operator_default!( SubAssign<$VectorN > for $VectorN { + impl_assignment_operator!( SubAssign<$VectorN > for $VectorN { fn sub_assign(&mut self, other) { $(self.$field -= other.$field);+ } }); - impl_operator_default!( Mul for $VectorN { + impl_operator!( Mul for $VectorN { fn mul(vector, scalar) -> $VectorN { $VectorN::new($(vector.$field * scalar),+) } }); - impl_assignment_operator_default!( MulAssign for $VectorN { + impl_assignment_operator!( MulAssign for $VectorN { fn mul_assign(&mut self, scalar) { $(self.$field *= scalar);+ } }); - impl_operator_default!( Div for $VectorN { + impl_operator!( Div for $VectorN { fn div(vector, scalar) -> $VectorN { $VectorN::new($(vector.$field / scalar),+) } }); - impl_assignment_operator_default!( DivAssign for $VectorN { + impl_assignment_operator!( DivAssign for $VectorN { fn div_assign(&mut self, scalar) { $(self.$field /= scalar);+ } }); @@ -631,13 +631,13 @@ macro_rules! impl_scalar_ops { #[cfg(feature = "simd")] macro_rules! impl_scalar_ops_default { ($VectorN:ident<$S:ident> { $($field:ident),+ }) => { - impl_operator_default!(Mul<$VectorN<$S>> for $S { + impl_operator!(Mul<$VectorN<$S>> for $S { fn mul(scalar, vector) -> $VectorN<$S> { $VectorN::new($(scalar * vector.$field),+) } }); - impl_operator_default!(Div<$VectorN<$S>> for $S { + impl_operator!(Div<$VectorN<$S>> for $S { fn div(scalar, vector) -> $VectorN<$S> { $VectorN::new($(scalar / vector.$field),+) } }); - impl_operator_default!(Rem<$VectorN<$S>> for $S { + impl_operator!(Rem<$VectorN<$S>> for $S { fn rem(scalar, vector) -> $VectorN<$S> { $VectorN::new($(scalar % vector.$field),+) } }); };