diff --git a/src/common/math.rs b/src/common/math.rs index 2814dbb..c97f499 100644 --- a/src/common/math.rs +++ b/src/common/math.rs @@ -1,6 +1,3 @@ -use cmp::Ord; -use num::*; - // TODO: move to a more appropriate module pub trait ToPtr { pure fn to_ptr() -> *T; diff --git a/src/common/ntrait.rs b/src/common/ntrait.rs deleted file mode 100644 index 7afc80d..0000000 --- a/src/common/ntrait.rs +++ /dev/null @@ -1,153 +0,0 @@ -/** - * Various traits intended to be used with the built in numeric types. These - * allow one to be more specific with trait bounds when using generics. - * - * Note: These traits won't be able to be used to their full potential until - * trait inheritence is implemented. - */ - -use cmp::{Eq, Ord}; -use std::cmp::FuzzyEq; -use ncast::*; -use nconsts::{IntConsts, FloatConsts}; - -trait NumExt: Copy, Eq, Num, NumCast, Ord {} - -trait UnSignedNum: NumExt {} - -pub impl u8: UnSignedNum {} -pub impl u16: UnSignedNum {} -pub impl u32: UnSignedNum {} -pub impl u64: UnSignedNum {} -pub impl uint: UnSignedNum {} - - -trait SignedNum: NumExt {} - -pub impl i8: SignedNum {} -pub impl i16: SignedNum {} -pub impl i32: SignedNum {} -pub impl i64: SignedNum {} -pub impl int: SignedNum {} - -pub impl f32: SignedNum {} -pub impl f64: SignedNum {} -pub impl float: SignedNum {} - - -trait IntegerNum: NumExt, IntConsts {} - -pub impl u8: IntegerNum {} -pub impl u16: IntegerNum {} -pub impl u32: IntegerNum {} -pub impl u64: IntegerNum {} -pub impl uint: IntegerNum {} - -pub impl i8: IntegerNum {} -pub impl i16: IntegerNum {} -pub impl i32: IntegerNum {} -pub impl i64: IntegerNum {} -pub impl int: IntegerNum {} - - -trait FloatNum: NumExt, FloatConsts, FuzzyEq {} - -pub impl f32: FloatNum {} -pub impl f64: FloatNum {} -pub impl float: FloatNum {} - - -pub impl u8 : Add { #[inline(always)] pure fn add(rhs: &u8) -> u8 { self + *rhs } } -pub impl u16 : Add { #[inline(always)] pure fn add(rhs: &u16) -> u16 { self + *rhs } } -pub impl u32 : Add { #[inline(always)] pure fn add(rhs: &u32) -> u32 { self + *rhs } } -pub impl u64 : Add { #[inline(always)] pure fn add(rhs: &u64) -> u64 { self + *rhs } } -pub impl uint : Add { #[inline(always)] pure fn add(rhs: &uint) -> uint { self + *rhs } } - -pub impl i8 : Add { #[inline(always)] pure fn add(rhs: &i8) -> i8 { self + *rhs } } -pub impl i16 : Add { #[inline(always)] pure fn add(rhs: &i16) -> i16 { self + *rhs } } -pub impl i32 : Add { #[inline(always)] pure fn add(rhs: &i32) -> i32 { self + *rhs } } -pub impl i64 : Add { #[inline(always)] pure fn add(rhs: &i64) -> i64 { self + *rhs } } -pub impl int : Add { #[inline(always)] pure fn add(rhs: &int) -> int { self + *rhs } } - -pub impl f32 : Add { #[inline(always)] pure fn add(rhs: &f32) -> f32 { self + *rhs } } -pub impl f64 : Add { #[inline(always)] pure fn add(rhs: &f64) -> f64 { self + *rhs } } -pub impl float : Add { #[inline(always)] pure fn add(rhs: &float) -> float { self + *rhs } } - - -pub impl u8 : Sub { #[inline(always)] pure fn sub(rhs: &u8) -> u8 { self - *rhs } } -pub impl u16 : Sub { #[inline(always)] pure fn sub(rhs: &u16) -> u16 { self - *rhs } } -pub impl u32 : Sub { #[inline(always)] pure fn sub(rhs: &u32) -> u32 { self - *rhs } } -pub impl u64 : Sub { #[inline(always)] pure fn sub(rhs: &u64) -> u64 { self - *rhs } } -pub impl uint : Sub { #[inline(always)] pure fn sub(rhs: &uint) -> uint { self - *rhs } } - -pub impl i8 : Sub { #[inline(always)] pure fn sub(rhs: &i8) -> i8 { self - *rhs } } -pub impl i16 : Sub { #[inline(always)] pure fn sub(rhs: &i16) -> i16 { self - *rhs } } -pub impl i32 : Sub { #[inline(always)] pure fn sub(rhs: &i32) -> i32 { self - *rhs } } -pub impl i64 : Sub { #[inline(always)] pure fn sub(rhs: &i64) -> i64 { self - *rhs } } -pub impl int : Sub { #[inline(always)] pure fn sub(rhs: &int) -> int { self - *rhs } } - -pub impl f32 : Sub { #[inline(always)] pure fn sub(rhs: &f32) -> f32 { self - *rhs } } -pub impl f64 : Sub { #[inline(always)] pure fn sub(rhs: &f64) -> f64 { self - *rhs } } -pub impl float : Sub { #[inline(always)] pure fn sub(rhs: &float) -> float { self - *rhs } } - - -pub impl u8 : Mul { #[inline(always)] pure fn mul(rhs: &u8) -> u8 { self * *rhs } } -pub impl u16 : Mul { #[inline(always)] pure fn mul(rhs: &u16) -> u16 { self * *rhs } } -pub impl u32 : Mul { #[inline(always)] pure fn mul(rhs: &u32) -> u32 { self * *rhs } } -pub impl u64 : Mul { #[inline(always)] pure fn mul(rhs: &u64) -> u64 { self * *rhs } } -pub impl uint : Mul { #[inline(always)] pure fn mul(rhs: &uint) -> uint { self * *rhs } } - -pub impl i8 : Mul { #[inline(always)] pure fn mul(rhs: &i8) -> i8 { self * *rhs } } -pub impl i16 : Mul { #[inline(always)] pure fn mul(rhs: &i16) -> i16 { self * *rhs } } -pub impl i32 : Mul { #[inline(always)] pure fn mul(rhs: &i32) -> i32 { self * *rhs } } -pub impl i64 : Mul { #[inline(always)] pure fn mul(rhs: &i64) -> i64 { self * *rhs } } -pub impl int : Mul { #[inline(always)] pure fn mul(rhs: &int) -> int { self * *rhs } } - -pub impl f32 : Mul { #[inline(always)] pure fn mul(rhs: &f32) -> f32 { self * *rhs } } -pub impl f64 : Mul { #[inline(always)] pure fn mul(rhs: &f64) -> f64 { self * *rhs } } -pub impl float : Mul { #[inline(always)] pure fn mul(rhs: &float) -> float { self * *rhs } } - - -pub impl u8 : Div { #[inline(always)] pure fn div(rhs: &u8) -> u8 { self / *rhs } } -pub impl u16 : Div { #[inline(always)] pure fn div(rhs: &u16) -> u16 { self / *rhs } } -pub impl u32 : Div { #[inline(always)] pure fn div(rhs: &u32) -> u32 { self / *rhs } } -pub impl u64 : Div { #[inline(always)] pure fn div(rhs: &u64) -> u64 { self / *rhs } } -pub impl uint : Div { #[inline(always)] pure fn div(rhs: &uint) -> uint { self / *rhs } } - -pub impl i8 : Div { #[inline(always)] pure fn div(rhs: &i8) -> i8 { self / *rhs } } -pub impl i16 : Div { #[inline(always)] pure fn div(rhs: &i16) -> i16 { self / *rhs } } -pub impl i32 : Div { #[inline(always)] pure fn div(rhs: &i32) -> i32 { self / *rhs } } -pub impl i64 : Div { #[inline(always)] pure fn div(rhs: &i64) -> i64 { self / *rhs } } -pub impl int : Div { #[inline(always)] pure fn div(rhs: &int) -> int { self / *rhs } } - -pub impl f32 : Div { #[inline(always)] pure fn div(rhs: &f32) -> f32 { self / *rhs } } -pub impl f64 : Div { #[inline(always)] pure fn div(rhs: &f64) -> f64 { self / *rhs } } -pub impl float : Div { #[inline(always)] pure fn div(rhs: &float) -> float { self / *rhs } } - - -pub impl u8 : Modulo { #[inline(always)] pure fn modulo(rhs: &u8) -> u8 { self % *rhs } } -pub impl u16 : Modulo { #[inline(always)] pure fn modulo(rhs: &u16) -> u16 { self % *rhs } } -pub impl u32 : Modulo { #[inline(always)] pure fn modulo(rhs: &u32) -> u32 { self % *rhs } } -pub impl u64 : Modulo { #[inline(always)] pure fn modulo(rhs: &u64) -> u64 { self % *rhs } } -pub impl uint : Modulo { #[inline(always)] pure fn modulo(rhs: &uint) -> uint { self % *rhs } } - -pub impl i8 : Modulo { #[inline(always)] pure fn modulo(rhs: &i8) -> i8 { self % *rhs } } -pub impl i16 : Modulo { #[inline(always)] pure fn modulo(rhs: &i16) -> i16 { self % *rhs } } -pub impl i32 : Modulo { #[inline(always)] pure fn modulo(rhs: &i32) -> i32 { self % *rhs } } -pub impl i64 : Modulo { #[inline(always)] pure fn modulo(rhs: &i64) -> i64 { self % *rhs } } -pub impl int : Modulo { #[inline(always)] pure fn modulo(rhs: &int) -> int { self % *rhs } } - -pub impl f32 : Modulo { #[inline(always)] pure fn modulo(rhs: &f32) -> f32 { self % *rhs } } -pub impl f64 : Modulo { #[inline(always)] pure fn modulo(rhs: &f64) -> f64 { self % *rhs } } -pub impl float : Modulo { #[inline(always)] pure fn modulo(rhs: &float) -> float { self % *rhs } } - - -pub impl i8 : Neg { #[inline(always)] pure fn neg() -> i8 { -self } } -pub impl i16 : Neg { #[inline(always)] pure fn neg() -> i16 { -self } } -pub impl i32 : Neg { #[inline(always)] pure fn neg() -> i32 { -self } } -pub impl i64 : Neg { #[inline(always)] pure fn neg() -> i64 { -self } } -pub impl int : Neg { #[inline(always)] pure fn neg() -> int { -self } } - -pub impl f32 : Neg { #[inline(always)] pure fn neg() -> f32 { -self } } -pub impl f64 : Neg { #[inline(always)] pure fn neg() -> f64 { -self } } -pub impl float : Neg { #[inline(always)] pure fn neg() -> float { -self } } \ No newline at end of file diff --git a/src/funs/common.rs b/src/funs/common.rs index c41b2bb..bf292f9 100644 --- a/src/funs/common.rs +++ b/src/funs/common.rs @@ -1,8 +1,7 @@ -use cmp::{Eq, Ord}; -use num::Num; +use core::cmp::{Eq, Ord}; -use ncast::*; -use vector::*; +use num::cast::*; +use vector::{Vec2, Vec3, Vec4}; /** * Common Functions for all numeric types @@ -201,16 +200,16 @@ pub trait Mix { #[inline(always)] pub pure fn mixb, B>(x: &T, y: &T, a: &B) -> T { x.mixb(y, a) } pub impl f32: Mix { - #[inline(always)] pure fn mix(y: &f32, a: &f32) -> f32 { self * (1f32 - (*a)) + y * (*a) } + #[inline(always)] pure fn mix(y: &f32, a: &f32) -> f32 { self * (1f32 - (*a)) + (*y) * (*a) } #[inline(always)] pure fn mixb(y: &f32, a: &bool) -> f32 { if *a { *y } else { self } } } pub impl f64: Mix { - #[inline(always)] pure fn mix(y: &f64, a: &f64) -> f64 { self * (1f64 - (*a)) + y * (*a) } + #[inline(always)] pure fn mix(y: &f64, a: &f64) -> f64 { self * (1f64 - (*a)) + (*y) * (*a) } #[inline(always)] pure fn mixb(y: &f64, a: &bool) -> f64 { if *a { *y } else { self } } } pub impl float: Mix { - #[inline(always)] pure fn mix(y: &float, a: &float) -> float { self * (1f - (*a)) + y * (*a) } + #[inline(always)] pure fn mix(y: &float, a: &float) -> float { self * (1f - (*a)) + (*y) * (*a) } #[inline(always)] pure fn mixb(y: &float, a: &bool) -> float { if *a { *y } else { self } } } \ No newline at end of file diff --git a/src/funs/exp.rs b/src/funs/exp.rs index 5f63ef1..cddb288 100644 --- a/src/funs/exp.rs +++ b/src/funs/exp.rs @@ -1,10 +1,7 @@ /** * Exponential Functions */ - -use num::Num; - -use ncast::*; +use num::cast::*; use vector::{Vec2, Vec3, Vec4}; pub trait Exp { diff --git a/src/funs/projection.rs b/src/funs/projection.rs index 019b478..6fd804d 100644 --- a/src/funs/projection.rs +++ b/src/funs/projection.rs @@ -1,8 +1,8 @@ -use float::consts::pi; -use float::tan; +use core::float::consts::pi; +use core::float::tan; use matrix::Mat4; -use ncast::*; +use num::cast::*; // // Create a perspective projection matrix diff --git a/src/funs/relv.rs b/src/funs/relv.rs index 28e2dd2..6bf3b14 100644 --- a/src/funs/relv.rs +++ b/src/funs/relv.rs @@ -2,7 +2,7 @@ * Vector Relational Functions */ -use cmp::{Eq, Ord}; +use core::cmp::{Eq, Ord}; use vector::{Vec2, Vec3, Vec4}; diff --git a/src/funs/transform.rs b/src/funs/transform.rs index 125c125..fca1acb 100644 --- a/src/funs/transform.rs +++ b/src/funs/transform.rs @@ -1,8 +1,6 @@ -use num::Num; -use ncast::*; -use funs::exp::Exp; use funs::trig::*; use matrix::{Mat3, Mat4}; +use num::cast::*; pub pure fn mat3_from_rotation(theta: T, axis: Vec3) -> Mat3 { let rad = radians(&theta); diff --git a/src/funs/trig.rs b/src/funs/trig.rs index 7a0c9e9..5fc7e8b 100644 --- a/src/funs/trig.rs +++ b/src/funs/trig.rs @@ -1,4 +1,4 @@ -use ncast::*; +use num::cast::*; use vector::{Vec3, Vec2, Vec4}; pub trait AngleConv { diff --git a/src/lmath.rc b/src/lmath.rc index 86f700f..c436922 100644 --- a/src/lmath.rc +++ b/src/lmath.rc @@ -24,9 +24,12 @@ mod test { use common::*; pub mod common { pub mod math; - pub mod ncast; - pub mod nconsts; - pub mod ntrait; +} + +pub mod num { + pub mod cast; + pub mod consts; + pub mod traits; } pub mod funs { diff --git a/src/matrix.rs b/src/matrix.rs index cec33f2..b377e7a 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,13 +1,14 @@ -use cast::transmute; -use cmp::Eq; -use ptr::to_unsafe_ptr; -use vec::raw::buf_as_slice; +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::to_unsafe_ptr; +use core::vec::raw::buf_as_slice; + use std::cmp::FuzzyEq; use funs::common::*; -use funs::exp::Exp; +use funs::exp::*; use math::*; -use ncast::*; +use num::cast::*; use quaternion::{Quat, ToQuat}; use vector::{Vec2, Vec3, Vec4}; diff --git a/src/common/ncast.rs b/src/num/cast.rs similarity index 100% rename from src/common/ncast.rs rename to src/num/cast.rs diff --git a/src/common/nconsts.rs b/src/num/consts.rs similarity index 100% rename from src/common/nconsts.rs rename to src/num/consts.rs diff --git a/src/num/traits.rs b/src/num/traits.rs new file mode 100644 index 0000000..6aabfcd --- /dev/null +++ b/src/num/traits.rs @@ -0,0 +1,59 @@ +/** + * Various traits intended to be used with the built in numeric types. These + * allow one to be more specific with trait bounds when using generics. + * + * Note: These traits won't be able to be used to their full potential until + * trait inheritence is implemented. + */ + +use core::cmp::{Eq, Ord}; + +use std::cmp::FuzzyEq; + +use num::cast::*; +use num::consts::*; + +trait NumExt: Copy, Eq, Num, NumCast, Ord {} + +trait UnSignedNum: NumExt {} + +pub impl u8: UnSignedNum {} +pub impl u16: UnSignedNum {} +pub impl u32: UnSignedNum {} +pub impl u64: UnSignedNum {} +pub impl uint: UnSignedNum {} + + +trait SignedNum: NumExt {} + +pub impl i8: SignedNum {} +pub impl i16: SignedNum {} +pub impl i32: SignedNum {} +pub impl i64: SignedNum {} +pub impl int: SignedNum {} + +pub impl f32: SignedNum {} +pub impl f64: SignedNum {} +pub impl float: SignedNum {} + + +trait IntegerNum: NumExt, IntConsts {} + +pub impl u8: IntegerNum {} +pub impl u16: IntegerNum {} +pub impl u32: IntegerNum {} +pub impl u64: IntegerNum {} +pub impl uint: IntegerNum {} + +pub impl i8: IntegerNum {} +pub impl i16: IntegerNum {} +pub impl i32: IntegerNum {} +pub impl i64: IntegerNum {} +pub impl int: IntegerNum {} + + +trait FloatNum: NumExt, FloatConsts, FuzzyEq {} + +pub impl f32: FloatNum {} +pub impl f64: FloatNum {} +pub impl float: FloatNum {} \ No newline at end of file diff --git a/src/quaternion.rs b/src/quaternion.rs index a159fc2..2fd00e5 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -1,8 +1,8 @@ -use cast::transmute; -use cmp::Eq; -use num::from_int; -use ptr::{addr_of, to_unsafe_ptr}; -use vec::raw::buf_as_slice; +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::{addr_of, to_unsafe_ptr}; +use core::vec::raw::buf_as_slice; + use std::cmp::FuzzyEq; use funs::exp::*; @@ -10,7 +10,7 @@ use funs::trig::*; use funs::common::*; use math::*; use matrix::{Mat3, Mat4}; -use ncast::*; +use num::cast::*; use vector::Vec3; @@ -250,7 +250,7 @@ pub impl Quat: Quaternion let wz2 = z2 * self.w; let wx2 = x2 * self.w; - let _1: T = from_int(1); + let _1: T = cast(1); Mat3::new(_1 - yy2 - zz2, xy2 - wz2, xz2 + wy2, xy2 + wz2, _1 - xx2 - zz2, yz2 - wx2, diff --git a/src/vector.rs b/src/vector.rs index cf6e9da..5ac1bee 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -1,12 +1,13 @@ -use cast::transmute; -use cmp::{Eq, Ord}; -use vec::raw::buf_as_slice; -use ptr::{addr_of, to_unsafe_ptr}; +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::{addr_of, to_unsafe_ptr}; +use core::vec::raw::buf_as_slice; + use std::cmp::FuzzyEq; use funs::exp::Exp; -use ncast::*; use math::*; +use num::cast::*; // GLSL equivalent type aliases