1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -21,86 +21,82 @@ and `Eq` to overload the `==` and `!=` operators.
21
21
*/
22
22
23
23
/**
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.
26
25
*
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`.
33
31
*/
34
32
#[ lang="eq" ]
35
33
pub trait Eq {
36
34
fn eq ( & self , other : & Self ) -> bool ;
37
35
fn ne ( & self , other : & Self ) -> bool ;
38
36
}
39
37
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 ;
46
41
}
47
42
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
+ )
54
51
55
- impl TotalOrd for u8 {
56
- #[ inline( always) ]
57
- fn cmp ( & self , other : & u8 ) -> Ordering { icmp ( self , other) }
58
- }
52
+ totaleq_impl ! ( bool )
59
53
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 )
64
58
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 )
69
63
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)
74
66
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 }
79
69
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 ;
83
73
}
84
74
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
+ )
89
87
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 )
94
92
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 )
99
97
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)
104
100
105
101
/**
106
102
* 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 {
171
167
#[ cfg( test) ]
172
168
mod test {
173
169
#[ test]
174
- fn test_int ( ) {
170
+ fn test_int_totalord ( ) {
175
171
assert_eq ! ( 5 . cmp( & 10 ) , Less ) ;
176
172
assert_eq ! ( 10 . cmp( & 5 ) , Greater ) ;
177
173
assert_eq ! ( 5 . cmp( & 5 ) , Equal ) ;
178
174
assert_eq ! ( ( -5 ) . cmp( & 12 ) , Less ) ;
179
175
assert_eq ! ( 12 . cmp( -5 ) , Greater ) ;
180
176
}
177
+
178
+ #[ test]
179
+ fn test_int_totaleq ( ) {
180
+ fail_unless ! ( 5 . equals( & 5 ) ) ;
181
+ fail_unless ! ( !2 . equals( & 17 ) ) ;
182
+ }
181
183
}
0 commit comments