diff --git a/src/common/math.rs b/src/common/math.rs index 4fa5992..4546da2 100644 --- a/src/common/math.rs +++ b/src/common/math.rs @@ -1,7 +1,7 @@ use cmp::Ord; use num::{Num, from_int}; -use num_util::*; +use ncast::*; // TODO: move to a more appropriate module pub trait ToPtr { diff --git a/src/common/num_util.rs b/src/common/ncast.rs similarity index 63% rename from src/common/num_util.rs rename to src/common/ncast.rs index d562f66..12a098c 100644 --- a/src/common/num_util.rs +++ b/src/common/ncast.rs @@ -1,135 +1,9 @@ -trait UnSigned {} // Needs trait inheritence to work properly - -pub impl u8: UnSigned {} -pub impl u16: UnSigned {} -pub impl u32: UnSigned {} -pub impl u64: UnSigned {} -pub impl uint: UnSigned {} - - -trait Signed {} // Needs trait inheritence to work properly - -pub impl> T: Signed {} - - -trait Integer {} // Needs trait inheritence to work properly - -pub impl u8: Integer {} -pub impl u16: Integer {} -pub impl u32: Integer {} -pub impl u64: Integer {} -pub impl uint: Integer {} - -pub impl i8: Integer {} -pub impl i16: Integer {} -pub impl i32: Integer {} -pub impl i64: Integer {} -pub impl int: Integer {} - - -trait FloatingPoint {} // Needs trait inheritence to work properly - -pub impl f32: FloatingPoint {} -pub impl f64: FloatingPoint {} -pub impl float: FloatingPoint {} - - -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 } } - - +/** + * This trait allows you to easily cast between the built in numeric types, + * going above and beyond the static 'to_int' function found in the Num trait. + * I've found that it is especially handy in generic functions when you need to + * mix floating point and integer values. + */ trait NumCast { static pure fn from(n: T) -> self; pure fn cast() -> T; diff --git a/src/common/ntrait.rs b/src/common/ntrait.rs new file mode 100644 index 0000000..6e8cdcc --- /dev/null +++ b/src/common/ntrait.rs @@ -0,0 +1,145 @@ +/** + * 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. + */ + +trait UnSigned {} // TODO: implement trait inheritence + +pub impl u8: UnSigned {} +pub impl u16: UnSigned {} +pub impl u32: UnSigned {} +pub impl u64: UnSigned {} +pub impl uint: UnSigned {} + + +trait Signed {} // TODO: implement trait inheritence + +pub impl i8: Signed {} +pub impl i16: Signed {} +pub impl i32: Signed {} +pub impl i64: Signed {} +pub impl int: Signed {} + +pub impl f32: Signed {} +pub impl f64: Signed {} +pub impl float: Signed {} + +trait Integer {} // TODO: implement trait inheritence + +pub impl u8: Integer {} +pub impl u16: Integer {} +pub impl u32: Integer {} +pub impl u64: Integer {} +pub impl uint: Integer {} + +pub impl i8: Integer {} +pub impl i16: Integer {} +pub impl i32: Integer {} +pub impl i64: Integer {} +pub impl int: Integer {} + + +trait FloatingPoint {} // TODO: implement trait inheritence + +pub impl f32: FloatingPoint {} +pub impl f64: FloatingPoint {} +pub impl float: FloatingPoint {} + + +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/lmath.rc b/src/lmath.rc index 04f0775..c778715 100644 --- a/src/lmath.rc +++ b/src/lmath.rc @@ -17,7 +17,8 @@ pub mod vector; use common::*; pub mod common { pub mod math; - pub mod num_util; + pub mod ncast; + pub mod ntrait; } #[test] diff --git a/src/matrix.rs b/src/matrix.rs index 4baa8cc..c649064 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -6,7 +6,7 @@ use vec::raw::buf_as_slice; use std::cmp::FuzzyEq; use math::*; -use num_util::*; +use ncast::*; use quaternion::{Quat, ToQuat}; use vector::{Vec2, Vec3, Vec4}; diff --git a/src/projection.rs b/src/projection.rs index 2aded23..5e78a3a 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -2,7 +2,7 @@ use float::consts::pi; use float::tan; use matrix::Mat4; -use num_util::*; +use ncast::*; // // Create a perspective projection matrix diff --git a/src/quaternion.rs b/src/quaternion.rs index be2f9e5..be6133b 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -5,7 +5,7 @@ use ptr::to_unsafe_ptr; use vec::raw::buf_as_slice; use std::cmp::FuzzyEq; -use math::{ToPtr, ExactEq, Sqrt}; +use math::*; use matrix::{Mat3, Mat4}; use vector::Vec3;