Implement FuzzyEq for Angle and Color types

This commit is contained in:
Brendan Zabarauskas 2012-12-12 14:19:19 +10:00
parent 3a3034b7b6
commit bc9104fdb5
2 changed files with 59 additions and 2 deletions

View file

@ -1,5 +1,7 @@
use core::cmp::{Eq, Ord}; use core::cmp::{Eq, Ord};
use std::cmp::FuzzyEq;
use funs::triganomic::{cos, sin}; use funs::triganomic::{cos, sin};
use mat::{Mat3, Mat4}; use mat::{Mat3, Mat4};
use num::conv::cast; use num::conv::cast;
@ -18,6 +20,7 @@ pub trait Angle<T>: Add<self,self>
Modulo<T,self> Modulo<T,self>
// Modulo<self,T> // TODO: not sure how to implement this, or if it is even possible... // Modulo<self,T> // TODO: not sure how to implement this, or if it is even possible...
Neg<self> Neg<self>
FuzzyEq
Eq Ord { Eq Ord {
static pure fn full_turn() -> self; static pure fn full_turn() -> self;
static pure fn half_turn() -> self; static pure fn half_turn() -> self;
@ -35,7 +38,7 @@ pub trait Angle<T>: Add<self,self>
// pub struct Radians<T>(T); // error: internal compiler error: deref_cat() invoked on non-derefable type angle::Radians<'a>
pub enum Radians<T> = T; pub enum Radians<T> = T;
pub impl<T:Copy Float> Radians<T>: Angle<T> { pub impl<T:Copy Float> Radians<T>: Angle<T> {
@ -109,6 +112,13 @@ pub impl<T:Copy Float> Radians<T>: Neg<Radians<T>> {
} }
} }
pub impl<T:Copy Float> Radians<T>: FuzzyEq {
#[inline(always)]
pure fn fuzzy_eq(other: &Radians<T>) -> bool {
(*self).fuzzy_eq(&**other)
}
}
pub impl<T:Copy Float> Radians<T>: Eq { pub impl<T:Copy Float> Radians<T>: Eq {
#[inline(always)] pure fn eq(&self, other: &Radians<T>) -> bool { **self == **other } #[inline(always)] pure fn eq(&self, other: &Radians<T>) -> bool { **self == **other }
#[inline(always)] pure fn ne(&self, other: &Radians<T>) -> bool { **self != **other } #[inline(always)] pure fn ne(&self, other: &Radians<T>) -> bool { **self != **other }
@ -136,9 +146,9 @@ pub impl<T> Radians<T>: ToStr {
// pub struct Degrees<T>(T); // error: internal compiler error: deref_cat() invoked on non-derefable type angle::Degrees<'a>
pub enum Degrees<T> = T; pub enum Degrees<T> = T;
// FIXME: not sure why I need the Eq and Ord trait bounds, but Rust complains if I don't include them
pub impl<T:Copy Float> Degrees<T>: Angle<T> { pub impl<T:Copy Float> Degrees<T>: Angle<T> {
#[inline(always)] static pure fn full_turn() -> Degrees<T> { Degrees(cast(360.0)) } #[inline(always)] static pure fn full_turn() -> Degrees<T> { Degrees(cast(360.0)) }
#[inline(always)] static pure fn half_turn() -> Degrees<T> { Degrees(cast(180.0)) } #[inline(always)] static pure fn half_turn() -> Degrees<T> { Degrees(cast(180.0)) }
@ -210,6 +220,13 @@ pub impl<T:Copy Float> Degrees<T>: Neg<Degrees<T>> {
} }
} }
pub impl<T:Copy Float> Degrees<T>: FuzzyEq {
#[inline(always)]
pure fn fuzzy_eq(other: &Degrees<T>) -> bool {
(*self).fuzzy_eq(&**other)
}
}
pub impl<T:Copy Float> Degrees<T>: Eq { pub impl<T:Copy Float> Degrees<T>: Eq {
#[inline(always)] pure fn eq(&self, other: &Degrees<T>) -> bool { **self == **other } #[inline(always)] pure fn eq(&self, other: &Degrees<T>) -> bool { **self == **other }
#[inline(always)] pure fn ne(&self, other: &Degrees<T>) -> bool { **self != **other } #[inline(always)] pure fn ne(&self, other: &Degrees<T>) -> bool { **self != **other }

View file

@ -4,6 +4,8 @@ use core::ptr::to_unsafe_ptr;
use core::sys::size_of; use core::sys::size_of;
use core::vec::raw::buf_as_slice; use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq;
use angle::Degrees; use angle::Degrees;
use channel::Channel; use channel::Channel;
use funs::common::Sign; use funs::common::Sign;
@ -380,6 +382,15 @@ pub impl<T:Copy Eq> RGB<T>: Eq {
} }
} }
pub impl<T:Copy Float> RGB<T>: FuzzyEq {
#[inline(always)]
pure fn fuzzy_eq(other: &RGB<T>) -> 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<T:Copy Eq> RGBA<T>: Eq {
} }
} }
pub impl<T:Copy Float> RGBA<T>: FuzzyEq {
#[inline(always)]
pure fn fuzzy_eq(other: &RGBA<T>) -> 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<T:Copy Float> HSV<T>: Eq {
} }
} }
pub impl<T:Copy Float> HSV<T>: FuzzyEq {
#[inline(always)]
pure fn fuzzy_eq(other: &HSV<T>) -> 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<T:Copy Float> HSVA<T>: Eq {
!(self == other) !(self == other)
} }
} }
pub impl<T:Copy Float> HSVA<T>: FuzzyEq {
#[inline(always)]
pure fn fuzzy_eq(other: &HSVA<T>) -> 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)
}
}