From b2e7f2e4b109ec5ba5e472244ed15dbef41e1d54 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 12 Jun 2013 11:15:12 +1000 Subject: [PATCH] Some constructors and functions don't need T:Copy --- src/mat2.rs | 75 ++++++++++++++++++++++++------------------- src/mat3.rs | 77 ++++++++++++++++++++++++++------------------- src/mat4.rs | 91 +++++++++++++++++++++++++++-------------------------- src/quat.rs | 52 +++++++++++++++--------------- src/vec2.rs | 22 ++++++------- src/vec3.rs | 24 +++++++------- src/vec4.rs | 26 +++++++-------- 7 files changed, 194 insertions(+), 173 deletions(-) diff --git a/src/mat2.rs b/src/mat2.rs index 3cab5ff..c80b460 100644 --- a/src/mat2.rs +++ b/src/mat2.rs @@ -21,41 +21,12 @@ use vec::*; use super::{Mat3, Mat4}; #[deriving(Eq)] -pub struct Mat2 { x: Vec2, y: Vec2 } - -impl Mat2 { - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec2 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec2,..2] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec2,..2] { - unsafe { transmute(self) } - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.col(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.col_mut(i).index_mut(j) - } +pub struct Mat2 { + x: Vec2, + y: Vec2, } -impl Mat2 { +impl Mat2 { /// Construct a 2 x 2 matrix /// /// # Arguments @@ -99,6 +70,44 @@ impl Mat2 { Mat2 { x: c0, y: c1 } } + #[inline] + pub fn col<'a>(&'a self, i: uint) -> &'a Vec2 { + &'a self.as_slice()[i] + } + + #[inline] + pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2 { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [Vec2,..2] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec2,..2] { + unsafe { transmute(self) } + } + + #[inline] + pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { + self.col(i).index(j) + } + + #[inline] + pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { + self.col_mut(i).index_mut(j) + } + + #[inline(always)] + pub fn map(&self, f: &fn(&Vec2) -> Vec2) -> Mat2 { + Mat2::from_cols(f(self.col(0)), + f(self.col(1))) + } +} + +impl Mat2 { #[inline] pub fn row(&self, i: uint) -> Vec2 { Vec2::new(*self.elem(0, i), diff --git a/src/mat3.rs b/src/mat3.rs index 14bcfaf..f612ac2 100644 --- a/src/mat3.rs +++ b/src/mat3.rs @@ -22,41 +22,13 @@ use quat::Quat; use super::Mat4; #[deriving(Eq)] -pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } - -impl Mat3 { - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec3 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec3,..3] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec3,..3] { - unsafe { transmute(self) } - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.col(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.col_mut(i).index_mut(j) - } +pub struct Mat3 { + x: Vec3, + y: Vec3, + z: Vec3, } -impl Mat3 { +impl Mat3 { /// Construct a 3 x 3 matrix /// /// # Arguments @@ -109,6 +81,45 @@ impl Mat3 { Mat3 { x: c0, y: c1, z: c2 } } + #[inline] + pub fn col<'a>(&'a self, i: uint) -> &'a Vec3 { + &'a self.as_slice()[i] + } + + #[inline] + pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3 { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [Vec3,..3] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec3,..3] { + unsafe { transmute(self) } + } + + #[inline] + pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { + self.col(i).index(j) + } + + #[inline] + pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { + self.col_mut(i).index_mut(j) + } + + #[inline(always)] + pub fn map(&self, f: &fn(&Vec3) -> Vec3) -> Mat3 { + Mat3::from_cols(f(self.col(0)), + f(self.col(1)), + f(self.col(2))) + } +} + +impl Mat3 { #[inline] pub fn row(&self, i: uint) -> Vec3 { Vec3::new(*self.elem(0, i), diff --git a/src/mat4.rs b/src/mat4.rs index 96f4dd2..49b032a 100644 --- a/src/mat4.rs +++ b/src/mat4.rs @@ -21,54 +21,15 @@ use std::uint; use vec::*; use super::Mat3; -/// A 4 x 4 column major matrix -/// -/// # Type parameters -/// -/// - `T` - The type of the elements of the matrix. Should be a floating point type. -/// -/// # Fields -/// -/// - `x`: the first column vector of the matrix -/// - `y`: the second column vector of the matrix -/// - `z`: the third column vector of the matrix -/// - `w`: the fourth column vector of the matrix #[deriving(Eq)] -pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } - -impl Mat4 { - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec4 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec4,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec4,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.col(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.col_mut(i).index_mut(j) - } +pub struct Mat4 { + x: Vec4, + y: Vec4, + z: Vec4, + w: Vec4, } -impl Mat4 { +impl Mat4 { /// Construct a 4 x 4 matrix /// /// # Arguments @@ -130,6 +91,46 @@ impl Mat4 { Mat4 { x: c0, y: c1, z: c2, w: c3 } } + #[inline] + pub fn col<'a>(&'a self, i: uint) -> &'a Vec4 { + &'a self.as_slice()[i] + } + + #[inline] + pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4 { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [Vec4,..4] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec4,..4] { + unsafe { transmute(self) } + } + + #[inline] + pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { + self.col(i).index(j) + } + + #[inline] + pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { + self.col_mut(i).index_mut(j) + } + + #[inline(always)] + pub fn map(&self, f: &fn(&Vec4) -> Vec4) -> Mat4 { + Mat4::from_cols(f(self.col(0)), + f(self.col(1)), + f(self.col(2)), + f(self.col(3))) + } +} + +impl Mat4 { #[inline] pub fn row(&self, i: uint) -> Vec4 { Vec4::new(*self.elem(0, i), diff --git a/src/quat.rs b/src/quat.rs index 025dfec..ab229ec 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -45,28 +45,6 @@ pub type Quatf64 = Quat; pub struct Quat { s: T, v: Vec3 } impl Quat { - #[inline] - pub fn index<'a>(&'a self, i: uint) -> &'a T { - &'a self.as_slice()[i] - } - - #[inline] - pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [T,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] { - unsafe { transmute(self) } - } -} - -impl Quat { /// Construct the quaternion from one scalar component and three /// imaginary components /// @@ -93,10 +71,23 @@ impl Quat { } #[inline] - pub fn swap(&mut self, a: uint, b: uint) { - let tmp = *self.index(a); - *self.index_mut(a) = *self.index(b); - *self.index_mut(b) = tmp; + pub fn index<'a>(&'a self, i: uint) -> &'a T { + &'a self.as_slice()[i] + } + + #[inline] + pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [T,..4] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] { + unsafe { transmute(self) } } #[inline] @@ -108,6 +99,15 @@ impl Quat { } } +impl Quat { + #[inline] + pub fn swap(&mut self, a: uint, b: uint) { + let tmp = *self.index(a); + *self.index_mut(a) = *self.index(b); + *self.index_mut(b) = tmp; + } +} + impl Quat { /// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i` #[inline] diff --git a/src/vec2.rs b/src/vec2.rs index 1985d43..7a7c19a 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -23,6 +23,11 @@ use super::Vec3; pub struct Vec2 { x: T, y: T } impl Vec2 { + #[inline] + pub fn new(x: T, y: T ) -> Vec2 { + Vec2 { x: x, y: y } + } + #[inline] pub fn index<'a>(&'a self, i: uint) -> &'a T { &'a self.as_slice()[i] @@ -42,14 +47,15 @@ impl Vec2 { pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..2] { unsafe { transmute(self) } } + + #[inline(always)] + pub fn map(&self, f: &fn(&T) -> T) -> Vec2 { + Vec2::new(f(self.index(0)), + f(self.index(1))) + } } impl Vec2 { - #[inline] - pub fn new(x: T, y: T ) -> Vec2 { - Vec2 { x: x, y: y } - } - #[inline] pub fn from_value(value: T) -> Vec2 { Vec2::new(value, value) @@ -61,12 +67,6 @@ impl Vec2 { *self.index_mut(a) = *self.index(b); *self.index_mut(b) = tmp; } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec2 { - Vec2::new(f(self.index(0)), - f(self.index(1))) - } } impl Vec2 { diff --git a/src/vec3.rs b/src/vec3.rs index 0267e51..3e08c41 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -23,6 +23,11 @@ use super::Vec4; pub struct Vec3 { x: T, y: T, z: T } impl Vec3 { + #[inline] + pub fn new(x: T, y: T, z: T ) -> Vec3 { + Vec3 { x: x, y: y, z: z } + } + #[inline] pub fn index<'a>(&'a self, i: uint) -> &'a T { &'a self.as_slice()[i] @@ -42,14 +47,16 @@ impl Vec3 { pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..3] { unsafe { transmute(self) } } + + #[inline(always)] + pub fn map(&self, f: &fn(&T) -> T) -> Vec3 { + Vec3::new(f(self.index(0)), + f(self.index(1)), + f(self.index(2))) + } } impl Vec3 { - #[inline] - pub fn new(x: T, y: T, z: T ) -> Vec3 { - Vec3 { x: x, y: y, z: z } - } - #[inline] pub fn from_value(value: T) -> Vec3 { Vec3::new(value, value, value) @@ -61,13 +68,6 @@ impl Vec3 { *self.index_mut(a) = *self.index(b); *self.index_mut(b) = tmp; } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec3 { - Vec3::new(f(self.index(0)), - f(self.index(1)), - f(self.index(2))) - } } impl Vec3 { diff --git a/src/vec4.rs b/src/vec4.rs index 3ce41a5..4e2ad28 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -21,6 +21,11 @@ use std::num::{Zero, One}; pub struct Vec4 { x: T, y: T, z: T, w: T } impl Vec4 { + #[inline] + pub fn new(x: T, y: T, z: T, w: T ) -> Vec4 { + Vec4 { x: x, y: y, z: z, w: w } + } + #[inline] pub fn index<'a>(&'a self, i: uint) -> &'a T { &'a self.as_slice()[i] @@ -40,14 +45,17 @@ impl Vec4 { pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] { unsafe { transmute(self) } } + + #[inline(always)] + pub fn map(&self, f: &fn(&T) -> T) -> Vec4 { + Vec4::new(f(self.index(0)), + f(self.index(1)), + f(self.index(2)), + f(self.index(3))) + } } impl Vec4 { - #[inline] - pub fn new(x: T, y: T, z: T, w: T ) -> Vec4 { - Vec4 { x: x, y: y, z: z, w: w } - } - #[inline] pub fn from_value(value: T) -> Vec4 { Vec4::new(value, value, value, value) @@ -59,14 +67,6 @@ impl Vec4 { *self.index_mut(a) = *self.index(b); *self.index_mut(b) = tmp; } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec4 { - Vec4::new(f(self.index(0)), - f(self.index(1)), - f(self.index(2)), - f(self.index(3))) - } } impl Vec4 {