diff --git a/src/angle.rs b/src/angle.rs index 91cbc7b..9b6f819 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -80,7 +80,7 @@ pub trait Angle : Clone + Zero + PartialEq + PartialOrd + ApproxEq -+ Neg ++ Neg + ToRad + ToDeg + ScalarConv @@ -181,23 +181,79 @@ Deg { } -impl Add, Rad> for Rad { #[inline] fn add(self, other: Rad) -> Rad { rad(self.s + other.s) } } -impl Add, Deg> for Deg { #[inline] fn add(self, other: Deg) -> Deg { deg(self.s + other.s) } } +impl Add for Rad { + type Output = Rad; -impl Sub, Rad> for Rad { #[inline] fn sub(self, other: Rad) -> Rad { rad(self.s - other.s) } } -impl Sub, Deg> for Deg { #[inline] fn sub(self, other: Deg) -> Deg { deg(self.s - other.s) } } + #[inline] + fn add(self, other: Rad) -> Rad { rad(self.s + other.s) } +} +impl Add for Deg { + type Output = Deg; -impl Neg> for Rad { #[inline] fn neg(self) -> Rad { rad(-self.s) } } -impl Neg> for Deg { #[inline] fn neg(self) -> Deg { deg(-self.s) } } + #[inline] + fn add(self, other: Deg) -> Deg { deg(self.s + other.s) } +} -impl Zero for Rad { #[inline] fn zero() -> Rad { rad(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } } -impl Zero for Deg { #[inline] fn zero() -> Deg { deg(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } } +impl Sub for Rad { + type Output = Rad; -impl Mul, Rad> for Rad { #[inline] fn mul(self, other: Rad) -> Rad { rad(self.s * other.s) } } -impl Mul, Deg> for Deg { #[inline] fn mul(self, other: Deg) -> Deg { deg(self.s * other.s) } } + #[inline] + fn sub(self, other: Rad) -> Rad { rad(self.s - other.s) } +} +impl Sub for Deg { + type Output = Deg; -impl One for Rad { #[inline] fn one() -> Rad { rad(one()) } } -impl One for Deg { #[inline] fn one() -> Deg { deg(one()) } } + #[inline] + fn sub(self, other: Deg) -> Deg { deg(self.s - other.s) } +} + +impl Neg for Rad { + type Output = Rad; + + #[inline] + fn neg(self) -> Rad { rad(-self.s) } +} +impl Neg for Deg { + type Output = Deg; + + #[inline] + fn neg(self) -> Deg { deg(-self.s) } +} + +impl Zero for Rad { + #[inline] + fn zero() -> Rad { rad(zero()) } + #[inline] + fn is_zero(&self) -> bool { *self == zero() } +} +impl Zero for Deg { + #[inline] + fn zero() -> Deg { deg(zero()) } + #[inline] + fn is_zero(&self) -> bool { *self == zero() } +} + +impl Mul for Rad { + type Output = Rad; + + #[inline] + fn mul(self, other: Rad) -> Rad { rad(self.s * other.s) } +} +impl Mul for Deg { + type Output = Deg; + + #[inline] + fn mul(self, other: Deg) -> Deg { deg(self.s * other.s) } +} + +impl One for Rad { + #[inline] + fn one() -> Rad { rad(one()) } +} +impl One for Deg { + #[inline] + fn one() -> Deg { deg(one()) } +} impl Angle for Rad { diff --git a/src/array.rs b/src/array.rs index 9322a52..2fefbaf 100644 --- a/src/array.rs +++ b/src/array.rs @@ -18,7 +18,7 @@ use std::ptr; use std::ops::*; /// An array containing elements of type `Element` -pub trait Array1: Index + IndexMut { +pub trait Array1: Index + IndexMut { /// Get the pointer to the first element of the array. fn ptr<'a>(&'a self) -> &'a Element { &(*self)[0] @@ -48,7 +48,7 @@ pub trait Array1: Index + IndexMut /// A column-major array pub trait Array2+'static, Row: Array1, Element: Copy>: - Index + IndexMut { + Index + IndexMut { /// Get the pointer to the first element of the array. fn ptr<'a>(&'a self) -> &'a Element { &(*self)[0][0] diff --git a/src/cgmath.rs b/src/cgmath.rs index b6260e3..5b5f816 100644 --- a/src/cgmath.rs +++ b/src/cgmath.rs @@ -19,6 +19,7 @@ #![feature(globs)] #![feature(macro_rules)] #![feature(old_orphan_check)] +#![feature(associated_types)] //! Computer graphics-centric math. //! diff --git a/src/matrix.rs b/src/matrix.rs index 9b0a73b..10785dd 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -286,7 +286,7 @@ Matrix4 { } pub trait Matrix>: Array2 - + Neg + + Neg + Zero + One + ApproxEq + Sized { @@ -371,29 +371,122 @@ pub trait Matrix>: Array2 fn is_symmetric(&self) -> bool; } -impl Add, Matrix2> for Matrix2 { #[inline] fn add(self, other: Matrix2) -> Matrix2 { self.add_m(&other) } } -impl Add, Matrix3> for Matrix3 { #[inline] fn add(self, other: Matrix3) -> Matrix3 { self.add_m(&other) } } -impl Add, Matrix4> for Matrix4 { #[inline] fn add(self, other: Matrix4) -> Matrix4 { self.add_m(&other) } } +impl Add for Matrix2 { + type Output = Matrix2; -impl Sub, Matrix2> for Matrix2 { #[inline] fn sub(self, other: Matrix2) -> Matrix2 { self.sub_m(&other) } } -impl Sub, Matrix3> for Matrix3 { #[inline] fn sub(self, other: Matrix3) -> Matrix3 { self.sub_m(&other) } } -impl Sub, Matrix4> for Matrix4 { #[inline] fn sub(self, other: Matrix4) -> Matrix4 { self.sub_m(&other) } } + #[inline] + fn add(self, other: Matrix2) -> Matrix2 { self.add_m(&other) } +} -impl Neg> for Matrix2 { #[inline] fn neg(self) -> Matrix2 { Matrix2::from_cols(self[0].neg(), self[1].neg()) } } -impl Neg> for Matrix3 { #[inline] fn neg(self) -> Matrix3 { Matrix3::from_cols(self[0].neg(), self[1].neg(), self[2].neg()) } } -impl Neg> for Matrix4 { #[inline] fn neg(self) -> Matrix4 { Matrix4::from_cols(self[0].neg(), self[1].neg(), self[2].neg(), self[3].neg()) } } +impl Add for Matrix3 { + type Output = Matrix3; -impl Zero for Matrix2 { #[inline] fn zero() -> Matrix2 { Matrix2::zero() } #[inline] fn is_zero(&self) -> bool{ *self == zero() } } -impl Zero for Matrix3 { #[inline] fn zero() -> Matrix3 { Matrix3::zero() } #[inline] fn is_zero(&self) -> bool{ *self == zero() } } -impl Zero for Matrix4 { #[inline] fn zero() -> Matrix4 { Matrix4::zero() } #[inline] fn is_zero(&self) -> bool{ *self == zero() } } + #[inline] + fn add(self, other: Matrix3) -> Matrix3 { self.add_m(&other) } +} -impl Mul, Matrix2> for Matrix2 { #[inline] fn mul(self, other: Matrix2) -> Matrix2 { self.mul_m(&other) } } -impl Mul, Matrix3> for Matrix3 { #[inline] fn mul(self, other: Matrix3) -> Matrix3 { self.mul_m(&other) } } -impl Mul, Matrix4> for Matrix4 { #[inline] fn mul(self, other: Matrix4) -> Matrix4 { self.mul_m(&other) } } +impl Add for Matrix4 { + type Output = Matrix4; -impl One for Matrix2 { #[inline] fn one() -> Matrix2 { Matrix2::identity() } } -impl One for Matrix3 { #[inline] fn one() -> Matrix3 { Matrix3::identity() } } -impl One for Matrix4 { #[inline] fn one() -> Matrix4 { Matrix4::identity() } } + #[inline] + fn add(self, other: Matrix4) -> Matrix4 { self.add_m(&other) } +} + +impl Sub for Matrix2 { + type Output = Matrix2; + + #[inline] + fn sub(self, other: Matrix2) -> Matrix2 { self.sub_m(&other) } +} + +impl Sub for Matrix3 { + type Output = Matrix3; + + #[inline] + fn sub(self, other: Matrix3) -> Matrix3 { self.sub_m(&other) } +} + +impl Sub for Matrix4 { + type Output = Matrix4; + + #[inline] + fn sub(self, other: Matrix4) -> Matrix4 { self.sub_m(&other) } +} + +impl Neg for Matrix2 { + type Output = Matrix2; + + #[inline] + fn neg(self) -> Matrix2 { Matrix2::from_cols(self[0].neg(), self[1].neg()) } +} + +impl Neg for Matrix3 { + type Output = Matrix3; + + #[inline] + fn neg(self) -> Matrix3 { Matrix3::from_cols(self[0].neg(), self[1].neg(), self[2].neg()) } +} + +impl Neg for Matrix4 { + type Output = Matrix4; + + #[inline] + fn neg(self) -> Matrix4 { Matrix4::from_cols(self[0].neg(), self[1].neg(), self[2].neg(), self[3].neg()) } +} + +impl Zero for Matrix2 { + #[inline] + fn zero() -> Matrix2 { Matrix2::zero() } + #[inline] + fn is_zero(&self) -> bool{ *self == zero() } +} + +impl Zero for Matrix3 { + #[inline] + fn zero() -> Matrix3 { Matrix3::zero() } + #[inline] + fn is_zero(&self) -> bool{ *self == zero() } +} + +impl Zero for Matrix4 { + #[inline] + fn zero() -> Matrix4 { Matrix4::zero() } + #[inline] + fn is_zero(&self) -> bool{ *self == zero() } +} + +impl Mul for Matrix2 { + type Output = Matrix2; + + #[inline] + fn mul(self, other: Matrix2) -> Matrix2 { self.mul_m(&other) } +} + +impl Mul for Matrix3 { + type Output = Matrix3; + + #[inline] + fn mul(self, other: Matrix3) -> Matrix3 { self.mul_m(&other) } +} + +impl Mul for Matrix4 { + type Output = Matrix4; + + #[inline] + fn mul(self, other: Matrix4) -> Matrix4 { self.mul_m(&other) } +} + +impl One for Matrix2 { + #[inline] + fn one() -> Matrix2 { Matrix2::identity() } +} +impl One for Matrix3 { + #[inline] + fn one() -> Matrix3 { Matrix3::identity() } +} +impl One for Matrix4 { + #[inline] fn one() -> Matrix4 { Matrix4::identity() } +} impl FixedArray<[[S; 2]; 2]> for Matrix2 { #[inline] @@ -438,14 +531,18 @@ impl FixedArray<[[S; 2]; 2]> for Matrix2 { } } -impl Index> for Matrix2 { +impl Index for Matrix2 { + type Output = Vector2; + #[inline] fn index<'a>(&'a self, i: &uint) -> &'a Vector2 { FixedArray::from_fixed_ref(&self.as_fixed()[*i]) } } -impl IndexMut> for Matrix2 { +impl IndexMut for Matrix2 { + type Output = Vector2; + #[inline] fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut Vector2 { FixedArray::from_fixed_mut(&mut self.as_mut_fixed()[*i]) @@ -518,14 +615,18 @@ impl FixedArray<[[S; 3]; 3]> for Matrix3 { } } -impl Index> for Matrix3 { +impl Index for Matrix3 { + type Output = Vector3; + #[inline] fn index<'a>(&'a self, i: &uint) -> &'a Vector3 { FixedArray::from_fixed_ref(&self.as_fixed()[*i]) } } -impl IndexMut> for Matrix3 { +impl IndexMut for Matrix3 { + type Output = Vector3; + #[inline] fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut Vector3 { FixedArray::from_fixed_mut(&mut self.as_mut_fixed()[*i]) @@ -603,14 +704,18 @@ impl FixedArray<[[S; 4]; 4]> for Matrix4 { } } -impl Index> for Matrix4 { +impl Index for Matrix4 { + type Output = Vector4; + #[inline] fn index<'a>(&'a self, i: &uint) -> &'a Vector4 { FixedArray::from_fixed_ref(&self.as_fixed()[*i]) } } -impl IndexMut> for Matrix4 { +impl IndexMut for Matrix4 { + type Output = Vector4; + #[inline] fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut Vector4 { FixedArray::from_fixed_mut(&mut self.as_mut_fixed()[*i]) diff --git a/src/num.rs b/src/num.rs index fe2d3d2..000f955 100644 --- a/src/num.rs +++ b/src/num.rs @@ -71,8 +71,8 @@ pub trait One { /// Base numeric types with partial ordering pub trait BaseNum: - Copy + NumCast + Clone + Add + Sub + - Mul + Div + Rem + Neg + PartialEq + Copy + NumCast + Clone + Add + Sub + + Mul + Div + Rem + Neg + PartialEq + PartialOrd + cmp::PartialOrd + fmt::Show + Zero + One {} diff --git a/src/point.rs b/src/point.rs index 4114860..0ad094f 100644 --- a/src/point.rs +++ b/src/point.rs @@ -135,14 +135,16 @@ impl FixedArray<[S; 2]> for Point2 { } } -impl Index for Point2 { +impl Index for Point2 { + type Output = S; #[inline] fn index<'a>(&'a self, i: &uint) -> &'a S { &self.as_fixed()[*i] } } -impl IndexMut for Point2 { +impl IndexMut for Point2 { + type Output = S; #[inline] fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut S { &mut self.as_mut_fixed()[*i] @@ -289,14 +291,18 @@ impl FixedArray<[S; 3]> for Point3 { } } -impl Index for Point3 { +impl Index for Point3 { + type Output = S; + #[inline] fn index<'a>(&'a self, i: &uint) -> &'a S { &self.as_fixed()[*i] } } -impl IndexMut for Point3 { +impl IndexMut for Point3 { + type Output = S; + #[inline] fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut S { &mut self.as_mut_fixed()[*i] diff --git a/src/quaternion.rs b/src/quaternion.rs index b1eaa77..abd7ed4 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -50,7 +50,9 @@ impl Array1 for Quaternion { } } -impl Index for Quaternion { +impl Index for Quaternion { + type Output = S; + #[inline] fn index<'a>(&'a self, i: &uint) -> &'a S { let slice: &[S; 4] = unsafe { mem::transmute(self) }; @@ -58,7 +60,9 @@ impl Index for Quaternion { } } -impl IndexMut for Quaternion { +impl IndexMut for Quaternion { + type Output = S; + #[inline] fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut S { let slice: &'a mut [S; 4] = unsafe { mem::transmute(self) }; @@ -357,7 +361,9 @@ impl ToMatrix4 for Quaternion { } } -impl Neg> for Quaternion { +impl Neg for Quaternion { + type Output = Quaternion; + #[inline] fn neg(self) -> Quaternion { Quaternion::from_sv(-self.s, -self.v) diff --git a/src/vector.rs b/src/vector.rs index 75c8cd2..eab3cf3 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -109,7 +109,7 @@ use num::{BaseNum, BaseFloat, Zero, One, zero, one}; /// A trait that specifies a range of numeric operations for vectors. Not all /// of these make sense from a linear algebra point of view, but are included /// for pragmatic reasons. -pub trait Vector: Array1 + Zero + One + Neg { +pub trait Vector: Array1 + Zero + One + Neg { /// Add a scalar to this vector, returning a new vector. fn add_s(&self, s: S) -> Self; /// Subtract a scalar from this vector, returning a new vector. @@ -251,14 +251,18 @@ macro_rules! vec( } } - impl<$S: Copy> Index for $Self<$S> { + impl<$S: Copy> Index for $Self<$S> { + type Output = S; + #[inline] fn index<'a>(&'a self, i: &uint) -> &'a $S { &self.as_fixed()[*i] } } - impl<$S: Copy> IndexMut for $Self<$S> { + impl<$S: Copy> IndexMut for $Self<$S> { + type Output = S; + #[inline] fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut $S { &mut self.as_mut_fixed()[*i] @@ -305,28 +309,46 @@ macro_rules! vec( #[inline] fn comp_max(&self) -> S { fold!(partial_max, { $(self.$field),+ }) } } - impl Add<$Self, $Self> for $Self { - #[inline] fn add(self, v: $Self) -> $Self { self.add_v(&v) } + impl Add for $Self { + type Output = $Self; + + #[inline] + fn add(self, v: $Self) -> $Self { self.add_v(&v) } } - impl Sub<$Self, $Self> for $Self { - #[inline] fn sub(self, v: $Self) -> $Self { self.sub_v(&v) } + impl Sub for $Self { + type Output = $Self; + + #[inline] + fn sub(self, v: $Self) -> $Self { self.sub_v(&v) } } - impl Neg<$Self> for $Self { - #[inline] fn neg(self) -> $Self { $Self::new($(-self.$field),+) } + impl Neg for $Self { + type Output = $Self; + + #[inline] + fn neg(self) -> $Self { $Self::new($(-self.$field),+) } } - impl Mul<$Self, $Self> for $Self { - #[inline] fn mul(self, v: $Self) -> $Self { self.mul_v(&v) } + impl Mul for $Self { + type Output = $Self; + + #[inline] + fn mul(self, v: $Self) -> $Self { self.mul_v(&v) } } - impl Div<$Self, $Self> for $Self { - #[inline] fn div(self, v: $Self) -> $Self { self.div_v(&v) } + impl Div for $Self { + type Output = $Self; + + #[inline] + fn div(self, v: $Self) -> $Self { self.div_v(&v) } } - impl Rem<$Self, $Self> for $Self { - #[inline] fn rem(self, v: $Self) -> $Self { self.rem_v(&v) } + impl Rem for $Self { + type Output = $Self; + + #[inline] + fn rem(self, v: $Self) -> $Self { self.rem_v(&v) } } impl ApproxEq for $Self {