Update for associated types changes

This commit is contained in:
Colin Sherratt 2015-01-04 18:56:01 -07:00
parent a6ec222e7f
commit d6bb64fd65
8 changed files with 260 additions and 64 deletions

View file

@ -80,7 +80,7 @@ pub trait Angle
: Clone + Zero
+ PartialEq + PartialOrd
+ ApproxEq<S>
+ Neg<Self>
+ Neg<Output=Self>
+ ToRad<S>
+ ToDeg<S>
+ ScalarConv<S>
@ -181,23 +181,79 @@ Deg<S> {
}
impl<S: BaseFloat> Add<Rad<S>, Rad<S>> for Rad<S> { #[inline] fn add(self, other: Rad<S>) -> Rad<S> { rad(self.s + other.s) } }
impl<S: BaseFloat> Add<Deg<S>, Deg<S>> for Deg<S> { #[inline] fn add(self, other: Deg<S>) -> Deg<S> { deg(self.s + other.s) } }
impl<S: BaseFloat> Add for Rad<S> {
type Output = Rad<S>;
impl<S: BaseFloat> Sub<Rad<S>, Rad<S>> for Rad<S> { #[inline] fn sub(self, other: Rad<S>) -> Rad<S> { rad(self.s - other.s) } }
impl<S: BaseFloat> Sub<Deg<S>, Deg<S>> for Deg<S> { #[inline] fn sub(self, other: Deg<S>) -> Deg<S> { deg(self.s - other.s) } }
#[inline]
fn add(self, other: Rad<S>) -> Rad<S> { rad(self.s + other.s) }
}
impl<S: BaseFloat> Add for Deg<S> {
type Output = Deg<S>;
impl<S: BaseFloat> Neg<Rad<S>> for Rad<S> { #[inline] fn neg(self) -> Rad<S> { rad(-self.s) } }
impl<S: BaseFloat> Neg<Deg<S>> for Deg<S> { #[inline] fn neg(self) -> Deg<S> { deg(-self.s) } }
#[inline]
fn add(self, other: Deg<S>) -> Deg<S> { deg(self.s + other.s) }
}
impl<S: BaseFloat> Zero for Rad<S> { #[inline] fn zero() -> Rad<S> { rad(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } }
impl<S: BaseFloat> Zero for Deg<S> { #[inline] fn zero() -> Deg<S> { deg(zero()) } #[inline] fn is_zero(&self) -> bool { *self == zero() } }
impl<S: BaseFloat> Sub for Rad<S> {
type Output = Rad<S>;
impl<S: BaseFloat> Mul<Rad<S>, Rad<S>> for Rad<S> { #[inline] fn mul(self, other: Rad<S>) -> Rad<S> { rad(self.s * other.s) } }
impl<S: BaseFloat> Mul<Deg<S>, Deg<S>> for Deg<S> { #[inline] fn mul(self, other: Deg<S>) -> Deg<S> { deg(self.s * other.s) } }
#[inline]
fn sub(self, other: Rad<S>) -> Rad<S> { rad(self.s - other.s) }
}
impl<S: BaseFloat> Sub for Deg<S> {
type Output = Deg<S>;
impl<S: BaseFloat> One for Rad<S> { #[inline] fn one() -> Rad<S> { rad(one()) } }
impl<S: BaseFloat> One for Deg<S> { #[inline] fn one() -> Deg<S> { deg(one()) } }
#[inline]
fn sub(self, other: Deg<S>) -> Deg<S> { deg(self.s - other.s) }
}
impl<S: BaseFloat> Neg for Rad<S> {
type Output = Rad<S>;
#[inline]
fn neg(self) -> Rad<S> { rad(-self.s) }
}
impl<S: BaseFloat> Neg for Deg<S> {
type Output = Deg<S>;
#[inline]
fn neg(self) -> Deg<S> { deg(-self.s) }
}
impl<S: BaseFloat> Zero for Rad<S> {
#[inline]
fn zero() -> Rad<S> { rad(zero()) }
#[inline]
fn is_zero(&self) -> bool { *self == zero() }
}
impl<S: BaseFloat> Zero for Deg<S> {
#[inline]
fn zero() -> Deg<S> { deg(zero()) }
#[inline]
fn is_zero(&self) -> bool { *self == zero() }
}
impl<S: BaseFloat> Mul for Rad<S> {
type Output = Rad<S>;
#[inline]
fn mul(self, other: Rad<S>) -> Rad<S> { rad(self.s * other.s) }
}
impl<S: BaseFloat> Mul for Deg<S> {
type Output = Deg<S>;
#[inline]
fn mul(self, other: Deg<S>) -> Deg<S> { deg(self.s * other.s) }
}
impl<S: BaseFloat> One for Rad<S> {
#[inline]
fn one() -> Rad<S> { rad(one()) }
}
impl<S: BaseFloat> One for Deg<S> {
#[inline]
fn one() -> Deg<S> { deg(one()) }
}
impl<S: BaseFloat>
Angle<S> for Rad<S> {

View file

@ -18,7 +18,7 @@ use std::ptr;
use std::ops::*;
/// An array containing elements of type `Element`
pub trait Array1<Element: Copy>: Index<uint, Element> + IndexMut<uint, Element> {
pub trait Array1<Element: Copy>: Index<uint, Output=Element> + IndexMut<uint, Output=Element> {
/// 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<Element: Copy>: Index<uint, Element> + IndexMut<uint, Element>
/// A column-major array
pub trait Array2<Column: Array1<Element>+'static, Row: Array1<Element>, Element: Copy>:
Index<uint, Column> + IndexMut<uint, Column> {
Index<uint, Output=Column> + IndexMut<uint, Output=Column> {
/// Get the pointer to the first element of the array.
fn ptr<'a>(&'a self) -> &'a Element {
&(*self)[0][0]

View file

@ -19,6 +19,7 @@
#![feature(globs)]
#![feature(macro_rules)]
#![feature(old_orphan_check)]
#![feature(associated_types)]
//! Computer graphics-centric math.
//!

View file

@ -286,7 +286,7 @@ Matrix4<S> {
}
pub trait Matrix<S: BaseFloat, V: Clone + Vector<S>>: Array2<V, V, S>
+ Neg<Self>
+ Neg
+ Zero + One
+ ApproxEq<S>
+ Sized {
@ -371,29 +371,122 @@ pub trait Matrix<S: BaseFloat, V: Clone + Vector<S>>: Array2<V, V, S>
fn is_symmetric(&self) -> bool;
}
impl<S: BaseFloat + 'static> Add<Matrix2<S>, Matrix2<S>> for Matrix2<S> { #[inline] fn add(self, other: Matrix2<S>) -> Matrix2<S> { self.add_m(&other) } }
impl<S: BaseFloat + 'static> Add<Matrix3<S>, Matrix3<S>> for Matrix3<S> { #[inline] fn add(self, other: Matrix3<S>) -> Matrix3<S> { self.add_m(&other) } }
impl<S: BaseFloat + 'static> Add<Matrix4<S>, Matrix4<S>> for Matrix4<S> { #[inline] fn add(self, other: Matrix4<S>) -> Matrix4<S> { self.add_m(&other) } }
impl<S: BaseFloat + 'static> Add for Matrix2<S> {
type Output = Matrix2<S>;
impl<S: BaseFloat + 'static> Sub<Matrix2<S>, Matrix2<S>> for Matrix2<S> { #[inline] fn sub(self, other: Matrix2<S>) -> Matrix2<S> { self.sub_m(&other) } }
impl<S: BaseFloat + 'static> Sub<Matrix3<S>, Matrix3<S>> for Matrix3<S> { #[inline] fn sub(self, other: Matrix3<S>) -> Matrix3<S> { self.sub_m(&other) } }
impl<S: BaseFloat + 'static> Sub<Matrix4<S>, Matrix4<S>> for Matrix4<S> { #[inline] fn sub(self, other: Matrix4<S>) -> Matrix4<S> { self.sub_m(&other) } }
#[inline]
fn add(self, other: Matrix2<S>) -> Matrix2<S> { self.add_m(&other) }
}
impl<S: BaseFloat> Neg<Matrix2<S>> for Matrix2<S> { #[inline] fn neg(self) -> Matrix2<S> { Matrix2::from_cols(self[0].neg(), self[1].neg()) } }
impl<S: BaseFloat> Neg<Matrix3<S>> for Matrix3<S> { #[inline] fn neg(self) -> Matrix3<S> { Matrix3::from_cols(self[0].neg(), self[1].neg(), self[2].neg()) } }
impl<S: BaseFloat> Neg<Matrix4<S>> for Matrix4<S> { #[inline] fn neg(self) -> Matrix4<S> { Matrix4::from_cols(self[0].neg(), self[1].neg(), self[2].neg(), self[3].neg()) } }
impl<S: BaseFloat + 'static> Add for Matrix3<S> {
type Output = Matrix3<S>;
impl<S: BaseFloat> Zero for Matrix2<S> { #[inline] fn zero() -> Matrix2<S> { Matrix2::zero() } #[inline] fn is_zero(&self) -> bool{ *self == zero() } }
impl<S: BaseFloat> Zero for Matrix3<S> { #[inline] fn zero() -> Matrix3<S> { Matrix3::zero() } #[inline] fn is_zero(&self) -> bool{ *self == zero() } }
impl<S: BaseFloat> Zero for Matrix4<S> { #[inline] fn zero() -> Matrix4<S> { Matrix4::zero() } #[inline] fn is_zero(&self) -> bool{ *self == zero() } }
#[inline]
fn add(self, other: Matrix3<S>) -> Matrix3<S> { self.add_m(&other) }
}
impl<S: BaseFloat + 'static> Mul<Matrix2<S>, Matrix2<S>> for Matrix2<S> { #[inline] fn mul(self, other: Matrix2<S>) -> Matrix2<S> { self.mul_m(&other) } }
impl<S: BaseFloat + 'static> Mul<Matrix3<S>, Matrix3<S>> for Matrix3<S> { #[inline] fn mul(self, other: Matrix3<S>) -> Matrix3<S> { self.mul_m(&other) } }
impl<S: BaseFloat + 'static> Mul<Matrix4<S>, Matrix4<S>> for Matrix4<S> { #[inline] fn mul(self, other: Matrix4<S>) -> Matrix4<S> { self.mul_m(&other) } }
impl<S: BaseFloat + 'static> Add for Matrix4<S> {
type Output = Matrix4<S>;
impl<S: BaseFloat> One for Matrix2<S> { #[inline] fn one() -> Matrix2<S> { Matrix2::identity() } }
impl<S: BaseFloat> One for Matrix3<S> { #[inline] fn one() -> Matrix3<S> { Matrix3::identity() } }
impl<S: BaseFloat> One for Matrix4<S> { #[inline] fn one() -> Matrix4<S> { Matrix4::identity() } }
#[inline]
fn add(self, other: Matrix4<S>) -> Matrix4<S> { self.add_m(&other) }
}
impl<S: BaseFloat + 'static> Sub for Matrix2<S> {
type Output = Matrix2<S>;
#[inline]
fn sub(self, other: Matrix2<S>) -> Matrix2<S> { self.sub_m(&other) }
}
impl<S: BaseFloat + 'static> Sub for Matrix3<S> {
type Output = Matrix3<S>;
#[inline]
fn sub(self, other: Matrix3<S>) -> Matrix3<S> { self.sub_m(&other) }
}
impl<S: BaseFloat + 'static> Sub for Matrix4<S> {
type Output = Matrix4<S>;
#[inline]
fn sub(self, other: Matrix4<S>) -> Matrix4<S> { self.sub_m(&other) }
}
impl<S: BaseFloat> Neg for Matrix2<S> {
type Output = Matrix2<S>;
#[inline]
fn neg(self) -> Matrix2<S> { Matrix2::from_cols(self[0].neg(), self[1].neg()) }
}
impl<S: BaseFloat> Neg for Matrix3<S> {
type Output = Matrix3<S>;
#[inline]
fn neg(self) -> Matrix3<S> { Matrix3::from_cols(self[0].neg(), self[1].neg(), self[2].neg()) }
}
impl<S: BaseFloat> Neg for Matrix4<S> {
type Output = Matrix4<S>;
#[inline]
fn neg(self) -> Matrix4<S> { Matrix4::from_cols(self[0].neg(), self[1].neg(), self[2].neg(), self[3].neg()) }
}
impl<S: BaseFloat> Zero for Matrix2<S> {
#[inline]
fn zero() -> Matrix2<S> { Matrix2::zero() }
#[inline]
fn is_zero(&self) -> bool{ *self == zero() }
}
impl<S: BaseFloat> Zero for Matrix3<S> {
#[inline]
fn zero() -> Matrix3<S> { Matrix3::zero() }
#[inline]
fn is_zero(&self) -> bool{ *self == zero() }
}
impl<S: BaseFloat> Zero for Matrix4<S> {
#[inline]
fn zero() -> Matrix4<S> { Matrix4::zero() }
#[inline]
fn is_zero(&self) -> bool{ *self == zero() }
}
impl<S: BaseFloat + 'static> Mul for Matrix2<S> {
type Output = Matrix2<S>;
#[inline]
fn mul(self, other: Matrix2<S>) -> Matrix2<S> { self.mul_m(&other) }
}
impl<S: BaseFloat + 'static> Mul for Matrix3<S> {
type Output = Matrix3<S>;
#[inline]
fn mul(self, other: Matrix3<S>) -> Matrix3<S> { self.mul_m(&other) }
}
impl<S: BaseFloat + 'static> Mul for Matrix4<S> {
type Output = Matrix4<S>;
#[inline]
fn mul(self, other: Matrix4<S>) -> Matrix4<S> { self.mul_m(&other) }
}
impl<S: BaseFloat> One for Matrix2<S> {
#[inline]
fn one() -> Matrix2<S> { Matrix2::identity() }
}
impl<S: BaseFloat> One for Matrix3<S> {
#[inline]
fn one() -> Matrix3<S> { Matrix3::identity() }
}
impl<S: BaseFloat> One for Matrix4<S> {
#[inline] fn one() -> Matrix4<S> { Matrix4::identity() }
}
impl<S> FixedArray<[[S; 2]; 2]> for Matrix2<S> {
#[inline]
@ -438,14 +531,18 @@ impl<S> FixedArray<[[S; 2]; 2]> for Matrix2<S> {
}
}
impl<S> Index<uint, Vector2<S>> for Matrix2<S> {
impl<S> Index<uint> for Matrix2<S> {
type Output = Vector2<S>;
#[inline]
fn index<'a>(&'a self, i: &uint) -> &'a Vector2<S> {
FixedArray::from_fixed_ref(&self.as_fixed()[*i])
}
}
impl<S> IndexMut<uint, Vector2<S>> for Matrix2<S> {
impl<S> IndexMut<uint> for Matrix2<S> {
type Output = Vector2<S>;
#[inline]
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut Vector2<S> {
FixedArray::from_fixed_mut(&mut self.as_mut_fixed()[*i])
@ -518,14 +615,18 @@ impl<S> FixedArray<[[S; 3]; 3]> for Matrix3<S> {
}
}
impl<S> Index<uint, Vector3<S>> for Matrix3<S> {
impl<S> Index<uint> for Matrix3<S> {
type Output = Vector3<S>;
#[inline]
fn index<'a>(&'a self, i: &uint) -> &'a Vector3<S> {
FixedArray::from_fixed_ref(&self.as_fixed()[*i])
}
}
impl<S> IndexMut<uint, Vector3<S>> for Matrix3<S> {
impl<S> IndexMut<uint> for Matrix3<S> {
type Output = Vector3<S>;
#[inline]
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut Vector3<S> {
FixedArray::from_fixed_mut(&mut self.as_mut_fixed()[*i])
@ -603,14 +704,18 @@ impl<S> FixedArray<[[S; 4]; 4]> for Matrix4<S> {
}
}
impl<S> Index<uint, Vector4<S>> for Matrix4<S> {
impl<S> Index<uint> for Matrix4<S> {
type Output = Vector4<S>;
#[inline]
fn index<'a>(&'a self, i: &uint) -> &'a Vector4<S> {
FixedArray::from_fixed_ref(&self.as_fixed()[*i])
}
}
impl<S> IndexMut<uint, Vector4<S>> for Matrix4<S> {
impl<S> IndexMut<uint> for Matrix4<S> {
type Output = Vector4<S>;
#[inline]
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut Vector4<S> {
FixedArray::from_fixed_mut(&mut self.as_mut_fixed()[*i])

View file

@ -71,8 +71,8 @@ pub trait One {
/// Base numeric types with partial ordering
pub trait BaseNum:
Copy + NumCast + Clone + Add<Self, Self> + Sub<Self, Self> +
Mul<Self, Self> + Div<Self, Self> + Rem<Self, Self> + Neg<Self> + PartialEq
Copy + NumCast + Clone + Add<Output=Self> + Sub<Output=Self> +
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> + Neg<Output=Self> + PartialEq
+ PartialOrd + cmp::PartialOrd + fmt::Show + Zero + One
{}

View file

@ -135,14 +135,16 @@ impl<S> FixedArray<[S; 2]> for Point2<S> {
}
}
impl<S: BaseNum> Index<uint, S> for Point2<S> {
impl<S: BaseNum> Index<uint> for Point2<S> {
type Output = S;
#[inline]
fn index<'a>(&'a self, i: &uint) -> &'a S {
&self.as_fixed()[*i]
}
}
impl<S: BaseNum> IndexMut<uint, S> for Point2<S> {
impl<S: BaseNum> IndexMut<uint> for Point2<S> {
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<S> FixedArray<[S; 3]> for Point3<S> {
}
}
impl<S: BaseNum> Index<uint, S> for Point3<S> {
impl<S: BaseNum> Index<uint> for Point3<S> {
type Output = S;
#[inline]
fn index<'a>(&'a self, i: &uint) -> &'a S {
&self.as_fixed()[*i]
}
}
impl<S: BaseNum> IndexMut<uint, S> for Point3<S> {
impl<S: BaseNum> IndexMut<uint> for Point3<S> {
type Output = S;
#[inline]
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut S {
&mut self.as_mut_fixed()[*i]

View file

@ -50,7 +50,9 @@ impl<S: Copy + BaseFloat> Array1<S> for Quaternion<S> {
}
}
impl<S: BaseFloat> Index<uint, S> for Quaternion<S> {
impl<S: BaseFloat> Index<uint> for Quaternion<S> {
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<S: BaseFloat> Index<uint, S> for Quaternion<S> {
}
}
impl<S: BaseFloat> IndexMut<uint, S> for Quaternion<S> {
impl<S: BaseFloat> IndexMut<uint> for Quaternion<S> {
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<S: BaseFloat> ToMatrix4<S> for Quaternion<S> {
}
}
impl<S: BaseFloat> Neg<Quaternion<S>> for Quaternion<S> {
impl<S: BaseFloat> Neg for Quaternion<S> {
type Output = Quaternion<S>;
#[inline]
fn neg(self) -> Quaternion<S> {
Quaternion::from_sv(-self.s, -self.v)

View file

@ -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<S: BaseNum>: Array1<S> + Zero + One + Neg<Self> {
pub trait Vector<S: BaseNum>: Array1<S> + Zero + One + Neg<Output=Self> {
/// 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<uint, S> for $Self<$S> {
impl<$S: Copy> Index<uint> for $Self<$S> {
type Output = S;
#[inline]
fn index<'a>(&'a self, i: &uint) -> &'a $S {
&self.as_fixed()[*i]
}
}
impl<$S: Copy> IndexMut<uint, S> for $Self<$S> {
impl<$S: Copy> IndexMut<uint> 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<S: BaseNum> Add<$Self<S>, $Self<S>> for $Self<S> {
#[inline] fn add(self, v: $Self<S>) -> $Self<S> { self.add_v(&v) }
impl<S: BaseNum> Add for $Self<S> {
type Output = $Self<S>;
#[inline]
fn add(self, v: $Self<S>) -> $Self<S> { self.add_v(&v) }
}
impl<S: BaseNum> Sub<$Self<S>, $Self<S>> for $Self<S> {
#[inline] fn sub(self, v: $Self<S>) -> $Self<S> { self.sub_v(&v) }
impl<S: BaseNum> Sub for $Self<S> {
type Output = $Self<S>;
#[inline]
fn sub(self, v: $Self<S>) -> $Self<S> { self.sub_v(&v) }
}
impl<S: BaseNum> Neg<$Self<S>> for $Self<S> {
#[inline] fn neg(self) -> $Self<S> { $Self::new($(-self.$field),+) }
impl<S: BaseNum> Neg for $Self<S> {
type Output = $Self<S>;
#[inline]
fn neg(self) -> $Self<S> { $Self::new($(-self.$field),+) }
}
impl<S: BaseNum> Mul<$Self<S>, $Self<S>> for $Self<S> {
#[inline] fn mul(self, v: $Self<S>) -> $Self<S> { self.mul_v(&v) }
impl<S: BaseNum> Mul for $Self<S> {
type Output = $Self<S>;
#[inline]
fn mul(self, v: $Self<S>) -> $Self<S> { self.mul_v(&v) }
}
impl<S: BaseNum> Div<$Self<S>, $Self<S>> for $Self<S> {
#[inline] fn div(self, v: $Self<S>) -> $Self<S> { self.div_v(&v) }
impl<S: BaseNum> Div for $Self<S> {
type Output = $Self<S>;
#[inline]
fn div(self, v: $Self<S>) -> $Self<S> { self.div_v(&v) }
}
impl<S: BaseNum> Rem<$Self<S>, $Self<S>> for $Self<S> {
#[inline] fn rem(self, v: $Self<S>) -> $Self<S> { self.rem_v(&v) }
impl<S: BaseNum> Rem for $Self<S> {
type Output = $Self<S>;
#[inline]
fn rem(self, v: $Self<S>) -> $Self<S> { self.rem_v(&v) }
}
impl<S: BaseFloat> ApproxEq<S> for $Self<S> {