Improve Index trait implementation
This commit is contained in:
parent
e285c77238
commit
9c3dbc2658
2 changed files with 25 additions and 25 deletions
17
src/quat.rs
17
src/quat.rs
|
@ -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 cmp::Eq;
|
||||||
use math::Sqrt;
|
use std::cmp::FuzzyEq;
|
||||||
|
use math::*;
|
||||||
use mat::{Mat3, Mat4};
|
use mat::{Mat3, Mat4};
|
||||||
use vec::Vec3;
|
use vec::Vec3;
|
||||||
|
|
||||||
|
@ -161,12 +164,10 @@ pub impl Quat: Quaternion<float> {
|
||||||
pub impl Quat: Index<uint, float> {
|
pub impl Quat: Index<uint, float> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pure fn index(i: uint) -> float {
|
pure fn index(i: uint) -> float {
|
||||||
match i {
|
unsafe {
|
||||||
0 => self.w,
|
do buf_as_slice(
|
||||||
1 => self.x,
|
transmute::<*Quat, *float>(
|
||||||
2 => self.y,
|
to_unsafe_ptr(&self)), 4) |slice| { slice[i] }
|
||||||
3 => self.z,
|
|
||||||
_ => fail(~"Vector index out of bounds.")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
33
src/vec.rs
33
src/vec.rs
|
@ -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 cmp::Eq;
|
||||||
use math::{Abs, abs, min, max, Sqrt};
|
use std::cmp::FuzzyEq;
|
||||||
|
use math::*;
|
||||||
|
|
||||||
//
|
//
|
||||||
// N-dimensional Vector
|
// N-dimensional Vector
|
||||||
|
@ -152,10 +155,10 @@ pub impl Vec2: Vector<float> {
|
||||||
pub impl Vec2: Index<uint, float> {
|
pub impl Vec2: Index<uint, float> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pure fn index(i: uint) -> float {
|
pure fn index(i: uint) -> float {
|
||||||
match i {
|
unsafe {
|
||||||
0 => self.x,
|
do buf_as_slice(
|
||||||
1 => self.y,
|
transmute::<*Vec2, *float>(
|
||||||
_ => fail(~"Vector index out of bounds.")
|
to_unsafe_ptr(&self)), 2) |slice| { slice[i] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -338,11 +341,9 @@ pub impl Vec3: Vector<float> {
|
||||||
pub impl Vec3: Index<uint, float> {
|
pub impl Vec3: Index<uint, float> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pure fn index(i: uint) -> float {
|
pure fn index(i: uint) -> float {
|
||||||
match i {
|
unsafe { do buf_as_slice(
|
||||||
0 => self.x,
|
transmute::<*Vec3, *float>(
|
||||||
1 => self.y,
|
to_unsafe_ptr(&self)), 3) |slice| { slice[i] }
|
||||||
2 => self.z,
|
|
||||||
_ => fail(~"Vector index out of bounds.")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -530,12 +531,10 @@ pub impl Vec4: Vector<float> {
|
||||||
pub impl Vec4: Index<uint, float> {
|
pub impl Vec4: Index<uint, float> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pure fn index(i: uint) -> float {
|
pure fn index(i: uint) -> float {
|
||||||
match i {
|
unsafe {
|
||||||
0 => self.x,
|
do buf_as_slice(
|
||||||
1 => self.y,
|
transmute::<*Vec4, *float>(
|
||||||
2 => self.z,
|
to_unsafe_ptr(&self)), 4) |slice| { slice[i] }
|
||||||
3 => self.w,
|
|
||||||
_ => fail(~"Vector index out of bounds.")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue