diff --git a/src/mat.rs b/src/mat.rs index 2393952..0058f12 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -1,3 +1,4 @@ +use core::cast::transmute; use core::cmp::ApproxEq; use core::num::Zero::zero; use core::num::One::one; @@ -441,10 +442,11 @@ impl BaseMat> for Mat2 { #[inline(always)] fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2 { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i)) + unsafe { + &'a mut transmute::< + &'a mut Mat2, + &'a mut [Vec2,..2] + >(self)[i] } } @@ -535,7 +537,7 @@ impl BaseMat> for Mat2 { #[inline(always)] fn to_ptr(&self) -> *T { - unsafe { cast::transmute(self) } + unsafe { transmute(self) } } } @@ -642,7 +644,7 @@ impl BaseMat2> for Mat2 { impl Index> for Mat2 { #[inline(always)] fn index(&self, i: &uint) -> Vec2 { - unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 2) |slice| { slice[*i] } } + unsafe { transmute::,[Vec2,..2]>(*self)[*i] } } } @@ -840,11 +842,11 @@ impl BaseMat> for Mat3 { #[inline(always)] fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3 { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i)) + unsafe { + &'a mut transmute::< + &'a mut Mat3, + &'a mut [Vec3,..3] + >(self)[i] } } @@ -957,7 +959,7 @@ impl BaseMat> for Mat3 { #[inline(always)] fn to_ptr(&self) -> *T { - unsafe { cast::transmute(self) } + unsafe { transmute(self) } } } @@ -1189,7 +1191,7 @@ impl BaseMat3> for Mat3 { impl Index> for Mat3 { #[inline(always)] fn index(&self, i: &uint) -> Vec3 { - unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 3) |slice| { slice[*i] } } + unsafe { transmute::,[Vec3,..3]>(*self)[*i] } } } @@ -1460,12 +1462,11 @@ impl BaseMat> for Mat4 { #[inline(always)] fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4 { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - 3 => &mut self.w, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i)) + unsafe { + &'a mut transmute::< + &'a mut Mat4, + &'a mut [Vec4,..4] + >(self)[i] } } @@ -1603,7 +1604,7 @@ impl BaseMat> for Mat4 { #[inline(always)] fn to_ptr(&self) -> *T { - unsafe { cast::transmute(self) } + unsafe { transmute(self) } } } @@ -1681,7 +1682,7 @@ impl Neg> for Mat4 { impl Index> for Mat4 { #[inline(always)] fn index(&self, i: &uint) -> Vec4 { - unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 4) |slice| { slice[*i] } } + unsafe { transmute::,[Vec4,..4]>(*self)[*i] } } } diff --git a/src/vec.rs b/src/vec.rs index 8c57424..85cd3aa 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1,3 +1,4 @@ +use core::cast::transmute; use core::cmp::ApproxEq; use core::num::Zero::zero; use core::num::One::one; @@ -536,10 +537,11 @@ impl BaseVec for Vec2 { #[inline(always)] fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i)) + unsafe { + &'a mut transmute::< + &'a mut Vec2, + &'a mut [T,..2] + >(self)[i] } } @@ -559,7 +561,7 @@ impl BaseVec2 for Vec2 { impl Index for Vec2 { #[inline(always)] fn index(&self, i: &uint) -> T { - unsafe { do vec::raw::buf_as_slice(self.to_ptr(), 2) |slice| { slice[*i] } } + unsafe { transmute::,[T,..2]>(*self)[*i] } } } @@ -842,11 +844,11 @@ impl BaseVec for Vec3 { #[inline(always)] fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i)) + unsafe { + &'a mut transmute::< + &'a mut Vec3, + &'a mut [T,..3] + >(self)[i] } } @@ -866,7 +868,7 @@ impl BaseVec3 for Vec3 { impl Index for Vec3 { #[inline(always)] fn index(&self, i: &uint) -> T { - unsafe { do vec::raw::buf_as_slice(self.to_ptr(), 3) |slice| { slice[*i] } } + unsafe { transmute::,[T,..3]>(*self)[*i] } } } @@ -1166,12 +1168,11 @@ impl BaseVec for Vec4 { #[inline(always)] fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { - match i { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - 3 => &mut self.w, - _ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i)) + unsafe { + &'a mut transmute::< + &'a mut Vec4, + &'a mut [T,..4] + >(self)[i] } } @@ -1191,7 +1192,7 @@ impl BaseVec4 for Vec4 { impl Index for Vec4 { #[inline(always)] fn index(&self, i: &uint) -> T { - unsafe { do vec::raw::buf_as_slice(self.to_ptr(), 4) |slice| { slice[*i] } } + unsafe { transmute::,[T,..4]>(*self)[*i] } } }