From df9c66d1bb5144dd08c1c19d75c0634f7a6df6e8 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 7 Sep 2013 19:51:01 +1000 Subject: [PATCH] Use macro instead of loop for Array::build --- src/cgmath/array.rs | 16 +++++++++------- src/cgmath/matrix.rs | 6 +++--- src/cgmath/point.rs | 4 ++-- src/cgmath/rotation.rs | 2 +- src/cgmath/vector.rs | 6 +++--- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/cgmath/array.rs b/src/cgmath/array.rs index 04ff408..5494fa5 100644 --- a/src/cgmath/array.rs +++ b/src/cgmath/array.rs @@ -53,7 +53,7 @@ pub trait Array } macro_rules! array( - (impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr]) => ( + (impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr] $_n:ident) => ( impl<$S: Clone> Array<$T, [$T,..$n]> for $Self { #[inline] fn i<'a>(&'a self, i: uint) -> &'a $T { @@ -82,12 +82,7 @@ macro_rules! array( #[inline] fn build(builder: &fn(i: uint) -> $T) -> $Self { - use std::unstable::intrinsics; - let mut s: [$T,..$n] = unsafe { intrinsics::uninit() }; - for i in range(0u, $n) { - s[i] = builder(i); - } - Array::from_slice(s) + Array::from_slice(gen_builder!($_n)) } #[inline] @@ -103,6 +98,13 @@ macro_rules! array( ) ) +macro_rules! gen_builder( + (_1) => ([builder(0)]); + (_2) => ([builder(0), builder(1)]); + (_3) => ([builder(0), builder(1), builder(2)]); + (_4) => ([builder(0), builder(1), builder(2), builder(3)]); +) + macro_rules! approx_eq( (impl<$S:ident> $Self:ty) => ( impl<$S: Clone + ApproxEq<$S>> ApproxEq<$S> for $Self { diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index 0d12d46..7e20fd0 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -163,9 +163,9 @@ impl One for Mat2 { #[inline] fn one() -> Mat2 { Mat2::i impl One for Mat3 { #[inline] fn one() -> Mat3 { Mat3::ident() } } impl One for Mat4 { #[inline] fn one() -> Mat4 { Mat4::ident() } } -array!(impl Mat2 -> [Vec2, ..2]) -array!(impl Mat3 -> [Vec3, ..3]) -array!(impl Mat4 -> [Vec4, ..4]) +array!(impl Mat2 -> [Vec2, ..2] _2) +array!(impl Mat3 -> [Vec3, ..3] _3) +array!(impl Mat4 -> [Vec4, ..4] _4) pub trait Matrix < diff --git a/src/cgmath/point.rs b/src/cgmath/point.rs index d447896..df9f1d7 100644 --- a/src/cgmath/point.rs +++ b/src/cgmath/point.rs @@ -76,8 +76,8 @@ pub trait Point #[inline] fn add_self_v(&mut self, other: &V) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.add(b) } } } -array!(impl Point2 -> [S, ..2]) -array!(impl Point3 -> [S, ..3]) +array!(impl Point2 -> [S, ..2] _2) +array!(impl Point3 -> [S, ..3] _3) impl Point, [S, ..2]> for Point2; impl Point, [S, ..3]> for Point3; diff --git a/src/cgmath/rotation.rs b/src/cgmath/rotation.rs index 830a64f..22b4993 100644 --- a/src/cgmath/rotation.rs +++ b/src/cgmath/rotation.rs @@ -103,7 +103,7 @@ pub trait ToRot3 { #[deriving(Eq, Clone)] pub struct Euler { x: A, y: A, z: A } -array!(impl Euler -> [A, ..3]) +array!(impl Euler -> [A, ..3] _3) pub trait ToEuler { fn to_euler(&self) -> Euler; diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index d5c6d0d..2f10772 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -83,9 +83,9 @@ impl Vec4 { #[inline] pub fn unit_w() -> Vec4 { Vec4::new(zero(), zero(), zero(), one()) } } -array!(impl Vec2 -> [S, ..2]) -array!(impl Vec3 -> [S, ..3]) -array!(impl Vec4 -> [S, ..4]) +array!(impl Vec2 -> [S, ..2] _2) +array!(impl Vec3 -> [S, ..3] _3) +array!(impl Vec4 -> [S, ..4] _4) approx_eq!(impl Vec2) approx_eq!(impl Vec3)