Use macro instead of loop for Array::build

This commit is contained in:
Brendan Zabarauskas 2013-09-07 19:51:01 +10:00
parent c84ca595b7
commit df9c66d1bb
5 changed files with 18 additions and 16 deletions

View file

@ -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 {

View file

@ -163,9 +163,9 @@ impl<S: Clone + Float> One for Mat2<S> { #[inline] fn one() -> Mat2<S> { Mat2::i
impl<S: Clone + Float> One for Mat3<S> { #[inline] fn one() -> Mat3<S> { Mat3::ident() } }
impl<S: Clone + Float> One for Mat4<S> { #[inline] fn one() -> Mat4<S> { Mat4::ident() } }
array!(impl<S> Mat2<S> -> [Vec2<S>, ..2])
array!(impl<S> Mat3<S> -> [Vec3<S>, ..3])
array!(impl<S> Mat4<S> -> [Vec4<S>, ..4])
array!(impl<S> Mat2<S> -> [Vec2<S>, ..2] _2)
array!(impl<S> Mat3<S> -> [Vec3<S>, ..3] _3)
array!(impl<S> Mat4<S> -> [Vec4<S>, ..4] _4)
pub trait Matrix
<

View file

@ -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<S> Point2<S> -> [S, ..2])
array!(impl<S> Point3<S> -> [S, ..3])
array!(impl<S> Point2<S> -> [S, ..2] _2)
array!(impl<S> Point3<S> -> [S, ..3] _3)
impl<S: Clone + Num + Ord> Point<S, Vec2<S>, [S, ..2]> for Point2<S>;
impl<S: Clone + Num + Ord> Point<S, Vec3<S>, [S, ..3]> for Point3<S>;

View file

@ -103,7 +103,7 @@ pub trait ToRot3<S: Clone + Float> {
#[deriving(Eq, Clone)]
pub struct Euler<A> { x: A, y: A, z: A }
array!(impl<A> Euler<A> -> [A, ..3])
array!(impl<A> Euler<A> -> [A, ..3] _3)
pub trait ToEuler<A> {
fn to_euler(&self) -> Euler<A>;

View file

@ -83,9 +83,9 @@ impl<S: Clone + Num + Ord> Vec4<S> {
#[inline] pub fn unit_w() -> Vec4<S> { Vec4::new(zero(), zero(), zero(), one()) }
}
array!(impl<S> Vec2<S> -> [S, ..2])
array!(impl<S> Vec3<S> -> [S, ..3])
array!(impl<S> Vec4<S> -> [S, ..4])
array!(impl<S> Vec2<S> -> [S, ..2] _2)
array!(impl<S> Vec3<S> -> [S, ..3] _3)
array!(impl<S> Vec4<S> -> [S, ..4] _4)
approx_eq!(impl<S> Vec2<S>)
approx_eq!(impl<S> Vec3<S>)