Make macro syntax clearer

This commit is contained in:
Brendan Zabarauskas 2013-08-28 10:39:44 +10:00
parent a49578fe83
commit ba8c6ac7d0
7 changed files with 68 additions and 69 deletions

View file

@ -29,24 +29,27 @@ pub trait Coordinate
{ {
} }
macro_rules! impl_coordinate_binop( macro_rules! coordinate_op(
($Self:ty, $Other:ty, $Result:ty, $Op:ident, $op:ident) => ( (impl<$S:ident> ($Op:ident, $op:ident) for ($Self:ty, $Other:ty) -> $Result:ty) => (
impl<S: Field> $Op<$Other, $Result> for $Self { impl<$S: Field> $Op<$Other, $Result> for $Self {
#[inline(always)] #[inline(always)]
fn $op(&self, other: &$Other) -> $Result { fn $op(&self, other: &$Other) -> $Result {
self.bimap(other, |a, b| a.$op(b)) self.bimap(other, |a, b| a.$op(b))
} }
} }
) );
) (impl<$S:ident> ($Op:ident, $op:ident) for $Self:ty -> $Result:ty) => (
impl<$S: Field> $Op<$Result> for $Self {
macro_rules! impl_coordinate_op(
($Self:ty, $Result:ty, $Op:ident, $op:ident) => (
impl<S: Field> $Op<$Result> for $Self {
#[inline(always)] #[inline(always)]
fn $op(&self) -> $Result { fn $op(&self) -> $Result {
self.map(|a| a.$op()) 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));
) )

View file

@ -20,7 +20,7 @@
/// Enforces the multiplication of an type by a scalar. /// Enforces the multiplication of an type by a scalar.
pub trait ScalarMul pub trait ScalarMul
< <
S/*: Field*/ S//: Field
> >
: Mul<S, Self> : Mul<S, Self>
+ Div<S, Self> + Div<S, Self>
@ -44,11 +44,16 @@ impl ScalarMul<f32> for f32;
impl ScalarMul<f64> for f64; impl ScalarMul<f64> for f64;
impl ScalarMul<float> for float; impl ScalarMul<float> for float;
macro_rules! impl_scalar_binop( macro_rules! scalar_op(
($Self:ty, $Op:ident, $op:ident) => ( (impl<$S:ident> ($Op:ident, $op:ident) for $Self:ty -> $Result:ty) => (
impl<S: Field> $Op<S, $Self> for $Self { impl<$S: Field> $Op<$S, $Self> for $Self {
#[inline(always)] #[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));
) )

View file

@ -30,5 +30,5 @@ pub trait SquareMatrix
fn det(&self) -> S; fn det(&self) -> S;
fn invert(&self) -> Option<Self>; fn invert(&self) -> Option<Self>;
fn invert_self(&mut self) -> Self; fn invert_self(&mut self) -> Self;
fn eigenvalue(&self, v: V) -> Option<V>; fn is_invertable(&self) -> bool;
} }

View file

@ -60,8 +60,8 @@ pub trait Indexable<T, Slice> {
} }
} }
macro_rules! impl_indexable( macro_rules! indexable(
($Self:ty, $S:ident, [$T:ty, ..$n:expr]) => ( (impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr]) => (
impl<$S> Indexable<$T, [$T,..$n]> for $Self { impl<$S> Indexable<$T, [$T,..$n]> for $Self {
#[inline] #[inline]
fn len(&self) -> uint { $n } fn len(&self) -> uint { $n }

View file

@ -75,41 +75,37 @@ impl<S: Field> Mat4<S> {
// Trait impls // Trait impls
impl_indexable!(Mat2<S>, S, [Vec2<S>, ..2]) indexable!(impl<S> Mat2<S> -> [Vec2<S>, ..2])
impl_indexable!(Mat3<S>, S, [Vec3<S>, ..3]) indexable!(impl<S> Mat3<S> -> [Vec3<S>, ..3])
impl_indexable!(Mat4<S>, S, [Vec4<S>, ..4]) indexable!(impl<S> Mat4<S> -> [Vec4<S>, ..4])
impl<S: Clone + Field> Swappable<Vec2<S>, [Vec2<S>, ..2]> for Mat2<S>; impl<S: Clone + Field> Swappable<Vec2<S>, [Vec2<S>, ..2]> for Mat2<S>;
impl<S: Clone + Field> Swappable<Vec3<S>, [Vec3<S>, ..3]> for Mat3<S>; impl<S: Clone + Field> Swappable<Vec3<S>, [Vec3<S>, ..3]> for Mat3<S>;
impl<S: Clone + Field> Swappable<Vec4<S>, [Vec4<S>, ..4]> for Mat4<S>; impl<S: Clone + Field> Swappable<Vec4<S>, [Vec4<S>, ..4]> for Mat4<S>;
impl_scalar_binop!(Mat2<S>, Mul, mul) scalar_op!(impl Mat2<S> * S -> Mat2<S>)
impl_scalar_binop!(Mat3<S>, Mul, mul) scalar_op!(impl Mat3<S> * S -> Mat3<S>)
impl_scalar_binop!(Mat4<S>, Mul, mul) scalar_op!(impl Mat4<S> * S -> Mat4<S>)
scalar_op!(impl Mat2<S> / S -> Mat2<S>)
impl_scalar_binop!(Mat2<S>, Div, div) scalar_op!(impl Mat3<S> / S -> Mat3<S>)
impl_scalar_binop!(Mat3<S>, Div, div) scalar_op!(impl Mat4<S> / S -> Mat4<S>)
impl_scalar_binop!(Mat4<S>, Div, div) scalar_op!(impl Mat2<S> % S -> Mat2<S>)
scalar_op!(impl Mat3<S> % S -> Mat3<S>)
impl_scalar_binop!(Mat2<S>, Rem, rem) scalar_op!(impl Mat4<S> % S -> Mat4<S>)
impl_scalar_binop!(Mat3<S>, Rem, rem)
impl_scalar_binop!(Mat4<S>, Rem, rem)
impl<S: Field> ScalarMul<S> for Mat2<S>; impl<S: Field> ScalarMul<S> for Mat2<S>;
impl<S: Field> ScalarMul<S> for Mat3<S>; impl<S: Field> ScalarMul<S> for Mat3<S>;
impl<S: Field> ScalarMul<S> for Mat4<S>; impl<S: Field> ScalarMul<S> for Mat4<S>;
impl_coordinate_binop!(Mat2<S>, Mat2<S>, Mat2<S>, Add, add) coordinate_op!(impl<S> Mat2<S> + Mat2<S> -> Mat2<S>)
impl_coordinate_binop!(Mat3<S>, Mat3<S>, Mat3<S>, Add, add) coordinate_op!(impl<S> Mat3<S> + Mat3<S> -> Mat3<S>)
impl_coordinate_binop!(Mat4<S>, Mat4<S>, Mat4<S>, Add, add) coordinate_op!(impl<S> Mat4<S> + Mat4<S> -> Mat4<S>)
coordinate_op!(impl<S> Mat2<S> - Mat2<S> -> Mat2<S>)
impl_coordinate_binop!(Mat2<S>, Mat2<S>, Mat2<S>, Sub, sub) coordinate_op!(impl<S> Mat3<S> - Mat3<S> -> Mat3<S>)
impl_coordinate_binop!(Mat3<S>, Mat3<S>, Mat3<S>, Sub, sub) coordinate_op!(impl<S> Mat4<S> - Mat4<S> -> Mat4<S>)
impl_coordinate_binop!(Mat4<S>, Mat4<S>, Mat4<S>, Sub, sub) coordinate_op!(impl<S> -Mat2<S> -> Mat2<S>)
coordinate_op!(impl<S> -Mat3<S> -> Mat3<S>)
impl_coordinate_op!(Mat2<S>, Mat2<S>, Neg, neg) coordinate_op!(impl<S> -Mat4<S> -> Mat4<S>)
impl_coordinate_op!(Mat3<S>, Mat3<S>, Neg, neg)
impl_coordinate_op!(Mat4<S>, Mat4<S>, Neg, neg)
impl<S: Field> Module<S> for Mat2<S>; impl<S: Field> Module<S> for Mat2<S>;
impl<S: Field> Module<S> for Mat3<S>; impl<S: Field> Module<S> for Mat3<S>;

View file

@ -41,8 +41,8 @@ impl<S: Field> Point3<S> {
} }
} }
impl_indexable!(Point2<S>, S, [S, ..2]) indexable!(impl<S> Point2<S> -> [S, ..2])
impl_indexable!(Point3<S>, S, [S, ..3]) indexable!(impl<S> Point3<S> -> [S, ..3])
impl<S: Clone + Field> Swappable<S, [S, ..2]> for Point2<S>; impl<S: Clone + Field> Swappable<S, [S, ..2]> for Point2<S>;
impl<S: Clone + Field> Swappable<S, [S, ..3]> for Point3<S>; impl<S: Clone + Field> Swappable<S, [S, ..3]> for Point3<S>;
@ -50,23 +50,20 @@ impl<S: Clone + Field> Swappable<S, [S, ..3]> for Point3<S>;
impl<S: Clone + Field> Coordinate<S, [S, ..2]> for Point2<S>; impl<S: Clone + Field> Coordinate<S, [S, ..2]> for Point2<S>;
impl<S: Clone + Field> Coordinate<S, [S, ..3]> for Point3<S>; impl<S: Clone + Field> Coordinate<S, [S, ..3]> for Point3<S>;
impl_scalar_binop!(Point2<S>, Mul, mul) scalar_op!(impl Point2<S> * S -> Point2<S>)
impl_scalar_binop!(Point3<S>, Mul, mul) scalar_op!(impl Point3<S> * S -> Point3<S>)
scalar_op!(impl Point2<S> / S -> Point2<S>)
impl_scalar_binop!(Point2<S>, Div, div) scalar_op!(impl Point3<S> / S -> Point3<S>)
impl_scalar_binop!(Point3<S>, Div, div) scalar_op!(impl Point2<S> % S -> Point2<S>)
scalar_op!(impl Point3<S> % S -> Point3<S>)
impl_scalar_binop!(Point2<S>, Rem, rem)
impl_scalar_binop!(Point3<S>, Rem, rem)
impl<S: Field> ScalarMul<S> for Point2<S>; impl<S: Field> ScalarMul<S> for Point2<S>;
impl<S: Field> ScalarMul<S> for Point3<S>; impl<S: Field> ScalarMul<S> for Point3<S>;
impl_coordinate_binop!(Point2<S>, Vec2<S>, Point2<S>, Add, add) coordinate_op!(impl<S> Point2<S> + Vec2<S> -> Point2<S>)
impl_coordinate_binop!(Point3<S>, Vec3<S>, Point3<S>, Add, add) coordinate_op!(impl<S> Point3<S> + Vec3<S> -> Point3<S>)
coordinate_op!(impl<S> Point2<S> - Point2<S> -> Vec2<S>)
impl_coordinate_binop!(Point2<S>, Point2<S>, Vec2<S>, Sub, sub) coordinate_op!(impl<S> Point3<S> - Point3<S> -> Vec3<S>)
impl_coordinate_binop!(Point3<S>, Point3<S>, Vec3<S>, Sub, sub)
impl<S: Field> AffineSpace<S, Vec2<S>> for Point2<S>; impl<S: Field> AffineSpace<S, Vec2<S>> for Point2<S>;
impl<S: Field> AffineSpace<S, Vec3<S>> for Point3<S>; impl<S: Field> AffineSpace<S, Vec3<S>> for Point3<S>;

View file

@ -68,8 +68,6 @@ impl_vec_clonable!(Vec4<S>)
impl_vec_clonable!(Vec5<S>) impl_vec_clonable!(Vec5<S>)
impl_vec_clonable!(Vec6<S>) impl_vec_clonable!(Vec6<S>)
// Operator impls
macro_rules! impl_vec_common( macro_rules! impl_vec_common(
($vec_ops_mod:ident, $Self:ty, [$S:ident, ..$n:expr]) => ( ($vec_ops_mod:ident, $Self:ty, [$S:ident, ..$n:expr]) => (
pub mod $vec_ops_mod { pub mod $vec_ops_mod {
@ -78,16 +76,16 @@ macro_rules! impl_vec_common(
use traits::ext::*; use traits::ext::*;
use traits::util::*; 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> Swappable<$S, [$S, ..$n]> for $Self;
impl<$S: Clone + Field> Coordinate<$S, [$S, ..$n]> for $Self; impl<$S: Clone + Field> Coordinate<$S, [$S, ..$n]> for $Self;
impl_scalar_binop!($Self, Mul, mul) scalar_op!(impl $Self * $S -> $Self)
impl_scalar_binop!($Self, Div, div) scalar_op!(impl $Self / $S -> $Self)
impl_scalar_binop!($Self, Rem, rem) scalar_op!(impl $Self % $S -> $Self)
impl_coordinate_binop!($Self, $Self, $Self, Add, add) coordinate_op!(impl<$S> $Self + $Self -> $Self)
impl_coordinate_binop!($Self, $Self, $Self, Sub, sub) coordinate_op!(impl<$S> $Self - $Self -> $Self)
impl_coordinate_op!($Self, $Self, Neg, neg) coordinate_op!(impl<$S> -$Self -> $Self)
impl<$S: Field> ScalarMul<$S> for $Self; impl<$S: Field> ScalarMul<$S> for $Self;
impl<$S: Field> Module<$S> for $Self; impl<$S: Field> Module<$S> for $Self;