From bc9104fdb590bc3449fef9b9ad86ac73a08f7536 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 12 Dec 2012 14:19:19 +1000 Subject: [PATCH] Implement FuzzyEq for Angle and Color types --- src/angle.rs | 21 +++++++++++++++++++-- src/color/color.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/angle.rs b/src/angle.rs index 56c1bef..98090f2 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -1,5 +1,7 @@ use core::cmp::{Eq, Ord}; +use std::cmp::FuzzyEq; + use funs::triganomic::{cos, sin}; use mat::{Mat3, Mat4}; use num::conv::cast; @@ -18,6 +20,7 @@ pub trait Angle: Add Modulo // Modulo // TODO: not sure how to implement this, or if it is even possible... Neg + FuzzyEq Eq Ord { static pure fn full_turn() -> self; static pure fn half_turn() -> self; @@ -35,7 +38,7 @@ pub trait Angle: Add - +// pub struct Radians(T); // error: internal compiler error: deref_cat() invoked on non-derefable type angle::Radians<'a> pub enum Radians = T; pub impl Radians: Angle { @@ -109,6 +112,13 @@ pub impl Radians: Neg> { } } +pub impl Radians: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &Radians) -> bool { + (*self).fuzzy_eq(&**other) + } +} + pub impl Radians: Eq { #[inline(always)] pure fn eq(&self, other: &Radians) -> bool { **self == **other } #[inline(always)] pure fn ne(&self, other: &Radians) -> bool { **self != **other } @@ -136,9 +146,9 @@ pub impl Radians: ToStr { +// pub struct Degrees(T); // error: internal compiler error: deref_cat() invoked on non-derefable type angle::Degrees<'a> pub enum Degrees = T; -// FIXME: not sure why I need the Eq and Ord trait bounds, but Rust complains if I don't include them pub impl Degrees: Angle { #[inline(always)] static pure fn full_turn() -> Degrees { Degrees(cast(360.0)) } #[inline(always)] static pure fn half_turn() -> Degrees { Degrees(cast(180.0)) } @@ -210,6 +220,13 @@ pub impl Degrees: Neg> { } } +pub impl Degrees: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &Degrees) -> bool { + (*self).fuzzy_eq(&**other) + } +} + pub impl Degrees: Eq { #[inline(always)] pure fn eq(&self, other: &Degrees) -> bool { **self == **other } #[inline(always)] pure fn ne(&self, other: &Degrees) -> bool { **self != **other } diff --git a/src/color/color.rs b/src/color/color.rs index 009fba6..94925ea 100644 --- a/src/color/color.rs +++ b/src/color/color.rs @@ -4,6 +4,8 @@ use core::ptr::to_unsafe_ptr; use core::sys::size_of; use core::vec::raw::buf_as_slice; +use std::cmp::FuzzyEq; + use angle::Degrees; use channel::Channel; use funs::common::Sign; @@ -380,6 +382,15 @@ pub impl RGB: Eq { } } +pub impl RGB: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &RGB) -> bool { + self.r.fuzzy_eq(&other.r) && + self.g.fuzzy_eq(&other.g) && + self.b.fuzzy_eq(&other.b) + } +} + /** @@ -534,6 +545,16 @@ pub impl RGBA: Eq { } } +pub impl RGBA: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &RGBA) -> bool { + self.r.fuzzy_eq(&other.r) && + self.g.fuzzy_eq(&other.g) && + self.b.fuzzy_eq(&other.b) && + self.a.fuzzy_eq(&other.a) + } +} + /** @@ -653,6 +674,15 @@ pub impl HSV: Eq { } } +pub impl HSV: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &HSV) -> bool { + self.h.fuzzy_eq(&other.h) && + self.s.fuzzy_eq(&other.s) && + self.v.fuzzy_eq(&other.v) + } +} + /** @@ -781,3 +811,13 @@ pub impl HSVA: Eq { !(self == other) } } + +pub impl HSVA: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &HSVA) -> bool { + self.h.fuzzy_eq(&other.h) && + self.s.fuzzy_eq(&other.s) && + self.v.fuzzy_eq(&other.v) && + self.a.fuzzy_eq(&other.a) + } +} \ No newline at end of file