Simplify index methods

This commit is contained in:
Brendan Zabarauskas 2013-05-22 17:01:52 +10:00
parent 11d6d0259a
commit 0361be55c5
2 changed files with 41 additions and 39 deletions

View file

@ -1,3 +1,4 @@
use core::cast::transmute;
use core::cmp::ApproxEq; use core::cmp::ApproxEq;
use core::num::Zero::zero; use core::num::Zero::zero;
use core::num::One::one; use core::num::One::one;
@ -441,10 +442,11 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec2<T>> for Mat2<T> {
#[inline(always)] #[inline(always)]
fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2<T> { fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2<T> {
match i { unsafe {
0 => &mut self.x, &'a mut transmute::<
1 => &mut self.y, &'a mut Mat2<T>,
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i)) &'a mut [Vec2<T>,..2]
>(self)[i]
} }
} }
@ -535,7 +537,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec2<T>> for Mat2<T> {
#[inline(always)] #[inline(always)]
fn to_ptr(&self) -> *T { fn to_ptr(&self) -> *T {
unsafe { cast::transmute(self) } unsafe { transmute(self) }
} }
} }
@ -642,7 +644,7 @@ impl<T:Copy + Float + NumAssign> BaseMat2<T, Vec2<T>> for Mat2<T> {
impl<T:Copy> Index<uint, Vec2<T>> for Mat2<T> { impl<T:Copy> Index<uint, Vec2<T>> for Mat2<T> {
#[inline(always)] #[inline(always)]
fn index(&self, i: &uint) -> Vec2<T> { fn index(&self, i: &uint) -> Vec2<T> {
unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 2) |slice| { slice[*i] } } unsafe { transmute::<Mat2<T>,[Vec2<T>,..2]>(*self)[*i] }
} }
} }
@ -840,11 +842,11 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec3<T>> for Mat3<T> {
#[inline(always)] #[inline(always)]
fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3<T> { fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3<T> {
match i { unsafe {
0 => &mut self.x, &'a mut transmute::<
1 => &mut self.y, &'a mut Mat3<T>,
2 => &mut self.z, &'a mut [Vec3<T>,..3]
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i)) >(self)[i]
} }
} }
@ -957,7 +959,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec3<T>> for Mat3<T> {
#[inline(always)] #[inline(always)]
fn to_ptr(&self) -> *T { fn to_ptr(&self) -> *T {
unsafe { cast::transmute(self) } unsafe { transmute(self) }
} }
} }
@ -1189,7 +1191,7 @@ impl<T:Copy + Float + NumAssign> BaseMat3<T, Vec3<T>> for Mat3<T> {
impl<T:Copy> Index<uint, Vec3<T>> for Mat3<T> { impl<T:Copy> Index<uint, Vec3<T>> for Mat3<T> {
#[inline(always)] #[inline(always)]
fn index(&self, i: &uint) -> Vec3<T> { fn index(&self, i: &uint) -> Vec3<T> {
unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 3) |slice| { slice[*i] } } unsafe { transmute::<Mat3<T>,[Vec3<T>,..3]>(*self)[*i] }
} }
} }
@ -1460,12 +1462,11 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec4<T>> for Mat4<T> {
#[inline(always)] #[inline(always)]
fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4<T> { fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4<T> {
match i { unsafe {
0 => &mut self.x, &'a mut transmute::<
1 => &mut self.y, &'a mut Mat4<T>,
2 => &mut self.z, &'a mut [Vec4<T>,..4]
3 => &mut self.w, >(self)[i]
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i))
} }
} }
@ -1603,7 +1604,7 @@ impl<T:Copy + Float + NumAssign> BaseMat<T, Vec4<T>> for Mat4<T> {
#[inline(always)] #[inline(always)]
fn to_ptr(&self) -> *T { fn to_ptr(&self) -> *T {
unsafe { cast::transmute(self) } unsafe { transmute(self) }
} }
} }
@ -1681,7 +1682,7 @@ impl<T:Copy + Float + NumAssign> Neg<Mat4<T>> for Mat4<T> {
impl<T:Copy> Index<uint, Vec4<T>> for Mat4<T> { impl<T:Copy> Index<uint, Vec4<T>> for Mat4<T> {
#[inline(always)] #[inline(always)]
fn index(&self, i: &uint) -> Vec4<T> { fn index(&self, i: &uint) -> Vec4<T> {
unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 4) |slice| { slice[*i] } } unsafe { transmute::<Mat4<T>,[Vec4<T>,..4]>(*self)[*i] }
} }
} }

View file

@ -1,3 +1,4 @@
use core::cast::transmute;
use core::cmp::ApproxEq; use core::cmp::ApproxEq;
use core::num::Zero::zero; use core::num::Zero::zero;
use core::num::One::one; use core::num::One::one;
@ -536,10 +537,11 @@ impl<T:Copy + Eq> BaseVec<T> for Vec2<T> {
#[inline(always)] #[inline(always)]
fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
match i { unsafe {
0 => &mut self.x, &'a mut transmute::<
1 => &mut self.y, &'a mut Vec2<T>,
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i)) &'a mut [T,..2]
>(self)[i]
} }
} }
@ -559,7 +561,7 @@ impl<T> BaseVec2<T> for Vec2<T> {
impl<T:Copy + Eq> Index<uint, T> for Vec2<T> { impl<T:Copy + Eq> Index<uint, T> for Vec2<T> {
#[inline(always)] #[inline(always)]
fn index(&self, i: &uint) -> T { fn index(&self, i: &uint) -> T {
unsafe { do vec::raw::buf_as_slice(self.to_ptr(), 2) |slice| { slice[*i] } } unsafe { transmute::<Vec2<T>,[T,..2]>(*self)[*i] }
} }
} }
@ -842,11 +844,11 @@ impl<T:Copy + Eq> BaseVec<T> for Vec3<T> {
#[inline(always)] #[inline(always)]
fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
match i { unsafe {
0 => &mut self.x, &'a mut transmute::<
1 => &mut self.y, &'a mut Vec3<T>,
2 => &mut self.z, &'a mut [T,..3]
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i)) >(self)[i]
} }
} }
@ -866,7 +868,7 @@ impl<T> BaseVec3<T> for Vec3<T> {
impl<T:Copy + Eq> Index<uint, T> for Vec3<T> { impl<T:Copy + Eq> Index<uint, T> for Vec3<T> {
#[inline(always)] #[inline(always)]
fn index(&self, i: &uint) -> T { fn index(&self, i: &uint) -> T {
unsafe { do vec::raw::buf_as_slice(self.to_ptr(), 3) |slice| { slice[*i] } } unsafe { transmute::<Vec3<T>,[T,..3]>(*self)[*i] }
} }
} }
@ -1166,12 +1168,11 @@ impl<T:Copy + Eq> BaseVec<T> for Vec4<T> {
#[inline(always)] #[inline(always)]
fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
match i { unsafe {
0 => &mut self.x, &'a mut transmute::<
1 => &mut self.y, &'a mut Vec4<T>,
2 => &mut self.z, &'a mut [T,..4]
3 => &mut self.w, >(self)[i]
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i))
} }
} }
@ -1191,7 +1192,7 @@ impl<T> BaseVec4<T> for Vec4<T> {
impl<T:Copy + Eq> Index<uint, T> for Vec4<T> { impl<T:Copy + Eq> Index<uint, T> for Vec4<T> {
#[inline(always)] #[inline(always)]
fn index(&self, i: &uint) -> T { fn index(&self, i: &uint) -> T {
unsafe { do vec::raw::buf_as_slice(self.to_ptr(), 4) |slice| { slice[*i] } } unsafe { transmute::<Vec4<T>,[T,..4]>(*self)[*i] }
} }
} }