Simplify trait bounds

This commit is contained in:
Brendan Zabarauskas 2013-09-17 18:50:42 +10:00
parent eade6cc18f
commit efbe4b199b
5 changed files with 55 additions and 55 deletions

View file

@ -38,7 +38,7 @@ approx_eq!(impl<S> Mat2<S>)
approx_eq!(impl<S> Mat3<S>) approx_eq!(impl<S> Mat3<S>)
approx_eq!(impl<S> Mat4<S>) approx_eq!(impl<S> Mat4<S>)
impl<S: Clone + Num + Ord> Mat2<S> { impl<S: Primitive> Mat2<S> {
#[inline] #[inline]
pub fn new(c0r0: S, c0r1: S, pub fn new(c0r0: S, c0r1: S,
c1r0: S, c1r1: S) -> Mat2<S> { c1r0: S, c1r1: S) -> Mat2<S> {
@ -68,7 +68,7 @@ impl<S: Clone + Num + Ord> Mat2<S> {
} }
} }
impl<S: Clone + Float> Mat2<S> { impl<S: Float> Mat2<S> {
#[inline] #[inline]
pub fn from_angle(theta: Rad<S>) -> Mat2<S> { pub fn from_angle(theta: Rad<S>) -> Mat2<S> {
let cos_theta = cos(theta.clone()); let cos_theta = cos(theta.clone());
@ -79,7 +79,7 @@ impl<S: Clone + Float> Mat2<S> {
} }
} }
impl<S: Clone + Num + Ord> Mat3<S> { impl<S: Primitive> Mat3<S> {
#[inline] #[inline]
pub fn new(c0r0:S, c0r1:S, c0r2:S, pub fn new(c0r0:S, c0r1:S, c0r2:S,
c1r0:S, c1r1:S, c1r2:S, c1r0:S, c1r1:S, c1r2:S,
@ -112,7 +112,7 @@ impl<S: Clone + Num + Ord> Mat3<S> {
} }
} }
impl<S: Clone + Float> Mat3<S> { impl<S: Float> Mat3<S> {
pub fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Mat3<S> { pub fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Mat3<S> {
let dir = dir.normalize(); let dir = dir.normalize();
let side = dir.cross(&up.normalize()); let side = dir.cross(&up.normalize());
@ -122,7 +122,7 @@ impl<S: Clone + Float> Mat3<S> {
} }
} }
impl<S: Clone + Num + Ord> Mat4<S> { impl<S: Primitive> Mat4<S> {
#[inline] #[inline]
pub fn new(c0r0: S, c0r1: S, c0r2: S, c0r3: S, pub fn new(c0r0: S, c0r1: S, c0r2: S, c0r3: S,
c1r0: S, c1r1: S, c1r2: S, c1r3: S, c1r0: S, c1r1: S, c1r2: S, c1r3: S,
@ -158,9 +158,9 @@ impl<S: Clone + Num + Ord> Mat4<S> {
} }
} }
impl<S: Clone + Float> One for Mat2<S> { #[inline] fn one() -> Mat2<S> { Mat2::ident() } } impl<S: Float> One for Mat2<S> { #[inline] fn one() -> Mat2<S> { Mat2::ident() } }
impl<S: Clone + Float> One for Mat3<S> { #[inline] fn one() -> Mat3<S> { Mat3::ident() } } impl<S: Float> One for Mat3<S> { #[inline] fn one() -> Mat3<S> { Mat3::ident() } }
impl<S: Clone + Float> One for Mat4<S> { #[inline] fn one() -> Mat4<S> { Mat4::ident() } } impl<S: Float> One for Mat4<S> { #[inline] fn one() -> Mat4<S> { Mat4::ident() } }
array!(impl<S> Mat2<S> -> [Vec2<S>, ..2] _2) array!(impl<S> Mat2<S> -> [Vec2<S>, ..2] _2)
array!(impl<S> Mat3<S> -> [Vec3<S>, ..3] _3) array!(impl<S> Mat3<S> -> [Vec3<S>, ..3] _3)
@ -168,7 +168,7 @@ array!(impl<S> Mat4<S> -> [Vec4<S>, ..4] _4)
pub trait Matrix pub trait Matrix
< <
S: Clone + Float, Slice, S: Float, Slice,
V: Clone + Vector<S, VSlice> + Array<S, VSlice>, VSlice V: Clone + Vector<S, VSlice> + Array<S, VSlice>, VSlice
> >
: Array<V, Slice> : Array<V, Slice>
@ -280,11 +280,11 @@ pub trait Matrix
fn is_symmetric(&self) -> bool; fn is_symmetric(&self) -> bool;
} }
impl<S: Clone + Float> Neg<Mat2<S>> for Mat2<S> { #[inline] fn neg(&self) -> Mat2<S> { build(|i| self.i(i).neg()) } } impl<S: Float> Neg<Mat2<S>> for Mat2<S> { #[inline] fn neg(&self) -> Mat2<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Float> Neg<Mat3<S>> for Mat3<S> { #[inline] fn neg(&self) -> Mat3<S> { build(|i| self.i(i).neg()) } } impl<S: Float> Neg<Mat3<S>> for Mat3<S> { #[inline] fn neg(&self) -> Mat3<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Float> Neg<Mat4<S>> for Mat4<S> { #[inline] fn neg(&self) -> Mat4<S> { build(|i| self.i(i).neg()) } } impl<S: Float> Neg<Mat4<S>> for Mat4<S> { #[inline] fn neg(&self) -> Mat4<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Float> impl<S: Float>
Matrix<S, [Vec2<S>, ..2], Vec2<S>, [S, ..2]> Matrix<S, [Vec2<S>, ..2], Vec2<S>, [S, ..2]>
for Mat2<S> for Mat2<S>
{ {
@ -333,7 +333,7 @@ for Mat2<S>
} }
} }
impl<S: Clone + Float> impl<S: Float>
Matrix<S, [Vec3<S>, ..3], Vec3<S>, [S, ..3]> Matrix<S, [Vec3<S>, ..3], Vec3<S>, [S, ..3]>
for Mat3<S> for Mat3<S>
{ {
@ -394,7 +394,7 @@ for Mat3<S>
} }
} }
impl<S: Clone + Float> impl<S: Float>
Matrix<S, [Vec4<S>, ..4], Vec4<S>, [S, ..4]> Matrix<S, [Vec4<S>, ..4], Vec4<S>, [S, ..4]>
for Mat4<S> for Mat4<S>
{ {
@ -523,11 +523,11 @@ for Mat4<S>
} }
// Conversion traits // Conversion traits
pub trait ToMat2<S: Clone + Num + Ord> { fn to_mat2(&self) -> Mat2<S>; } pub trait ToMat2<S: Primitive> { fn to_mat2(&self) -> Mat2<S>; }
pub trait ToMat3<S: Clone + Num + Ord> { fn to_mat3(&self) -> Mat3<S>; } pub trait ToMat3<S: Primitive> { fn to_mat3(&self) -> Mat3<S>; }
pub trait ToMat4<S: Clone + Num + Ord> { fn to_mat4(&self) -> Mat4<S>; } pub trait ToMat4<S: Primitive> { fn to_mat4(&self) -> Mat4<S>; }
impl<S: Clone + Float> ToMat3<S> for Mat2<S> { impl<S: Float> ToMat3<S> for Mat2<S> {
/// Clone the elements of a 2-dimensional matrix into the top corner of a /// Clone the elements of a 2-dimensional matrix into the top corner of a
/// 3-dimensional identity matrix. /// 3-dimensional identity matrix.
fn to_mat3(&self) -> Mat3<S> { fn to_mat3(&self) -> Mat3<S> {
@ -537,7 +537,7 @@ impl<S: Clone + Float> ToMat3<S> for Mat2<S> {
} }
} }
impl<S: Clone + Float> ToMat4<S> for Mat2<S> { impl<S: Float> ToMat4<S> for Mat2<S> {
/// Clone the elements of a 2-dimensional matrix into the top corner of a /// Clone the elements of a 2-dimensional matrix into the top corner of a
/// 4-dimensional identity matrix. /// 4-dimensional identity matrix.
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
@ -548,7 +548,7 @@ impl<S: Clone + Float> ToMat4<S> for Mat2<S> {
} }
} }
impl<S: Clone + Float> ToMat4<S> for Mat3<S> { impl<S: Float> ToMat4<S> for Mat3<S> {
/// Clone the elements of a 3-dimensional matrix into the top corner of a /// Clone the elements of a 3-dimensional matrix into the top corner of a
/// 4-dimensional identity matrix. /// 4-dimensional identity matrix.
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
@ -559,7 +559,7 @@ impl<S: Clone + Float> ToMat4<S> for Mat3<S> {
} }
} }
impl<S:Clone + Float> ToQuat<S> for Mat3<S> { impl<S:Float> ToQuat<S> for Mat3<S> {
/// Convert the matrix to a quaternion /// Convert the matrix to a quaternion
fn to_quat(&self) -> Quat<S> { fn to_quat(&self) -> Quat<S> {
// Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's // Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's

View file

@ -35,19 +35,19 @@ pub struct Plane<S> {
distance: S, distance: S,
} }
impl<S: Clone + Float> Intersect<Option<Point3<S>>> for (Plane<S>, Ray3<S>) { impl<S: Float> Intersect<Option<Point3<S>>> for (Plane<S>, Ray3<S>) {
fn intersection(&self) -> Option<Point3<S>> { fn intersection(&self) -> Option<Point3<S>> {
fail!("Not yet implemented"); fail!("Not yet implemented");
} }
} }
impl<S: Clone + Float> Intersect<Option<Ray3<S>>> for (Plane<S>, Plane<S>) { impl<S: Float> Intersect<Option<Ray3<S>>> for (Plane<S>, Plane<S>) {
fn intersection(&self) -> Option<Ray3<S>> { fn intersection(&self) -> Option<Ray3<S>> {
fail!("Not yet implemented"); fail!("Not yet implemented");
} }
} }
impl<S: Clone + Float> Intersect<Option<Point3<S>>> for (Plane<S>, Plane<S>, Plane<S>) { impl<S: Float> Intersect<Option<Point3<S>>> for (Plane<S>, Plane<S>, Plane<S>) {
fn intersection(&self) -> Option<Point3<S>> { fn intersection(&self) -> Option<Point3<S>> {
fail!("Not yet implemented"); fail!("Not yet implemented");
} }

View file

@ -56,7 +56,7 @@ impl<S: Num> Point3<S> {
/// Specifies the numeric operations for point types. /// Specifies the numeric operations for point types.
pub trait Point pub trait Point
< <
S: Clone + Num, S: Primitive,
V: Vector<S, Slice>, V: Vector<S, Slice>,
Slice Slice
> >
@ -79,8 +79,8 @@ pub trait Point
array!(impl<S> Point2<S> -> [S, ..2] _2) array!(impl<S> Point2<S> -> [S, ..2] _2)
array!(impl<S> Point3<S> -> [S, ..3] _3) array!(impl<S> Point3<S> -> [S, ..3] _3)
impl<S: Clone + Num + Ord> Point<S, Vec2<S>, [S, ..2]> for Point2<S>; impl<S: Primitive> Point<S, Vec2<S>, [S, ..2]> for Point2<S>;
impl<S: Clone + Num + Ord> Point<S, Vec3<S>, [S, ..3]> for Point3<S>; impl<S: Primitive> Point<S, Vec3<S>, [S, ..3]> for Point3<S>;
impl<S> ToStr for Point2<S> { impl<S> ToStr for Point2<S> {
fn to_str(&self) -> ~str { fn to_str(&self) -> ~str {

View file

@ -27,11 +27,11 @@ pub struct Quat<S> { s: S, v: Vec3<S> }
array!(impl<S> Quat<S> -> [S, ..4] _4) array!(impl<S> Quat<S> -> [S, ..4] _4)
approx_eq!(impl<S> Quat<S>) approx_eq!(impl<S> Quat<S>)
pub trait ToQuat<S: Clone + Float> { pub trait ToQuat<S: Float> {
fn to_quat(&self) -> Quat<S>; fn to_quat(&self) -> Quat<S>;
} }
impl<S: Clone + Float> Quat<S> { impl<S: Float> Quat<S> {
/// Construct a new quaternion from one scalar component and three /// Construct a new quaternion from one scalar component and three
/// imaginary components /// imaginary components
#[inline] #[inline]
@ -174,7 +174,7 @@ impl<S: Clone + Float> Quat<S> {
} }
} }
impl<S: Clone + Float> Quat<S> { impl<S: Float> Quat<S> {
/// Spherical Linear Intoperlation /// Spherical Linear Intoperlation
/// ///
/// Perform a spherical linear interpolation between the quaternion and /// Perform a spherical linear interpolation between the quaternion and
@ -219,7 +219,7 @@ impl<S: Clone + Float> Quat<S> {
} }
} }
impl<S: Clone + Float> ToMat3<S> for Quat<S> { impl<S: Float> ToMat3<S> for Quat<S> {
/// Convert the quaternion to a 3 x 3 rotation matrix /// Convert the quaternion to a 3 x 3 rotation matrix
fn to_mat3(&self) -> Mat3<S> { fn to_mat3(&self) -> Mat3<S> {
let x2 = self.v.x + self.v.x; let x2 = self.v.x + self.v.x;
@ -244,7 +244,7 @@ impl<S: Clone + Float> ToMat3<S> for Quat<S> {
} }
} }
impl<S: Clone + Float> Neg<Quat<S>> for Quat<S> { impl<S: Float> Neg<Quat<S>> for Quat<S> {
#[inline] #[inline]
fn neg(&self) -> Quat<S> { fn neg(&self) -> Quat<S> {
Quat::from_sv(-self.s, -self.v) Quat::from_sv(-self.s, -self.v)

View file

@ -31,14 +31,14 @@ pub struct Vec3<S> { x: S, y: S, z: S }
pub struct Vec4<S> { x: S, y: S, z: S, w: S } pub struct Vec4<S> { x: S, y: S, z: S, w: S }
// Conversion traits // Conversion traits
pub trait ToVec2<S: Clone + Num + Ord> { fn to_vec2(&self) -> Vec2<S>; } pub trait ToVec2<S: Primitive> { fn to_vec2(&self) -> Vec2<S>; }
pub trait ToVec3<S: Clone + Num + Ord> { fn to_vec3(&self) -> Vec3<S>; } pub trait ToVec3<S: Primitive> { fn to_vec3(&self) -> Vec3<S>; }
pub trait ToVec4<S: Clone + Num + Ord> { fn to_vec4(&self) -> Vec4<S>; } pub trait ToVec4<S: Primitive> { fn to_vec4(&self) -> Vec4<S>; }
// Utility macro for generating associated functions for the vectors // Utility macro for generating associated functions for the vectors
macro_rules! vec( macro_rules! vec(
(impl $Self:ident <$S:ident> { $($field:ident),+ }) => ( (impl $Self:ident <$S:ident> { $($field:ident),+ }) => (
impl<$S: Clone + Num + Ord> $Self<$S> { impl<$S: Primitive> $Self<$S> {
#[inline] #[inline]
pub fn new($($field: $S),+) -> $Self<$S> { pub fn new($($field: $S),+) -> $Self<$S> {
$Self { $($field: $field),+ } $Self { $($field: $field),+ }
@ -65,18 +65,18 @@ vec!(impl Vec2<S> { x, y })
vec!(impl Vec3<S> { x, y, z }) vec!(impl Vec3<S> { x, y, z })
vec!(impl Vec4<S> { x, y, z, w }) vec!(impl Vec4<S> { x, y, z, w })
impl<S: Clone + Num + Ord> Vec2<S> { impl<S: Primitive> Vec2<S> {
#[inline] pub fn unit_x() -> Vec2<S> { Vec2::new(one(), zero()) } #[inline] pub fn unit_x() -> Vec2<S> { Vec2::new(one(), zero()) }
#[inline] pub fn unit_y() -> Vec2<S> { Vec2::new(zero(), one()) } #[inline] pub fn unit_y() -> Vec2<S> { Vec2::new(zero(), one()) }
} }
impl<S: Clone + Num + Ord> Vec3<S> { impl<S: Primitive> Vec3<S> {
#[inline] pub fn unit_x() -> Vec3<S> { Vec3::new(one(), zero(), zero()) } #[inline] pub fn unit_x() -> Vec3<S> { Vec3::new(one(), zero(), zero()) }
#[inline] pub fn unit_y() -> Vec3<S> { Vec3::new(zero(), one(), zero()) } #[inline] pub fn unit_y() -> Vec3<S> { Vec3::new(zero(), one(), zero()) }
#[inline] pub fn unit_z() -> Vec3<S> { Vec3::new(zero(), zero(), one()) } #[inline] pub fn unit_z() -> Vec3<S> { Vec3::new(zero(), zero(), one()) }
} }
impl<S: Clone + Num + Ord> Vec4<S> { impl<S: Primitive> Vec4<S> {
#[inline] pub fn unit_x() -> Vec4<S> { Vec4::new(one(), zero(), zero(), zero()) } #[inline] pub fn unit_x() -> Vec4<S> { Vec4::new(one(), zero(), zero(), zero()) }
#[inline] pub fn unit_y() -> Vec4<S> { Vec4::new(zero(), one(), zero(), zero()) } #[inline] pub fn unit_y() -> Vec4<S> { Vec4::new(zero(), one(), zero(), zero()) }
#[inline] pub fn unit_z() -> Vec4<S> { Vec4::new(zero(), zero(), one(), zero()) } #[inline] pub fn unit_z() -> Vec4<S> { Vec4::new(zero(), zero(), one(), zero()) }
@ -96,7 +96,7 @@ approx_eq!(impl<S> Vec4<S>)
/// for pragmatic reasons. /// for pragmatic reasons.
pub trait Vector pub trait Vector
< <
S: Clone + Num + Ord, S: Primitive,
Slice Slice
> >
: Array<S, Slice> : Array<S, Slice>
@ -145,19 +145,19 @@ pub trait Vector
#[inline] fn comp_max(&self) -> S { self.iter().max().unwrap().clone() } #[inline] fn comp_max(&self) -> S { self.iter().max().unwrap().clone() }
} }
#[inline] fn dot<S: Clone + Num + Ord, Slice, V: Vector<S, Slice>>(a: V, b: V) -> S { a.dot(&b) } #[inline] fn dot<S: Primitive, Slice, V: Vector<S, Slice>>(a: V, b: V) -> S { a.dot(&b) }
impl<S: Clone + Num + Ord> One for Vec2<S> { #[inline] fn one() -> Vec2<S> { Vec2::ident() } } impl<S: Primitive> One for Vec2<S> { #[inline] fn one() -> Vec2<S> { Vec2::ident() } }
impl<S: Clone + Num + Ord> One for Vec3<S> { #[inline] fn one() -> Vec3<S> { Vec3::ident() } } impl<S: Primitive> One for Vec3<S> { #[inline] fn one() -> Vec3<S> { Vec3::ident() } }
impl<S: Clone + Num + Ord> One for Vec4<S> { #[inline] fn one() -> Vec4<S> { Vec4::ident() } } impl<S: Primitive> One for Vec4<S> { #[inline] fn one() -> Vec4<S> { Vec4::ident() } }
impl<S: Clone + Num + Ord> Neg<Vec2<S>> for Vec2<S> { #[inline] fn neg(&self) -> Vec2<S> { build(|i| self.i(i).neg()) } } impl<S: Primitive> Neg<Vec2<S>> for Vec2<S> { #[inline] fn neg(&self) -> Vec2<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Num + Ord> Neg<Vec3<S>> for Vec3<S> { #[inline] fn neg(&self) -> Vec3<S> { build(|i| self.i(i).neg()) } } impl<S: Primitive> Neg<Vec3<S>> for Vec3<S> { #[inline] fn neg(&self) -> Vec3<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Num + Ord> Neg<Vec4<S>> for Vec4<S> { #[inline] fn neg(&self) -> Vec4<S> { build(|i| self.i(i).neg()) } } impl<S: Primitive> Neg<Vec4<S>> for Vec4<S> { #[inline] fn neg(&self) -> Vec4<S> { build(|i| self.i(i).neg()) } }
macro_rules! vector( macro_rules! vector(
(impl $Self:ident <$S:ident> $Slice:ty { $x:ident, $($xs:ident),+ }) => ( (impl $Self:ident <$S:ident> $Slice:ty { $x:ident, $($xs:ident),+ }) => (
impl<$S: Clone + Num + Ord> Vector<$S, $Slice> for $Self<$S>; impl<$S: Primitive> Vector<$S, $Slice> for $Self<$S>;
) )
) )
@ -166,7 +166,7 @@ vector!(impl Vec3<S> [S, ..3] { x, y, z })
vector!(impl Vec4<S> [S, ..4] { x, y, z, w }) vector!(impl Vec4<S> [S, ..4] { x, y, z, w })
/// Operations specific to numeric two-dimensional vectors. /// Operations specific to numeric two-dimensional vectors.
impl<S: Clone + Num + Ord> Vec2<S> { impl<S: Primitive> Vec2<S> {
/// The perpendicular dot product of the vector and `other`. /// The perpendicular dot product of the vector and `other`.
#[inline] #[inline]
pub fn perp_dot(&self, other: &Vec2<S>) -> S { pub fn perp_dot(&self, other: &Vec2<S>) -> S {
@ -175,7 +175,7 @@ impl<S: Clone + Num + Ord> Vec2<S> {
} }
/// Operations specific to numeric three-dimensional vectors. /// Operations specific to numeric three-dimensional vectors.
impl<S: Clone + Num + Ord> Vec3<S> { impl<S: Primitive> Vec3<S> {
/// Returns the cross product of the vector and `other`. /// Returns the cross product of the vector and `other`.
#[inline] #[inline]
pub fn cross(&self, other: &Vec3<S>) -> Vec3<S> { pub fn cross(&self, other: &Vec3<S>) -> Vec3<S> {
@ -196,7 +196,7 @@ impl<S: Clone + Num + Ord> Vec3<S> {
/// 2-dimensional and 3-dimensional vectors. /// 2-dimensional and 3-dimensional vectors.
pub trait EuclideanVector pub trait EuclideanVector
< <
S: Clone + Float, S: Float,
Slice Slice
> >
: Vector<S, Slice> : Vector<S, Slice>
@ -267,21 +267,21 @@ pub trait EuclideanVector
} }
} }
impl<S: Clone + Float> EuclideanVector<S, [S, ..2]> for Vec2<S> { impl<S: Float> EuclideanVector<S, [S, ..2]> for Vec2<S> {
#[inline] #[inline]
fn angle(&self, other: &Vec2<S>) -> Rad<S> { fn angle(&self, other: &Vec2<S>) -> Rad<S> {
atan2(self.perp_dot(other), self.dot(other)) atan2(self.perp_dot(other), self.dot(other))
} }
} }
impl<S: Clone + Float> EuclideanVector<S, [S, ..3]> for Vec3<S> { impl<S: Float> EuclideanVector<S, [S, ..3]> for Vec3<S> {
#[inline] #[inline]
fn angle(&self, other: &Vec3<S>) -> Rad<S> { fn angle(&self, other: &Vec3<S>) -> Rad<S> {
atan2(self.cross(other).length(), self.dot(other)) atan2(self.cross(other).length(), self.dot(other))
} }
} }
impl<S: Clone + Float> EuclideanVector<S, [S, ..4]> for Vec4<S> { impl<S: Float> EuclideanVector<S, [S, ..4]> for Vec4<S> {
#[inline] #[inline]
fn angle(&self, other: &Vec4<S>) -> Rad<S> { fn angle(&self, other: &Vec4<S>) -> Rad<S> {
acos(self.dot(other) / (self.length() * other.length())) acos(self.dot(other) / (self.length() * other.length()))