Skip to content

Commit 9c22f65

Browse files
committed
auto merge of #7799 : blake2-ppc/rust/eq-default, r=sanxiyn
Let Eq::ne be implemented to the inverse of eq by default.
2 parents 60d5bb9 + 6999b53 commit 9c22f65

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/libstd/cmp.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and `Eq` to overload the `==` and `!=` operators.
2121
*/
2222

2323
#[allow(missing_doc)];
24+
#[allow(default_methods)]; // NOTE: Remove when allowed in stage0
2425

2526
/**
2627
* Trait for values that can be compared for equality and inequality.
@@ -29,12 +30,14 @@ and `Eq` to overload the `==` and `!=` operators.
2930
* unequal. For example, with the built-in floating-point types `a == b` and `a != b` will both
3031
* evaluate to false if either `a` or `b` is NaN (cf. IEEE 754-2008 section 5.11).
3132
*
33+
* Eq only requires the `eq` method to be implemented; `ne` is its negation by default.
34+
*
3235
* Eventually, this will be implemented by default for types that implement `TotalEq`.
3336
*/
3437
#[lang="eq"]
3538
pub trait Eq {
3639
fn eq(&self, other: &Self) -> bool;
37-
fn ne(&self, other: &Self) -> bool;
40+
fn ne(&self, other: &Self) -> bool { !self.eq(other) }
3841
}
3942

4043
/// 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 {
164167
* for compatibility with floating-point NaN semantics
165168
* (cf. IEEE 754-2008 section 5.11).
166169
*/
167-
#[allow(default_methods)] // NOTE: Remove when allowed in stage0
168170
#[lang="ord"]
169171
pub trait Ord {
170172
fn lt(&self, other: &Self) -> bool;

src/test/run-pass/cmp-default.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Test default methods in Ord
11+
// Test default methods in Ord and Eq
1212
//
13+
struct Fool(bool);
14+
15+
impl Eq for Fool {
16+
fn eq(&self, other: &Fool) -> bool {
17+
**self != **other
18+
}
19+
}
20+
1321
struct Int(int);
1422

1523
impl Ord for Int {
@@ -40,4 +48,9 @@ pub fn main() {
4048
assert!(RevInt(1) > RevInt(2));
4149
assert!(RevInt(1) >= RevInt(2));
4250
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));
4356
}

0 commit comments

Comments
 (0)