diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-03-27 15:20:44 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-03-27 17:29:10 -0400 |
| commit | d2b267bcb5ac1adbc20bca0c170106ae3b3538ab (patch) | |
| tree | 1e121e3e6efee7d72ef425bf3211126136667982 /src/libcore/cmp.rs | |
| parent | 91cb6687a8869fa14aef8d978fb13e330c711cd3 (diff) | |
| download | rust-d2b267bcb5ac1adbc20bca0c170106ae3b3538ab.tar.gz rust-d2b267bcb5ac1adbc20bca0c170106ae3b3538ab.zip | |
add a TotalEq trait
Diffstat (limited to 'src/libcore/cmp.rs')
| -rw-r--r-- | src/libcore/cmp.rs | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index bc5b4b7f148..95f6f9bc1b5 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -21,15 +21,13 @@ and `Eq` to overload the `==` and `!=` operators. */ /** -* Trait for values that can be compared for equality -* and inequality. +* Trait for values that can be compared for equality and inequality. * -* Eventually this may be simplified to only require -* an `eq` method, with the other generated from -* a default implementation. However it should -* remain possible to implement `ne` separately, for -* compatibility with floating-point NaN semantics -* (cf. IEEE 754-2008 section 5.11). +* This trait allows partial equality, where types can be unordered instead of strictly equal or +* unequal. For example, with the built-in floating-point types `a == b` and `a != b` will both +* evaluate to false if either `a` or `b` is NaN (cf. IEEE 754-2008 section 5.11). +* +* Eventually, this will be implemented by default for types that implement `TotalEq`. */ #[lang="eq"] pub trait Eq { @@ -37,11 +35,40 @@ pub trait Eq { fn ne(&self, other: &Self) -> bool; } +/// Trait for equality comparisons where `a == b` and `a != b` are strict inverses. +pub trait TotalEq { + fn equals(&self, other: &Self) -> bool; +} + +macro_rules! totaleq_impl( + ($t:ty) => { + impl TotalEq for $t { + #[inline(always)] + fn equals(&self, other: &$t) -> bool { *self == *other } + } + } +) + +totaleq_impl!(bool) + +totaleq_impl!(u8) +totaleq_impl!(u16) +totaleq_impl!(u32) +totaleq_impl!(u64) + +totaleq_impl!(i8) +totaleq_impl!(i16) +totaleq_impl!(i32) +totaleq_impl!(i64) + +totaleq_impl!(int) +totaleq_impl!(uint) + #[deriving(Eq)] pub enum Ordering { Less, Equal, Greater } /// Trait for types that form a total order -pub trait TotalOrd { +pub trait TotalOrd: TotalEq { fn cmp(&self, other: &Self) -> Ordering; } @@ -140,11 +167,17 @@ pub fn max<T:Ord>(v1: T, v2: T) -> T { #[cfg(test)] mod test { #[test] - fn test_int() { + fn test_int_totalord() { assert_eq!(5.cmp(&10), Less); assert_eq!(10.cmp(&5), Greater); assert_eq!(5.cmp(&5), Equal); assert_eq!((-5).cmp(&12), Less); assert_eq!(12.cmp(-5), Greater); } + + #[test] + fn test_int_totaleq() { + fail_unless!(5.equals(&5)); + fail_unless!(!2.equals(&17)); + } } |
