Improve Index trait implementation

This commit is contained in:
Brendan Zabarauskas 2012-10-29 15:47:53 +10:00
parent e285c77238
commit 9c3dbc2658
2 changed files with 25 additions and 25 deletions

View file

@ -1,6 +1,9 @@
use std::cmp::FuzzyEq;
use cast::transmute;
use core::vec::raw::buf_as_slice; // for some reason we need to specify the core module here
use ptr::to_unsafe_ptr;
use cmp::Eq;
use math::Sqrt;
use std::cmp::FuzzyEq;
use math::*;
use mat::{Mat3, Mat4};
use vec::Vec3;
@ -161,12 +164,10 @@ pub impl Quat: Quaternion<float> {
pub impl Quat: Index<uint, float> {
#[inline]
pure fn index(i: uint) -> float {
match i {
0 => self.w,
1 => self.x,
2 => self.y,
3 => self.z,
_ => fail(~"Vector index out of bounds.")
unsafe {
do buf_as_slice(
transmute::<*Quat, *float>(
to_unsafe_ptr(&self)), 4) |slice| { slice[i] }
}
}
}

View file

@ -1,6 +1,9 @@
use std::cmp::FuzzyEq;
use cast::transmute;
use core::vec::raw::buf_as_slice; // for some reason we need to specify the core module here
use ptr::to_unsafe_ptr;
use cmp::Eq;
use math::{Abs, abs, min, max, Sqrt};
use std::cmp::FuzzyEq;
use math::*;
//
// N-dimensional Vector
@ -152,10 +155,10 @@ pub impl Vec2: Vector<float> {
pub impl Vec2: Index<uint, float> {
#[inline]
pure fn index(i: uint) -> float {
match i {
0 => self.x,
1 => self.y,
_ => fail(~"Vector index out of bounds.")
unsafe {
do buf_as_slice(
transmute::<*Vec2, *float>(
to_unsafe_ptr(&self)), 2) |slice| { slice[i] }
}
}
}
@ -338,11 +341,9 @@ pub impl Vec3: Vector<float> {
pub impl Vec3: Index<uint, float> {
#[inline]
pure fn index(i: uint) -> float {
match i {
0 => self.x,
1 => self.y,
2 => self.z,
_ => fail(~"Vector index out of bounds.")
unsafe { do buf_as_slice(
transmute::<*Vec3, *float>(
to_unsafe_ptr(&self)), 3) |slice| { slice[i] }
}
}
}
@ -530,12 +531,10 @@ pub impl Vec4: Vector<float> {
pub impl Vec4: Index<uint, float> {
#[inline]
pure fn index(i: uint) -> float {
match i {
0 => self.x,
1 => self.y,
2 => self.z,
3 => self.w,
_ => fail(~"Vector index out of bounds.")
unsafe {
do buf_as_slice(
transmute::<*Vec4, *float>(
to_unsafe_ptr(&self)), 4) |slice| { slice[i] }
}
}
}