From 10f8570441e23bde7dedf75fa54d1a1d41619f3d Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 1 Dec 2012 14:19:21 +1000 Subject: [PATCH] Divide up trait implementations --- src/color/color.rs | 72 +++++++++++++++------------- src/mat.rs | 114 ++++++++++++++++++++++++++------------------- src/num/consts.rs | 15 ++++-- src/quat.rs | 48 ++++++++++--------- src/vec.rs | 84 ++++++++++++++++++++------------- 5 files changed, 195 insertions(+), 138 deletions(-) diff --git a/src/color/color.rs b/src/color/color.rs index 07b9c13..311d1af 100644 --- a/src/color/color.rs +++ b/src/color/color.rs @@ -143,14 +143,6 @@ pub impl RGB { } pub impl RGB: Dimensional { - #[inline(always)] - pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*RGB, *T>( - to_unsafe_ptr(&self)), 3) |slice| { slice[i] } - } - } - #[inline(always)] static pure fn dim() -> uint { 3 } @@ -165,6 +157,16 @@ pub impl RGB: Dimensional { } } +pub impl RGB: Index { + #[inline(always)] + pure fn index(i: uint) -> T { + unsafe { do buf_as_slice( + transmute::<*RGB, *T>( + to_unsafe_ptr(&self)), 3) |slice| { slice[i] } + } + } +} + pub impl RGB: Color { #[inline(always)] pure fn inverse(&self) -> RGB { @@ -262,14 +264,6 @@ pub impl RGBA { } pub impl RGBA: Dimensional { - #[inline(always)] - pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*RGBA, *T>( - to_unsafe_ptr(&self)), 4) |slice| { slice[i] } - } - } - #[inline(always)] static pure fn dim() -> uint { 4 } @@ -284,6 +278,16 @@ pub impl RGBA: Dimensional { } } +pub impl RGBA: Index { + #[inline(always)] + pure fn index(i: uint) -> T { + unsafe { do buf_as_slice( + transmute::<*RGBA, *T>( + to_unsafe_ptr(&self)), 4) |slice| { slice[i] } + } + } +} + pub impl RGBA: Color { #[inline(always)] pure fn inverse(&self) -> RGBA { @@ -378,14 +382,6 @@ pub impl HSV { } pub impl HSV: Dimensional { - #[inline(always)] - pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*HSV, *T>( - to_unsafe_ptr(&self)), 3) |slice| { slice[i] } - } - } - #[inline(always)] static pure fn dim() -> uint { 3 } @@ -400,6 +396,16 @@ pub impl HSV: Dimensional { } } +pub impl HSV: Index { + #[inline(always)] + pure fn index(i: uint) -> T { + unsafe { do buf_as_slice( + transmute::<*HSV, *T>( + to_unsafe_ptr(&self)), 3) |slice| { slice[i] } + } + } +} + pub impl HSV: Color { #[inline(always)] pure fn inverse(&self) -> HSV { @@ -473,14 +479,6 @@ pub impl HSVA { } pub impl HSVA: Dimensional { - #[inline(always)] - pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*HSVA, *T>( - to_unsafe_ptr(&self)), 4) |slice| { slice[i] } - } - } - #[inline(always)] static pure fn dim() -> uint { 4 } @@ -495,6 +493,16 @@ pub impl HSVA: Dimensional { } } +pub impl HSVA: Index { + #[inline(always)] + pure fn index(i: uint) -> T { + unsafe { do buf_as_slice( + transmute::<*HSVA, *T>( + to_unsafe_ptr(&self)), 4) |slice| { slice[i] } + } + } +} + pub impl HSVA: Color { #[inline(always)] pure fn inverse(&self) -> HSVA { diff --git a/src/mat.rs b/src/mat.rs index 60c3099..54043d8 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -165,12 +165,6 @@ pub impl Mat2 { } pub impl Mat2: Matrix, Vec2> { - #[inline(always)] - static pure fn dim() -> uint { 2 } - - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } - #[inline(always)] static pure fn cols() -> uint { 2 } @@ -185,7 +179,22 @@ pub impl Mat2: Matrix, Vec2> { Vec2::new(self[0][i], self[1][i]) } +} + +pub impl Mat2: Dimensional { + #[inline(always)] + static pure fn dim() -> uint { 2 } + #[inline(always)] + static pure fn size_of() -> uint { size_of::>() } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + self[0].to_ptr() + } +} + +pub impl Mat2: Index> { #[inline(always)] pure fn index(i: uint) -> Vec2 { unsafe { do buf_as_slice( @@ -193,11 +202,6 @@ pub impl Mat2: Matrix, Vec2> { to_unsafe_ptr(&self)), 2) |slice| { slice[i] } } } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - self[0].to_ptr() - } } pub impl Mat2: MatrixNxN> { @@ -215,11 +219,6 @@ pub impl Mat2: NumericMatrix, Vec2> { NumericVector::zero()) } - #[inline(always)] - pure fn neg() -> Mat2 { - Mat2::from_cols(-self[0], -self[1]) - } - #[inline(always)] pure fn mul_t(&self, value: T) -> Mat2 { Mat2::from_cols(self[0].mul_t(value), @@ -245,6 +244,13 @@ pub impl Mat2: NumericMatrix, Vec2> { } } +pub impl Mat2: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat2 { + Mat2::from_cols(-self[0], -self[1]) + } +} + pub impl Mat2: NumericMatrixNxN> { #[inline(always)] static pure fn identity() -> Mat2 { @@ -399,12 +405,6 @@ pub impl Mat3 { } pub impl Mat3: Matrix, Vec3> { - #[inline(always)] - static pure fn dim() -> uint { 3 } - - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } - #[inline(always)] static pure fn cols() -> uint { 3 } @@ -420,7 +420,22 @@ pub impl Mat3: Matrix, Vec3> { self[1][i], self[2][i]) } +} + +pub impl Mat3: Dimensional { + #[inline(always)] + static pure fn dim() -> uint { 3 } + #[inline(always)] + static pure fn size_of() -> uint { size_of::>() } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + self[0].to_ptr() + } +} + +pub impl Mat3: Index> { #[inline(always)] pure fn index(i: uint) -> Vec3 { unsafe { do buf_as_slice( @@ -428,11 +443,6 @@ pub impl Mat3: Matrix, Vec3> { to_unsafe_ptr(&self)), 3) |slice| { slice[i] } } } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - self[0].to_ptr() - } } pub impl Mat3: MatrixNxN> { @@ -457,11 +467,6 @@ pub impl Mat3: NumericMatrix, Vec3> { NumericVector::zero()) } - #[inline(always)] - pure fn neg() -> Mat3 { - Mat3::from_cols(-self[0], -self[1], -self[2]) - } - #[inline(always)] pure fn mul_t(&self, value: T) -> Mat3 { Mat3::from_cols(self[0].mul_t(value), @@ -491,6 +496,13 @@ pub impl Mat3: NumericMatrix, Vec3> { } } +pub impl Mat3: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat3 { + Mat3::from_cols(-self[0], -self[1], -self[2]) + } +} + pub impl Mat3: NumericMatrixNxN> { #[inline(always)] static pure fn identity() -> Mat3 { @@ -712,12 +724,6 @@ pub impl Mat4 { } pub impl Mat4: Matrix, Vec4> { - #[inline(always)] - static pure fn dim() -> uint { 4 } - - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } - #[inline(always)] static pure fn cols() -> uint { 4 } @@ -734,7 +740,22 @@ pub impl Mat4: Matrix, Vec4> { self[2][i], self[3][i]) } +} + +pub impl Mat4: Dimensional { + #[inline(always)] + static pure fn dim() -> uint { 4 } + #[inline(always)] + static pure fn size_of() -> uint { size_of::>() } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + self[0].to_ptr() + } +} + +pub impl Mat4: Index> { #[inline(always)] pure fn index(i: uint) -> Vec4 { unsafe { do buf_as_slice( @@ -742,11 +763,6 @@ pub impl Mat4: Matrix, Vec4> { to_unsafe_ptr(&self)), 4) |slice| { slice[i] } } } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - self[0].to_ptr() - } } pub impl Mat4: MatrixNxN> { @@ -779,11 +795,6 @@ pub impl Mat4: NumericMatrix, Vec4> { NumericVector::zero()) } - #[inline(always)] - pure fn neg() -> Mat4 { - Mat4::from_cols(-self[0], -self[1], -self[2], -self[3]) - } - #[inline(always)] pure fn mul_t(&self, value: T) -> Mat4 { Mat4::from_cols(self[0].mul_t(value), @@ -817,6 +828,13 @@ pub impl Mat4: NumericMatrix, Vec4> { } } +pub impl Mat4: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat4 { + Mat4::from_cols(-self[0], -self[1], -self[2], -self[3]) + } +} + pub impl Mat4: NumericMatrixNxN> { #[inline(always)] static pure fn identity() -> Mat4 { diff --git a/src/num/consts.rs b/src/num/consts.rs index 235efdf..06164da 100644 --- a/src/num/consts.rs +++ b/src/num/consts.rs @@ -69,9 +69,12 @@ pub impl int: IntConsts { #[inline(always)] static pure fn one() -> int { 1 } } -pub impl f32: FloatConsts { +pub impl f32: IntConsts { #[inline(always)] static pure fn zero() -> f32 { 0f32 } #[inline(always)] static pure fn one() -> f32 { 1f32 } +} + +pub impl f32: FloatConsts { #[inline(always)] static pure fn pi() -> f32 { 3.14159265358979323846264338327950288_f32 } #[inline(always)] static pure fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144_f32 } #[inline(always)] static pure fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721_f32 } @@ -87,9 +90,12 @@ pub impl f32: FloatConsts { #[inline(always)] static pure fn ln_10() -> f32 { 2.30258509299404568401799145468436421_f32 } } -pub impl f64: FloatConsts { +pub impl f64: IntConsts { #[inline(always)] static pure fn zero() -> f64 { 0f64 } #[inline(always)] static pure fn one() -> f64 { 1f64 } +} + +pub impl f64: FloatConsts { #[inline(always)] static pure fn pi() -> f64 { 3.14159265358979323846264338327950288_f64 } #[inline(always)] static pure fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144_f64 } #[inline(always)] static pure fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721_f64 } @@ -105,9 +111,12 @@ pub impl f64: FloatConsts { #[inline(always)] static pure fn ln_10() -> f64 { 2.30258509299404568401799145468436421_f64 } } -pub impl float: FloatConsts { +pub impl float: IntConsts { #[inline(always)] static pure fn zero() -> float { 0f } #[inline(always)] static pure fn one() -> float { 1f } +} + +pub impl float: FloatConsts { #[inline(always)] static pure fn pi() -> float { 3.14159265358979323846264338327950288 } #[inline(always)] static pure fn frac_pi_2() -> float { 1.57079632679489661923132169163975144 } #[inline(always)] static pure fn frac_pi_4() -> float { 0.785398163397448309615660845819875721 } diff --git a/src/quat.rs b/src/quat.rs index 64f16de..2256cfe 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -70,6 +70,29 @@ pub impl Quat { } } +pub impl Quat: Dimensional { + #[inline(always)] + static pure fn dim() -> uint { 4 } + + #[inline(always)] + static pure fn size_of() -> uint { size_of::>() } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + to_unsafe_ptr(&self[0]) + } +} + +pub impl Quat: Index { + #[inline(always)] + pure fn index(i: uint) -> T { + unsafe { do buf_as_slice( + transmute::<*Quat, *T>( + to_unsafe_ptr(&self)), 4) |slice| { slice[i] } + } + } +} + pub impl Quat: Quaternion { #[inline(always)] static pure fn identity() -> Quat { @@ -87,22 +110,6 @@ pub impl Quat: Quaternion { NumCast::zero()) } - #[inline(always)] - static pure fn dim() -> uint { 4 } - - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } - - #[inline(always)] - pure fn to_ptr() -> *T { - to_unsafe_ptr(&self[0]) - } - - #[inline(always)] - pure fn neg() -> Quat { - Quat::from_sv(-self.s, -self.v) - } - #[inline(always)] pure fn mul_t(&self, value: T) -> Quat { Quat::new(self[0] * value, @@ -252,13 +259,10 @@ pub impl Quat: Quaternion { } } -pub impl Quat: Index { +pub impl Quat: Neg> { #[inline(always)] - pure fn index(i: uint) -> T { - unsafe { do buf_as_slice( - transmute::<*Quat, *T>( - to_unsafe_ptr(&self)), 4) |slice| { slice[i] } - } + pure fn neg(&self) -> Quat { + Quat::new(-self[0], -self[1], -self[2], -self[3]) } } diff --git a/src/vec.rs b/src/vec.rs index 4954926..11775c8 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -105,13 +105,22 @@ pub impl Vec2: Vector { static pure fn from_value(value: T) -> Vec2 { Vec2::new(value, value) } - +} + +pub impl Vec2: Dimensional { #[inline(always)] static pure fn dim() -> uint { 2 } #[inline(always)] static pure fn size_of() -> uint { size_of::>() } + #[inline(always)] + pure fn to_ptr(&self) -> *T { + ptr::to_unsafe_ptr(&self[0]) + } +} + +pub impl Vec2: Index { #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( @@ -119,11 +128,6 @@ pub impl Vec2: Vector { to_unsafe_ptr(&self)), 2) |slice| { slice[i] } } } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - ptr::to_unsafe_ptr(&self[0]) - } } pub impl Vec2: NumericVector { @@ -139,11 +143,6 @@ pub impl Vec2: NumericVector { NumCast::zero()) } - #[inline(always)] - pure fn neg() -> Vec2 { - Vec2::new(-self[0], -self[1]) - } - #[inline(always)] pure fn mul_t(&self, value: T) -> Vec2 { Vec2::new(self[0] * value, @@ -174,6 +173,13 @@ pub impl Vec2: NumericVector { self[1] * other[1] } } + +pub impl Vec2: Neg> { + #[inline(always)] + pure fn neg(&self) -> Vec2 { + Vec2::new(-self[0], -self[1]) + } +} pub impl Vec2: GeometricVector { #[inline(always)] @@ -259,13 +265,22 @@ pub impl Vec3: Vector { static pure fn from_value(value: T) -> Vec3 { Vec3::new(value, value, value) } - +} + +pub impl Vec3: Dimensional { #[inline(always)] static pure fn dim() -> uint { 3 } #[inline(always)] static pure fn size_of() -> uint { size_of::>() } + #[inline(always)] + pure fn to_ptr(&self) -> *T { + to_unsafe_ptr(&self[0]) + } +} + +pub impl Vec3: Index { #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( @@ -273,11 +288,6 @@ pub impl Vec3: Vector { to_unsafe_ptr(&self)), 3) |slice| { slice[i] } } } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - to_unsafe_ptr(&self[0]) - } } pub impl Vec3: NumericVector { @@ -295,11 +305,6 @@ pub impl Vec3: NumericVector { NumCast::zero()) } - #[inline(always)] - pure fn neg() -> Vec3 { - Vec3::new(-self[0], -self[1], -self[2]) - } - #[inline(always)] pure fn mul_t(&self, value: T) -> Vec3 { Vec3::new(self[0] * value, @@ -336,6 +341,13 @@ pub impl Vec3: NumericVector { } } +pub impl Vec3: Neg> { + #[inline(always)] + pure fn neg(&self) -> Vec3 { + Vec3::new(-self[0], -self[1], -self[2]) + } +} + pub impl Vec3: NumericVector3 { #[inline(always)] pure fn cross(&self, other: &Vec3) -> Vec3 { @@ -431,13 +443,22 @@ pub impl Vec4: Vector { static pure fn from_value(value: T) -> Vec4 { Vec4::new(value, value, value, value) } - +} + +pub impl Vec4: Dimensional { #[inline(always)] static pure fn dim() -> uint { 4 } #[inline(always)] static pure fn size_of() -> uint { size_of::>() } + #[inline(always)] + pure fn to_ptr(&self) -> *T { + to_unsafe_ptr(&self[0]) + } +} + +pub impl Vec4: Index { #[inline(always)] pure fn index(i: uint) -> T { unsafe { do buf_as_slice( @@ -445,11 +466,6 @@ pub impl Vec4: Vector { to_unsafe_ptr(&self)), 4) |slice| { slice[i] } } } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - to_unsafe_ptr(&self[0]) - } } pub impl Vec4: NumericVector { @@ -469,11 +485,6 @@ pub impl Vec4: NumericVector { NumCast::zero()) } - #[inline(always)] - pure fn neg() -> Vec4 { - Vec4::new(-self[0], -self[1], -self[2], -self[3]) - } - #[inline(always)] pure fn mul_t(&self, value: T) -> Vec4 { Vec4::new(self[0] * value, @@ -515,6 +526,13 @@ pub impl Vec4: NumericVector { } } +pub impl Vec4: Neg> { + #[inline(always)] + pure fn neg(&self) -> Vec4 { + Vec4::new(-self[0], -self[1], -self[2], -self[3]) + } +} + pub impl Vec4: GeometricVector { #[inline(always)] pure fn length2(&self) -> T {