File tree 2 files changed +18
-3
lines changed
2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ and `Eq` to overload the `==` and `!=` operators.
21
21
*/
22
22
23
23
#[ allow( missing_doc) ] ;
24
+ #[ allow( default_methods) ] ; // NOTE: Remove when allowed in stage0
24
25
25
26
/**
26
27
* Trait for values that can be compared for equality and inequality.
@@ -29,12 +30,14 @@ and `Eq` to overload the `==` and `!=` operators.
29
30
* unequal. For example, with the built-in floating-point types `a == b` and `a != b` will both
30
31
* evaluate to false if either `a` or `b` is NaN (cf. IEEE 754-2008 section 5.11).
31
32
*
33
+ * Eq only requires the `eq` method to be implemented; `ne` is its negation by default.
34
+ *
32
35
* Eventually, this will be implemented by default for types that implement `TotalEq`.
33
36
*/
34
37
#[ lang="eq" ]
35
38
pub trait Eq {
36
39
fn eq ( & self , other : & Self ) -> bool ;
37
- fn ne ( & self , other : & Self ) -> bool ;
40
+ fn ne ( & self , other : & Self ) -> bool { ! self . eq ( other ) }
38
41
}
39
42
40
43
/// Trait for equality comparisons where `a == b` and `a != b` are strict inverses.
@@ -164,7 +167,6 @@ pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
164
167
* for compatibility with floating-point NaN semantics
165
168
* (cf. IEEE 754-2008 section 5.11).
166
169
*/
167
- #[ allow( default_methods) ] // NOTE: Remove when allowed in stage0
168
170
#[ lang="ord" ]
169
171
pub trait Ord {
170
172
fn lt ( & self , other : & Self ) -> bool ;
Original file line number Diff line number Diff line change 8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // Test default methods in Ord
11
+ // Test default methods in Ord and Eq
12
12
//
13
+ struct Fool ( bool ) ;
14
+
15
+ impl Eq for Fool {
16
+ fn eq ( & self , other : & Fool ) -> bool {
17
+ * * self != * * other
18
+ }
19
+ }
20
+
13
21
struct Int ( int ) ;
14
22
15
23
impl Ord for Int {
@@ -40,4 +48,9 @@ pub fn main() {
40
48
assert ! ( RevInt ( 1 ) > RevInt ( 2 ) ) ;
41
49
assert ! ( RevInt ( 1 ) >= RevInt ( 2 ) ) ;
42
50
assert ! ( RevInt ( 1 ) >= RevInt ( 1 ) ) ;
51
+
52
+ assert ! ( Fool ( true ) == Fool ( false ) ) ;
53
+ assert ! ( Fool ( true ) != Fool ( true ) ) ;
54
+ assert ! ( Fool ( false ) != Fool ( false ) ) ;
55
+ assert ! ( Fool ( false ) == Fool ( true ) ) ;
43
56
}
You can’t perform that action at this time.
0 commit comments