From dea0ccc1fd8c1e416ecd956141d1bf1bc030776f Mon Sep 17 00:00:00 2001 From: Jeroen Bollen Date: Sun, 17 Apr 2016 09:23:37 +0200 Subject: [PATCH] Made PntAsVec use associated types. (#179) Fix #177 --- src/lib.rs | 12 ++++++++---- src/structs/pnt_macros.rs | 8 +++++--- src/traits/structure.rs | 22 +++++++++++++--------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f6531611..bf3de13d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -311,9 +311,11 @@ pub fn orig() -> P { /// Returns the center of two points. #[inline] -pub fn center, V: Copy + Norm>(a: &P, b: &P) -> P { +pub fn center>(a: &P, b: &P) -> P + where

::Vec: Norm +{ let _2 = one::() + one(); - (*a + *b.as_vec()) / _2 + (*a + b.to_vec()) / _2 } /* @@ -321,13 +323,15 @@ pub fn center, V: Copy + Norm>(a: &P, b: &P) */ /// Returns the distance between two points. #[inline(always)] -pub fn dist, V: Norm>(a: &P, b: &P) -> N { +pub fn dist>(a: &P, b: &P) -> N where

::Vec: Norm { a.dist(b) } /// Returns the squared distance between two points. #[inline(always)] -pub fn sqdist, V: Norm>(a: &P, b: &P) -> N { +pub fn sqdist>(a: &P, b: &P) -> N + where

::Vec: Norm +{ a.sqdist(b) } diff --git a/src/structs/pnt_macros.rs b/src/structs/pnt_macros.rs index 8b793b15..f5df77a9 100644 --- a/src/structs/pnt_macros.rs +++ b/src/structs/pnt_macros.rs @@ -82,7 +82,9 @@ macro_rules! pnt_as_vec_impl( } } - impl PntAsVec<$tv> for $t { + impl PntAsVec for $t { + type Vec = $tv; + #[inline] fn to_vec(self) -> $tv { self.to_vec() @@ -132,11 +134,11 @@ macro_rules! pnt_from_homogeneous_impl( macro_rules! num_float_pnt_impl( ($t: ident, $tv: ident) => ( - impl NumPnt> for $t + impl NumPnt for $t where N: BaseNum { } - impl FloatPnt> for $t + impl FloatPnt for $t where N: BaseFloat + ApproxEq { } ) diff --git a/src/traits/structure.rs b/src/traits/structure.rs index 28d49fc0..468c0258 100644 --- a/src/traits/structure.rs +++ b/src/traits/structure.rs @@ -236,38 +236,42 @@ pub trait FloatVec: NumVec + Norm + Neg + Bas * Pnt related traits. */ /// Trait that relates a point of an affine space to a vector of the associated vector space. -pub trait PntAsVec { +pub trait PntAsVec { + /// The vector type of the vector space associated to this point's affine space. + type Vec; + /// Converts this point to its associated vector. - fn to_vec(self) -> V; + fn to_vec(self) -> Self::Vec; /// Converts a reference to this point to a reference to its associated vector. - fn as_vec<'a>(&'a self) -> &'a V; + fn as_vec<'a>(&'a self) -> &'a Self::Vec; // NOTE: this is used in some places to overcome some limitations untill the trait reform is // done on rustc. /// Sets the coordinates of this point to match those of a given vector. - fn set_coords(&mut self, coords: V); + fn set_coords(&mut self, coords: Self::Vec); } /// Trait grouping most common operations on points. // XXX: the vector space element `V` should be an associated type. Though this would prevent V from // having bounds (they are not supported yet). So, for now, we will just use a type parameter. -pub trait NumPnt: +pub trait NumPnt: Copy + - PntAsVec + + PntAsVec + Dim + Orig + PartialEq + Axpy + - Sub + + Sub::Vec> + Mul + Div + - Add + + Add<::Vec, Output = Self> + Index { // FIXME: + Sub } /// Trait of points with components implementing the `BaseFloat` trait. -pub trait FloatPnt>: NumPnt + Sized { +pub trait FloatPnt: NumPnt + Sized +where ::Vec: Norm { /// Computes the square distance between two points. #[inline] fn sqdist(&self, other: &Self) -> N {