1- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22// file at the top-level directory of this distribution and at
33// http://rust-lang.org/COPYRIGHT.
44//
@@ -21,86 +21,82 @@ and `Eq` to overload the `==` and `!=` operators.
2121*/
2222
2323/**
24- * Trait for values that can be compared for equality
25- * and inequality.
24+ * Trait for values that can be compared for equality and inequality.
2625*
27- * Eventually this may be simplified to only require
28- * an `eq` method, with the other generated from
29- * a default implementation. However it should
30- * remain possible to implement `ne` separately, for
31- * compatibility with floating-point NaN semantics
32- * (cf. IEEE 754-2008 section 5.11).
26+ * This trait allows partial equality, where types can be unordered instead of strictly equal or
27+ * unequal. For example, with the built-in floating-point types `a == b` and `a != b` will both
28+ * evaluate to false if either `a` or `b` is NaN (cf. IEEE 754-2008 section 5.11).
29+ *
30+ * Eventually, this will be implemented by default for types that implement `TotalEq`.
3331*/
3432#[ lang="eq" ]
3533pub trait Eq {
3634 fn eq ( & self , other : & Self ) -> bool ;
3735 fn ne ( & self , other : & Self ) -> bool ;
3836}
3937
40- #[ deriving( Eq ) ]
41- pub enum Ordering { Less , Equal , Greater }
42-
43- /// Trait for types that form a total order
44- pub trait TotalOrd {
45- fn cmp ( & self , other : & Self ) -> Ordering ;
38+ /// Trait for equality comparisons where `a == b` and `a != b` are strict inverses.
39+ pub trait TotalEq {
40+ fn equals ( & self , other : & Self ) -> bool ;
4641}
4742
48- #[ inline( always) ]
49- fn icmp < T : Ord > ( a : & T , b : & T ) -> Ordering {
50- if * a < * b { Less }
51- else if * a > * b { Greater }
52- else { Equal }
53- }
43+ macro_rules! totaleq_impl(
44+ ( $t: ty) => {
45+ impl TotalEq for $t {
46+ #[ inline( always) ]
47+ fn equals( & self , other: & $t) -> bool { * self == * other }
48+ }
49+ }
50+ )
5451
55- impl TotalOrd for u8 {
56- #[ inline( always) ]
57- fn cmp ( & self , other : & u8 ) -> Ordering { icmp ( self , other) }
58- }
52+ totaleq_impl ! ( bool )
5953
60- impl TotalOrd for u16 {
61- # [ inline ( always ) ]
62- fn cmp ( & self , other : & u16 ) -> Ordering { icmp ( self , other ) }
63- }
54+ totaleq_impl ! ( u8 )
55+ totaleq_impl ! ( u16 )
56+ totaleq_impl ! ( u32 )
57+ totaleq_impl ! ( u64 )
6458
65- impl TotalOrd for u32 {
66- # [ inline ( always ) ]
67- fn cmp ( & self , other : & u32 ) -> Ordering { icmp ( self , other ) }
68- }
59+ totaleq_impl ! ( i8 )
60+ totaleq_impl ! ( i16 )
61+ totaleq_impl ! ( i32 )
62+ totaleq_impl ! ( i64 )
6963
70- impl TotalOrd for u64 {
71- #[ inline( always) ]
72- fn cmp ( & self , other : & u64 ) -> Ordering { icmp ( self , other) }
73- }
64+ totaleq_impl ! ( int)
65+ totaleq_impl ! ( uint)
7466
75- impl TotalOrd for i8 {
76- #[ inline( always) ]
77- fn cmp ( & self , other : & i8 ) -> Ordering { icmp ( self , other) }
78- }
67+ #[ deriving( Eq ) ]
68+ pub enum Ordering { Less , Equal , Greater }
7969
80- impl TotalOrd for i16 {
81- # [ inline ( always ) ]
82- fn cmp ( & self , other : & i16 ) -> Ordering { icmp ( self , other ) }
70+ /// Trait for types that form a total order
71+ pub trait TotalOrd : TotalEq {
72+ fn cmp ( & self , other : & Self ) -> Ordering ;
8373}
8474
85- impl TotalOrd for i32 {
86- #[ inline( always) ]
87- fn cmp ( & self , other : & i32 ) -> Ordering { icmp ( self , other) }
88- }
75+ macro_rules! totalord_impl(
76+ ( $t: ty) => {
77+ impl TotalOrd for $t {
78+ #[ inline( always) ]
79+ fn cmp( & self , other: & $t) -> Ordering {
80+ if * self < * other { Less }
81+ else if * self > * other { Greater }
82+ else { Equal }
83+ }
84+ }
85+ }
86+ )
8987
90- impl TotalOrd for i64 {
91- # [ inline ( always ) ]
92- fn cmp ( & self , other : & i64 ) -> Ordering { icmp ( self , other ) }
93- }
88+ totalord_impl ! ( u8 )
89+ totalord_impl ! ( u16 )
90+ totalord_impl ! ( u32 )
91+ totalord_impl ! ( u64 )
9492
95- impl TotalOrd for int {
96- # [ inline ( always ) ]
97- fn cmp ( & self , other : & int ) -> Ordering { icmp ( self , other ) }
98- }
93+ totalord_impl ! ( i8 )
94+ totalord_impl ! ( i16 )
95+ totalord_impl ! ( i32 )
96+ totalord_impl ! ( i64 )
9997
100- impl TotalOrd for uint {
101- #[ inline( always) ]
102- fn cmp ( & self , other : & uint ) -> Ordering { icmp ( self , other) }
103- }
98+ totalord_impl ! ( int)
99+ totalord_impl ! ( uint)
104100
105101/**
106102* Trait for values that can be compared for a sort-order.
@@ -171,11 +167,17 @@ pub fn max<T:Ord>(v1: T, v2: T) -> T {
171167#[ cfg( test) ]
172168mod test {
173169 #[ test]
174- fn test_int ( ) {
170+ fn test_int_totalord ( ) {
175171 assert_eq ! ( 5 . cmp( & 10 ) , Less ) ;
176172 assert_eq ! ( 10 . cmp( & 5 ) , Greater ) ;
177173 assert_eq ! ( 5 . cmp( & 5 ) , Equal ) ;
178174 assert_eq ! ( ( -5 ) . cmp( & 12 ) , Less ) ;
179175 assert_eq ! ( 12 . cmp( -5 ) , Greater ) ;
180176 }
177+
178+ #[ test]
179+ fn test_int_totaleq ( ) {
180+ fail_unless ! ( 5 . equals( & 5 ) ) ;
181+ fail_unless ! ( !2 . equals( & 17 ) ) ;
182+ }
181183}
0 commit comments