Add and implement ToPtr trait

This commit is contained in:
Brendan Zabarauskas 2012-10-30 13:24:50 +10:00
parent 819c092321
commit 28a9265568
4 changed files with 57 additions and 3 deletions

View file

@ -1,6 +1,11 @@
use cmp::Ord;
use num::{Num, from_int};
// TODO: move to a more appropriate module
pub trait ToPtr<T> {
pure fn to_ptr() -> *T;
}
pub trait ExactEq {
pure fn exact_eq(other: &self) -> bool;
}

View file

@ -1,6 +1,6 @@
use std::cmp::FuzzyEq;
use cmp::Eq;
use math::{ExactEq, Sqrt};
use math::{ToPtr, ExactEq, Sqrt};
use quaternion::Quat;
use vector::{Vec2, Vec3, Vec4};
@ -219,6 +219,13 @@ pub impl Mat2: FuzzyEq {
}
}
pub impl Mat2: ToPtr<float> {
#[inline(always)]
pure fn to_ptr() -> *float {
self[0].to_ptr()
}
}
@ -473,6 +480,13 @@ pub impl Mat3: FuzzyEq {
}
}
pub impl Mat3: ToPtr<float> {
#[inline(always)]
pure fn to_ptr() -> *float {
self[0].to_ptr()
}
}
@ -723,4 +737,11 @@ pub impl Mat4: FuzzyEq {
self[2].fuzzy_eq(&other[2]) &&
self[3].fuzzy_eq(&other[3])
}
}
pub impl Mat4: ToPtr<float> {
#[inline(always)]
pure fn to_ptr() -> *float {
self[0].to_ptr()
}
}

View file

@ -3,7 +3,7 @@ use vec::raw::buf_as_slice;
use ptr::to_unsafe_ptr;
use cmp::Eq;
use std::cmp::FuzzyEq;
use math::{ExactEq, Sqrt};
use math::{ToPtr, ExactEq, Sqrt};
use matrix::{Mat3, Mat4};
use vector::Vec3;
@ -203,6 +203,13 @@ pub impl Quat: FuzzyEq {
}
}
pub impl Quat: ToPtr<float> {
#[inline(always)]
pure fn to_ptr() -> *float {
to_unsafe_ptr(&self[0])
}
}
pub impl Quat: ToStr {
pure fn to_str() -> ~str {
fmt!("Quat[ %f, %f, %f, %f ]", self.w, self.x, self.y, self.z)

View file

@ -3,7 +3,7 @@ use vec::raw::buf_as_slice;
use ptr::to_unsafe_ptr;
use cmp::Eq;
use std::cmp::FuzzyEq;
use math::{Abs, abs, ExactEq, max, min, Sqrt};
use math::{ToPtr, Abs, abs, ExactEq, max, min, Sqrt};
//
// N-dimensional Vector
@ -201,6 +201,13 @@ pub impl Vec2: FuzzyEq {
}
}
pub impl Vec2: ToPtr<float> {
#[inline(always)]
pure fn to_ptr() -> *float {
to_unsafe_ptr(&self[0])
}
}
pub impl Vec2: ToStr {
pure fn to_str() -> ~str {
fmt!("Vec2[ %f, %f ]", self[0], self[1])
@ -392,6 +399,13 @@ pub impl Vec3: FuzzyEq {
}
}
pub impl Vec3: ToPtr<float> {
#[inline(always)]
pure fn to_ptr() -> *float {
to_unsafe_ptr(&self[0])
}
}
pub impl Vec3: ToStr {
pure fn to_str() -> ~str {
fmt!("Vec3[ %f, %f, %f ]", self[0], self[1], self[2])
@ -589,6 +603,13 @@ pub impl Vec4: FuzzyEq {
}
}
pub impl Vec4: ToPtr<float> {
#[inline(always)]
pure fn to_ptr() -> *float {
to_unsafe_ptr(&self[0])
}
}
pub impl Vec4: ToStr {
pure fn to_str() -> ~str {
fmt!("Vec4[ %f, %f, %f, %f ]", self[0], self[1], self[2], self[3])