From 981836626b3aa2786489e61acd4a908b2a8db729 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 13 Dec 2015 12:14:57 +1100 Subject: [PATCH] Make Angle's type parameter an associated type --- src/angle.rs | 60 ++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/angle.rs b/src/angle.rs index 51a48dd..4f18c83 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -74,21 +74,21 @@ impl ScalarConv for Deg { } /// Operations on angles. -pub trait Angle -< - S: BaseFloat -> -: Clone + Zero -+ PartialEq + PartialOrd -+ ApproxEq -+ Neg -+ Into> -+ Into> -+ ScalarConv -+ fmt::Debug +pub trait Angle where + Self: Clone + Zero, + Self: PartialEq + PartialOrd, + // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 + Self: ApproxEq::Unitless>, + Self: Neg, + Self: Into::Unitless>>, + Self: Into::Unitless>>, + Self: ScalarConv<::Unitless>, + Self: fmt::Debug, { + type Unitless: BaseFloat; + /// Create a new angle from any other valid angle. - fn from>(theta: A) -> Self; + fn from>(theta: A) -> Self; /// Negate this angle, in-place. #[inline] fn neg_self(&mut self) { *self = -(*self).clone() } @@ -98,16 +98,16 @@ pub trait Angle /// Subtract another angle from this one, returning the new angle. #[inline] fn sub_a(&self, other: Self) -> Self { ScalarConv::from(*self.s() - *other.s()) } /// Divide this angle by another, returning the ratio. - #[inline] fn div_a(&self, other: Self) -> S { *self.s() / *other.s() } + #[inline] fn div_a(&self, other: Self) -> Self::Unitless { *self.s() / *other.s() } /// Take the remainder of this angle with another. - #[inline] fn rem_a(&self, other: Self) -> S { *self.s() % *other.s() } + #[inline] fn rem_a(&self, other: Self) -> Self::Unitless { *self.s() % *other.s() } /// Multiply this angle by a scalar, returning the new angle. - #[inline] fn mul_s(&self, s: S) -> Self { ScalarConv::from(*self.s() * s) } + #[inline] fn mul_s(&self, scalar: Self::Unitless) -> Self { ScalarConv::from(*self.s() * scalar) } /// Divide this angle by a scalar, returing the new angle. - #[inline] fn div_s(&self, s: S) -> Self { ScalarConv::from(*self.s() / s) } + #[inline] fn div_s(&self, scalar: Self::Unitless) -> Self { ScalarConv::from(*self.s() / scalar) } /// Take the remainder of this angle by a scalar, returning the new angle. - #[inline] fn rem_s(&self, s: S) -> Self { ScalarConv::from(*self.s() % s) } + #[inline] fn rem_s(&self, scalar: Self::Unitless) -> Self { ScalarConv::from(*self.s() % scalar) } /// Add this angle with another, in-place. #[inline] fn add_self_a(&mut self, other: Self) { *self.mut_s() = *self.s() + *other.s() } @@ -115,11 +115,11 @@ pub trait Angle #[inline] fn sub_self_a(&mut self, other: Self) { *self.mut_s() = *self.s() - *other.s() } /// Multiply this angle by a scalar, in-place. - #[inline] fn mul_self_s(&mut self, s: S) { *self.mut_s() = *self.s() * s } + #[inline] fn mul_self_s(&mut self, scalar: Self::Unitless) { *self.mut_s() = *self.s() * scalar } /// Divide this angle by a scalar, in-place. - #[inline] fn div_self_s(&mut self, s: S) { *self.mut_s() = *self.s() / s } + #[inline] fn div_self_s(&mut self, scalar: Self::Unitless) { *self.mut_s() = *self.s() / scalar } /// Take the remainder of this angle by a scalar, in-place. - #[inline] fn rem_self_s(&mut self, s: S) { *self.mut_s() = *self.s() % s } + #[inline] fn rem_self_s(&mut self, scalar: Self::Unitless) { *self.mut_s() = *self.s() % scalar } /// Return the angle, normalized to the range `[0, full_turn)`. #[inline] @@ -159,7 +159,7 @@ pub trait Angle #[inline] fn equiv(&self, other: &Self) -> bool { self.normalize() == other.normalize() } } -#[inline] pub fn bisect>(a: A, b: A) -> A { a.bisect(b) } +#[inline] pub fn bisect(a: A, b: A) -> A { a.bisect(b) } impl>, S: BaseFloat> Add for Rad { type Output = Rad; @@ -239,15 +239,16 @@ impl One for Deg { } const PI_2: f64 = f64::consts::PI * 2f64; -impl -Angle for Rad { - #[inline] fn from>(theta: A) -> Rad { theta.into() } + +impl Angle for Rad { + type Unitless = S; + #[inline] fn from>(theta: A) -> Rad { theta.into() } #[inline] fn full_turn() -> Rad { rad(cast(PI_2).unwrap()) } } -impl -Angle for Deg { - #[inline] fn from>(theta: A) -> Deg { theta.into() } +impl Angle for Deg { + type Unitless = S; + #[inline] fn from>(theta: A) -> Deg { theta.into() } #[inline] fn full_turn() -> Deg { deg(cast(360i32).unwrap()) } } @@ -265,8 +266,7 @@ Angle for Deg { #[inline] pub fn atan>>(s: S) -> R { rad(s.atan()).into() } #[inline] pub fn atan2>>(a: S, b: S) -> R { rad(a.atan2(b)).into() } -impl -fmt::Debug for Rad { +impl fmt::Debug for Rad { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?} rad", self.s) }