Move numeric-specific modules to num module, clean up imports

This commit is contained in:
Brendan Zabarauskas 2012-11-15 10:31:35 +10:00
parent 29e1231eef
commit 90ee516370
15 changed files with 98 additions and 196 deletions

View file

@ -1,6 +1,3 @@
use cmp::Ord;
use num::*;
// TODO: move to a more appropriate module
pub trait ToPtr<T> {
pure fn to_ptr() -> *T;

View file

@ -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<u8,u8> { #[inline(always)] pure fn add(rhs: &u8) -> u8 { self + *rhs } }
pub impl u16 : Add<u16,u16> { #[inline(always)] pure fn add(rhs: &u16) -> u16 { self + *rhs } }
pub impl u32 : Add<u32,u32> { #[inline(always)] pure fn add(rhs: &u32) -> u32 { self + *rhs } }
pub impl u64 : Add<u64,u64> { #[inline(always)] pure fn add(rhs: &u64) -> u64 { self + *rhs } }
pub impl uint : Add<uint,uint> { #[inline(always)] pure fn add(rhs: &uint) -> uint { self + *rhs } }
pub impl i8 : Add<i8,i8> { #[inline(always)] pure fn add(rhs: &i8) -> i8 { self + *rhs } }
pub impl i16 : Add<i16,i16> { #[inline(always)] pure fn add(rhs: &i16) -> i16 { self + *rhs } }
pub impl i32 : Add<i32,i32> { #[inline(always)] pure fn add(rhs: &i32) -> i32 { self + *rhs } }
pub impl i64 : Add<i64,i64> { #[inline(always)] pure fn add(rhs: &i64) -> i64 { self + *rhs } }
pub impl int : Add<int,int> { #[inline(always)] pure fn add(rhs: &int) -> int { self + *rhs } }
pub impl f32 : Add<f32,f32> { #[inline(always)] pure fn add(rhs: &f32) -> f32 { self + *rhs } }
pub impl f64 : Add<f64,f64> { #[inline(always)] pure fn add(rhs: &f64) -> f64 { self + *rhs } }
pub impl float : Add<float,float> { #[inline(always)] pure fn add(rhs: &float) -> float { self + *rhs } }
pub impl u8 : Sub<u8,u8> { #[inline(always)] pure fn sub(rhs: &u8) -> u8 { self - *rhs } }
pub impl u16 : Sub<u16,u16> { #[inline(always)] pure fn sub(rhs: &u16) -> u16 { self - *rhs } }
pub impl u32 : Sub<u32,u32> { #[inline(always)] pure fn sub(rhs: &u32) -> u32 { self - *rhs } }
pub impl u64 : Sub<u64,u64> { #[inline(always)] pure fn sub(rhs: &u64) -> u64 { self - *rhs } }
pub impl uint : Sub<uint,uint> { #[inline(always)] pure fn sub(rhs: &uint) -> uint { self - *rhs } }
pub impl i8 : Sub<i8,i8> { #[inline(always)] pure fn sub(rhs: &i8) -> i8 { self - *rhs } }
pub impl i16 : Sub<i16,i16> { #[inline(always)] pure fn sub(rhs: &i16) -> i16 { self - *rhs } }
pub impl i32 : Sub<i32,i32> { #[inline(always)] pure fn sub(rhs: &i32) -> i32 { self - *rhs } }
pub impl i64 : Sub<i64,i64> { #[inline(always)] pure fn sub(rhs: &i64) -> i64 { self - *rhs } }
pub impl int : Sub<int,int> { #[inline(always)] pure fn sub(rhs: &int) -> int { self - *rhs } }
pub impl f32 : Sub<f32,f32> { #[inline(always)] pure fn sub(rhs: &f32) -> f32 { self - *rhs } }
pub impl f64 : Sub<f64,f64> { #[inline(always)] pure fn sub(rhs: &f64) -> f64 { self - *rhs } }
pub impl float : Sub<float,float> { #[inline(always)] pure fn sub(rhs: &float) -> float { self - *rhs } }
pub impl u8 : Mul<u8,u8> { #[inline(always)] pure fn mul(rhs: &u8) -> u8 { self * *rhs } }
pub impl u16 : Mul<u16,u16> { #[inline(always)] pure fn mul(rhs: &u16) -> u16 { self * *rhs } }
pub impl u32 : Mul<u32,u32> { #[inline(always)] pure fn mul(rhs: &u32) -> u32 { self * *rhs } }
pub impl u64 : Mul<u64,u64> { #[inline(always)] pure fn mul(rhs: &u64) -> u64 { self * *rhs } }
pub impl uint : Mul<uint,uint> { #[inline(always)] pure fn mul(rhs: &uint) -> uint { self * *rhs } }
pub impl i8 : Mul<i8,i8> { #[inline(always)] pure fn mul(rhs: &i8) -> i8 { self * *rhs } }
pub impl i16 : Mul<i16,i16> { #[inline(always)] pure fn mul(rhs: &i16) -> i16 { self * *rhs } }
pub impl i32 : Mul<i32,i32> { #[inline(always)] pure fn mul(rhs: &i32) -> i32 { self * *rhs } }
pub impl i64 : Mul<i64,i64> { #[inline(always)] pure fn mul(rhs: &i64) -> i64 { self * *rhs } }
pub impl int : Mul<int,int> { #[inline(always)] pure fn mul(rhs: &int) -> int { self * *rhs } }
pub impl f32 : Mul<f32,f32> { #[inline(always)] pure fn mul(rhs: &f32) -> f32 { self * *rhs } }
pub impl f64 : Mul<f64,f64> { #[inline(always)] pure fn mul(rhs: &f64) -> f64 { self * *rhs } }
pub impl float : Mul<float,float> { #[inline(always)] pure fn mul(rhs: &float) -> float { self * *rhs } }
pub impl u8 : Div<u8,u8> { #[inline(always)] pure fn div(rhs: &u8) -> u8 { self / *rhs } }
pub impl u16 : Div<u16,u16> { #[inline(always)] pure fn div(rhs: &u16) -> u16 { self / *rhs } }
pub impl u32 : Div<u32,u32> { #[inline(always)] pure fn div(rhs: &u32) -> u32 { self / *rhs } }
pub impl u64 : Div<u64,u64> { #[inline(always)] pure fn div(rhs: &u64) -> u64 { self / *rhs } }
pub impl uint : Div<uint,uint> { #[inline(always)] pure fn div(rhs: &uint) -> uint { self / *rhs } }
pub impl i8 : Div<i8,i8> { #[inline(always)] pure fn div(rhs: &i8) -> i8 { self / *rhs } }
pub impl i16 : Div<i16,i16> { #[inline(always)] pure fn div(rhs: &i16) -> i16 { self / *rhs } }
pub impl i32 : Div<i32,i32> { #[inline(always)] pure fn div(rhs: &i32) -> i32 { self / *rhs } }
pub impl i64 : Div<i64,i64> { #[inline(always)] pure fn div(rhs: &i64) -> i64 { self / *rhs } }
pub impl int : Div<int,int> { #[inline(always)] pure fn div(rhs: &int) -> int { self / *rhs } }
pub impl f32 : Div<f32,f32> { #[inline(always)] pure fn div(rhs: &f32) -> f32 { self / *rhs } }
pub impl f64 : Div<f64,f64> { #[inline(always)] pure fn div(rhs: &f64) -> f64 { self / *rhs } }
pub impl float : Div<float,float> { #[inline(always)] pure fn div(rhs: &float) -> float { self / *rhs } }
pub impl u8 : Modulo<u8,u8> { #[inline(always)] pure fn modulo(rhs: &u8) -> u8 { self % *rhs } }
pub impl u16 : Modulo<u16,u16> { #[inline(always)] pure fn modulo(rhs: &u16) -> u16 { self % *rhs } }
pub impl u32 : Modulo<u32,u32> { #[inline(always)] pure fn modulo(rhs: &u32) -> u32 { self % *rhs } }
pub impl u64 : Modulo<u64,u64> { #[inline(always)] pure fn modulo(rhs: &u64) -> u64 { self % *rhs } }
pub impl uint : Modulo<uint,uint> { #[inline(always)] pure fn modulo(rhs: &uint) -> uint { self % *rhs } }
pub impl i8 : Modulo<i8,i8> { #[inline(always)] pure fn modulo(rhs: &i8) -> i8 { self % *rhs } }
pub impl i16 : Modulo<i16,i16> { #[inline(always)] pure fn modulo(rhs: &i16) -> i16 { self % *rhs } }
pub impl i32 : Modulo<i32,i32> { #[inline(always)] pure fn modulo(rhs: &i32) -> i32 { self % *rhs } }
pub impl i64 : Modulo<i64,i64> { #[inline(always)] pure fn modulo(rhs: &i64) -> i64 { self % *rhs } }
pub impl int : Modulo<int,int> { #[inline(always)] pure fn modulo(rhs: &int) -> int { self % *rhs } }
pub impl f32 : Modulo<f32,f32> { #[inline(always)] pure fn modulo(rhs: &f32) -> f32 { self % *rhs } }
pub impl f64 : Modulo<f64,f64> { #[inline(always)] pure fn modulo(rhs: &f64) -> f64 { self % *rhs } }
pub impl float : Modulo<float,float> { #[inline(always)] pure fn modulo(rhs: &float) -> float { self % *rhs } }
pub impl i8 : Neg<i8> { #[inline(always)] pure fn neg() -> i8 { -self } }
pub impl i16 : Neg<i16> { #[inline(always)] pure fn neg() -> i16 { -self } }
pub impl i32 : Neg<i32> { #[inline(always)] pure fn neg() -> i32 { -self } }
pub impl i64 : Neg<i64> { #[inline(always)] pure fn neg() -> i64 { -self } }
pub impl int : Neg<int> { #[inline(always)] pure fn neg() -> int { -self } }
pub impl f32 : Neg<f32> { #[inline(always)] pure fn neg() -> f32 { -self } }
pub impl f64 : Neg<f64> { #[inline(always)] pure fn neg() -> f64 { -self } }
pub impl float : Neg<float> { #[inline(always)] pure fn neg() -> float { -self } }

View file

@ -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<B> {
#[inline(always)] pub pure fn mixb<T:Mix<B>, B>(x: &T, y: &T, a: &B) -> T { x.mixb(y, a) }
pub impl f32: Mix<bool> {
#[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<bool> {
#[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<bool> {
#[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 } }
}

View file

@ -1,10 +1,7 @@
/**
* Exponential Functions
*/
use num::Num;
use ncast::*;
use num::cast::*;
use vector::{Vec2, Vec3, Vec4};
pub trait Exp {

View file

@ -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

View file

@ -2,7 +2,7 @@
* Vector Relational Functions
*/
use cmp::{Eq, Ord};
use core::cmp::{Eq, Ord};
use vector::{Vec2, Vec3, Vec4};

View file

@ -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<T:Copy Num NumCast AngleConv Trig>(theta: T, axis: Vec3<T>) -> Mat3<T> {
let rad = radians(&theta);

View file

@ -1,4 +1,4 @@
use ncast::*;
use num::cast::*;
use vector::{Vec3, Vec2, Vec4};
pub trait AngleConv {

View file

@ -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 {

View file

@ -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};

59
src/num/traits.rs Normal file
View file

@ -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 {}

View file

@ -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<T:Copy Num NumCast Trig Exp Extent Ord FuzzyEq> Quat<T>: Quaternion<T>
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,

View file

@ -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