diff --git a/src/cgmath/traits/alg/coordinate.rs b/src/cgmath/traits/alg/coordinate.rs index fc14d2e..f5c28c7 100644 --- a/src/cgmath/traits/alg/coordinate.rs +++ b/src/cgmath/traits/alg/coordinate.rs @@ -29,24 +29,27 @@ pub trait Coordinate { } -macro_rules! impl_coordinate_binop( - ($Self:ty, $Other:ty, $Result:ty, $Op:ident, $op:ident) => ( - impl $Op<$Other, $Result> for $Self { +macro_rules! coordinate_op( + (impl<$S:ident> ($Op:ident, $op:ident) for ($Self:ty, $Other:ty) -> $Result:ty) => ( + impl<$S: Field> $Op<$Other, $Result> for $Self { #[inline(always)] fn $op(&self, other: &$Other) -> $Result { self.bimap(other, |a, b| a.$op(b)) } } - ) -) - -macro_rules! impl_coordinate_op( - ($Self:ty, $Result:ty, $Op:ident, $op:ident) => ( - impl $Op<$Result> for $Self { + ); + (impl<$S:ident> ($Op:ident, $op:ident) for $Self:ty -> $Result:ty) => ( + impl<$S: Field> $Op<$Result> for $Self { #[inline(always)] fn $op(&self) -> $Result { self.map(|a| a.$op()) } } - ) + ); + (impl<$S:ident> -$Self:ty -> $Result:ty) => (coordinate_op!(impl<$S> (Neg, neg) for $Self -> $Result)); + (impl<$S:ident> $Self:ty + $Other:ty -> $Result:ty) => (coordinate_op!(impl<$S> (Add, add) for ($Self, $Other) -> $Result)); + (impl<$S:ident> $Self:ty - $Other:ty -> $Result:ty) => (coordinate_op!(impl<$S> (Sub, sub) for ($Self, $Other) -> $Result)); + (impl<$S:ident> $Self:ty * $Other:ty -> $Result:ty) => (coordinate_op!(impl<$S> (Mul, mul) for ($Self, $Other) -> $Result)); + (impl<$S:ident> $Self:ty / $Other:ty -> $Result:ty) => (coordinate_op!(impl<$S> (Div, div) for ($Self, $Other) -> $Result)); + (impl<$S:ident> $Self:ty % $Other:ty -> $Result:ty) => (coordinate_op!(impl<$S> (Rem, rem) for ($Self, $Other) -> $Result)); ) diff --git a/src/cgmath/traits/alg/scalar_mul.rs b/src/cgmath/traits/alg/scalar_mul.rs index 790aed9..2171e6a 100644 --- a/src/cgmath/traits/alg/scalar_mul.rs +++ b/src/cgmath/traits/alg/scalar_mul.rs @@ -20,7 +20,7 @@ /// Enforces the multiplication of an type by a scalar. pub trait ScalarMul < - S/*: Field*/ + S//: Field > : Mul + Div @@ -44,11 +44,16 @@ impl ScalarMul for f32; impl ScalarMul for f64; impl ScalarMul for float; -macro_rules! impl_scalar_binop( - ($Self:ty, $Op:ident, $op:ident) => ( - impl $Op for $Self { +macro_rules! scalar_op( + (impl<$S:ident> ($Op:ident, $op:ident) for $Self:ty -> $Result:ty) => ( + impl<$S: Field> $Op<$S, $Self> for $Self { #[inline(always)] - fn $op(&self, s: &S) -> $Self { self.map(|x| x.$op(s)) } + fn $op(&self, s: &$S) -> $Result { self.map(|x| x.$op(s)) } } - ) + ); + (impl $Self:ty + $S:ident -> $Result:ty) => (scalar_op!(impl<$S> (Add, add) for $Self -> $Result)); + (impl $Self:ty - $S:ident -> $Result:ty) => (scalar_op!(impl<$S> (Sub, sub) for $Self -> $Result)); + (impl $Self:ty * $S:ident -> $Result:ty) => (scalar_op!(impl<$S> (Mul, mul) for $Self -> $Result)); + (impl $Self:ty / $S:ident -> $Result:ty) => (scalar_op!(impl<$S> (Div, div) for $Self -> $Result)); + (impl $Self:ty % $S:ident -> $Result:ty) => (scalar_op!(impl<$S> (Rem, rem) for $Self -> $Result)); ) diff --git a/src/cgmath/traits/alg/square_matrix.rs b/src/cgmath/traits/alg/square_matrix.rs index 3c84620..2bdf594 100644 --- a/src/cgmath/traits/alg/square_matrix.rs +++ b/src/cgmath/traits/alg/square_matrix.rs @@ -30,5 +30,5 @@ pub trait SquareMatrix fn det(&self) -> S; fn invert(&self) -> Option; fn invert_self(&mut self) -> Self; - fn eigenvalue(&self, v: V) -> Option; -} \ No newline at end of file + fn is_invertable(&self) -> bool; +} diff --git a/src/cgmath/traits/util/indexable.rs b/src/cgmath/traits/util/indexable.rs index 6a8cc6c..cefa748 100644 --- a/src/cgmath/traits/util/indexable.rs +++ b/src/cgmath/traits/util/indexable.rs @@ -60,8 +60,8 @@ pub trait Indexable { } } -macro_rules! impl_indexable( - ($Self:ty, $S:ident, [$T:ty, ..$n:expr]) => ( +macro_rules! indexable( + (impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr]) => ( impl<$S> Indexable<$T, [$T,..$n]> for $Self { #[inline] fn len(&self) -> uint { $n } diff --git a/src/cgmath/types/matrix.rs b/src/cgmath/types/matrix.rs index 6efd0a6..5a3d428 100644 --- a/src/cgmath/types/matrix.rs +++ b/src/cgmath/types/matrix.rs @@ -75,41 +75,37 @@ impl Mat4 { // Trait impls -impl_indexable!(Mat2, S, [Vec2, ..2]) -impl_indexable!(Mat3, S, [Vec3, ..3]) -impl_indexable!(Mat4, S, [Vec4, ..4]) +indexable!(impl Mat2 -> [Vec2, ..2]) +indexable!(impl Mat3 -> [Vec3, ..3]) +indexable!(impl Mat4 -> [Vec4, ..4]) impl Swappable, [Vec2, ..2]> for Mat2; impl Swappable, [Vec3, ..3]> for Mat3; impl Swappable, [Vec4, ..4]> for Mat4; -impl_scalar_binop!(Mat2, Mul, mul) -impl_scalar_binop!(Mat3, Mul, mul) -impl_scalar_binop!(Mat4, Mul, mul) - -impl_scalar_binop!(Mat2, Div, div) -impl_scalar_binop!(Mat3, Div, div) -impl_scalar_binop!(Mat4, Div, div) - -impl_scalar_binop!(Mat2, Rem, rem) -impl_scalar_binop!(Mat3, Rem, rem) -impl_scalar_binop!(Mat4, Rem, rem) +scalar_op!(impl Mat2 * S -> Mat2) +scalar_op!(impl Mat3 * S -> Mat3) +scalar_op!(impl Mat4 * S -> Mat4) +scalar_op!(impl Mat2 / S -> Mat2) +scalar_op!(impl Mat3 / S -> Mat3) +scalar_op!(impl Mat4 / S -> Mat4) +scalar_op!(impl Mat2 % S -> Mat2) +scalar_op!(impl Mat3 % S -> Mat3) +scalar_op!(impl Mat4 % S -> Mat4) impl ScalarMul for Mat2; impl ScalarMul for Mat3; impl ScalarMul for Mat4; -impl_coordinate_binop!(Mat2, Mat2, Mat2, Add, add) -impl_coordinate_binop!(Mat3, Mat3, Mat3, Add, add) -impl_coordinate_binop!(Mat4, Mat4, Mat4, Add, add) - -impl_coordinate_binop!(Mat2, Mat2, Mat2, Sub, sub) -impl_coordinate_binop!(Mat3, Mat3, Mat3, Sub, sub) -impl_coordinate_binop!(Mat4, Mat4, Mat4, Sub, sub) - -impl_coordinate_op!(Mat2, Mat2, Neg, neg) -impl_coordinate_op!(Mat3, Mat3, Neg, neg) -impl_coordinate_op!(Mat4, Mat4, Neg, neg) +coordinate_op!(impl Mat2 + Mat2 -> Mat2) +coordinate_op!(impl Mat3 + Mat3 -> Mat3) +coordinate_op!(impl Mat4 + Mat4 -> Mat4) +coordinate_op!(impl Mat2 - Mat2 -> Mat2) +coordinate_op!(impl Mat3 - Mat3 -> Mat3) +coordinate_op!(impl Mat4 - Mat4 -> Mat4) +coordinate_op!(impl -Mat2 -> Mat2) +coordinate_op!(impl -Mat3 -> Mat3) +coordinate_op!(impl -Mat4 -> Mat4) impl Module for Mat2; impl Module for Mat3; diff --git a/src/cgmath/types/point.rs b/src/cgmath/types/point.rs index cf7fb19..76fc601 100644 --- a/src/cgmath/types/point.rs +++ b/src/cgmath/types/point.rs @@ -41,8 +41,8 @@ impl Point3 { } } -impl_indexable!(Point2, S, [S, ..2]) -impl_indexable!(Point3, S, [S, ..3]) +indexable!(impl Point2 -> [S, ..2]) +indexable!(impl Point3 -> [S, ..3]) impl Swappable for Point2; impl Swappable for Point3; @@ -50,23 +50,20 @@ impl Swappable for Point3; impl Coordinate for Point2; impl Coordinate for Point3; -impl_scalar_binop!(Point2, Mul, mul) -impl_scalar_binop!(Point3, Mul, mul) - -impl_scalar_binop!(Point2, Div, div) -impl_scalar_binop!(Point3, Div, div) - -impl_scalar_binop!(Point2, Rem, rem) -impl_scalar_binop!(Point3, Rem, rem) +scalar_op!(impl Point2 * S -> Point2) +scalar_op!(impl Point3 * S -> Point3) +scalar_op!(impl Point2 / S -> Point2) +scalar_op!(impl Point3 / S -> Point3) +scalar_op!(impl Point2 % S -> Point2) +scalar_op!(impl Point3 % S -> Point3) impl ScalarMul for Point2; impl ScalarMul for Point3; -impl_coordinate_binop!(Point2, Vec2, Point2, Add, add) -impl_coordinate_binop!(Point3, Vec3, Point3, Add, add) - -impl_coordinate_binop!(Point2, Point2, Vec2, Sub, sub) -impl_coordinate_binop!(Point3, Point3, Vec3, Sub, sub) +coordinate_op!(impl Point2 + Vec2 -> Point2) +coordinate_op!(impl Point3 + Vec3 -> Point3) +coordinate_op!(impl Point2 - Point2 -> Vec2) +coordinate_op!(impl Point3 - Point3 -> Vec3) impl AffineSpace> for Point2; impl AffineSpace> for Point3; diff --git a/src/cgmath/types/vector.rs b/src/cgmath/types/vector.rs index 8e5f120..b3118ba 100644 --- a/src/cgmath/types/vector.rs +++ b/src/cgmath/types/vector.rs @@ -68,8 +68,6 @@ impl_vec_clonable!(Vec4) impl_vec_clonable!(Vec5) impl_vec_clonable!(Vec6) -// Operator impls - macro_rules! impl_vec_common( ($vec_ops_mod:ident, $Self:ty, [$S:ident, ..$n:expr]) => ( pub mod $vec_ops_mod { @@ -78,16 +76,16 @@ macro_rules! impl_vec_common( use traits::ext::*; use traits::util::*; - impl_indexable!($Self, $S, [$S, ..$n]) + indexable!(impl<$S> $Self -> [$S, ..$n]) impl<$S: Clone + Field> Swappable<$S, [$S, ..$n]> for $Self; impl<$S: Clone + Field> Coordinate<$S, [$S, ..$n]> for $Self; - impl_scalar_binop!($Self, Mul, mul) - impl_scalar_binop!($Self, Div, div) - impl_scalar_binop!($Self, Rem, rem) - impl_coordinate_binop!($Self, $Self, $Self, Add, add) - impl_coordinate_binop!($Self, $Self, $Self, Sub, sub) - impl_coordinate_op!($Self, $Self, Neg, neg) + scalar_op!(impl $Self * $S -> $Self) + scalar_op!(impl $Self / $S -> $Self) + scalar_op!(impl $Self % $S -> $Self) + coordinate_op!(impl<$S> $Self + $Self -> $Self) + coordinate_op!(impl<$S> $Self - $Self -> $Self) + coordinate_op!(impl<$S> -$Self -> $Self) impl<$S: Field> ScalarMul<$S> for $Self; impl<$S: Field> Module<$S> for $Self;