From 6c14df7fda9e470ca57fe4fad02ba1d1d5a3920a Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 4 Dec 2012 21:42:24 +1000 Subject: [PATCH] Move methods operating on `&mut self` to a separate trait --- src/vec.rs | 128 ++++++++++++++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/src/vec.rs b/src/vec.rs index 2ea253e..925d128 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -17,6 +17,13 @@ use num::kinds::Number; pub trait Vector: Dimensional, ToPtr, Eq, DefaultEq { /// Construct the vector from a single value, copying it to each component static pure fn from_value(value: T) -> self; +} + +pub trait MutableVector: Vector { + /** + * Get a mutable reference to the component at `i` + */ + fn index_mut(&mut self, i: uint) -> &self/mut T; /** * Swap two components of the vector in place @@ -48,7 +55,7 @@ pub trait Vector4: Vector { /** * A vector with numeric components */ -pub trait NumericVector: Vector, Neg{ +pub trait NumericVector: Vector, Neg { /** * Returns a vector with each component set to one */ @@ -74,6 +81,11 @@ pub trait NumericVector: Vector, Neg{ */ pure fn add_v(&self, other: &self) -> self; + /** + * Add the vector `other` + */ + // fn add_v_self(&mut self, other: &self); + /** * Returns the difference between the vector and `other` */ @@ -172,24 +184,6 @@ pub impl Vec2: Vector { static pure fn from_value(value: T) -> Vec2 { Vec2::new(value, value) } - - #[inline(always)] - fn swap(&mut self, a: uint, b: uint) { - let addr_a = - match a { - 0 => &mut self.x, - 1 => &mut self.y, - _ => fail(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", a)) - }; - let addr_b = - match b { - 0 => &mut self.x, - 1 => &mut self.y, - _ => fail(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", b)) - }; - - util::swap(addr_a, addr_b); - } } pub impl Vec2: Dimensional { @@ -217,6 +211,23 @@ pub impl Vec2: ToPtr { } } } + +pub impl Vec2: MutableVector { + #[inline(always)] + fn index_mut(&mut self, i: uint) -> &self/mut T { + match i { + 0 => &mut self.x, + 1 => &mut self.y, + _ => fail(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i)) + } + } + + #[inline(always)] + fn swap(&mut self, a: uint, b: uint) { + util::swap(self.index_mut(a), + self.index_mut(b)); + } +} pub impl Vec2: NumericVector { #[inline(always)] @@ -353,26 +364,6 @@ pub impl Vec3: Vector { static pure fn from_value(value: T) -> Vec3 { Vec3::new(value, value, value) } - - #[inline(always)] - fn swap(&mut self, a: uint, b: uint) { - let addr_a = - match a { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - _ => fail(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", a)) - }; - let addr_b = - match b { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - _ => fail(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", b)) - }; - - util::swap(addr_a, addr_b); - } } pub impl Vec3: Dimensional { @@ -401,6 +392,24 @@ pub impl Vec3: ToPtr { } } +pub impl Vec3: MutableVector { + #[inline(always)] + fn index_mut(&mut self, i: uint) -> &self/mut T { + match i { + 0 => &mut self.x, + 1 => &mut self.y, + 2 => &mut self.z, + _ => fail(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i)) + } + } + + #[inline(always)] + fn swap(&mut self, a: uint, b: uint) { + util::swap(self.index_mut(a), + self.index_mut(b)); + } +} + pub impl Vec3: NumericVector { #[inline(always)] static pure fn identity() -> Vec3 { @@ -554,28 +563,6 @@ pub impl Vec4: Vector { static pure fn from_value(value: T) -> Vec4 { Vec4::new(value, value, value, value) } - - #[inline(always)] - fn swap(&mut self, a: uint, b: uint) { - let addr_a = - match a { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - 3 => &mut self.w, - _ => fail(fmt!("index out of bounds: expected an index from 0 to 3 but found %u", a)) - }; - let addr_b = - match b { - 0 => &mut self.x, - 1 => &mut self.y, - 2 => &mut self.z, - 3 => &mut self.w, - _ => fail(fmt!("index out of bounds: expected an index from 0 to 3 but found %u", b)) - }; - - util::swap(addr_a, addr_b); - } } pub impl Vec4: Dimensional { @@ -604,6 +591,25 @@ pub impl Vec4: ToPtr { } } +pub impl Vec4: MutableVector { + #[inline(always)] + fn index_mut(&mut self, i: uint) -> &self/mut T { + match i { + 0 => &mut self.x, + 1 => &mut self.y, + 2 => &mut self.z, + 3 => &mut self.w, + _ => fail(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i)) + } + } + + #[inline(always)] + fn swap(&mut self, a: uint, b: uint) { + util::swap(self.index_mut(a), + self.index_mut(b)); + } +} + pub impl Vec4: NumericVector { #[inline(always)] static pure fn identity() -> Vec4 {