about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-07-15 05:01:19 -0700
committerbors <bors@rust-lang.org>2013-07-15 05:01:19 -0700
commit9c22f6587049a13c74e1c07e0f6590ba356a3be9 (patch)
tree36c0f14677d327512f4e1f0f299b0c3dbaddc3aa
parent60d5bb941324f1c1faee9ff60360e169a410e87b (diff)
parent6999b5332f845292c4fdab824ae0fe2e3bf53421 (diff)
downloadrust-9c22f6587049a13c74e1c07e0f6590ba356a3be9.tar.gz
rust-9c22f6587049a13c74e1c07e0f6590ba356a3be9.zip
auto merge of #7799 : blake2-ppc/rust/eq-default, r=sanxiyn
Let Eq::ne be implemented to the inverse of eq by default.
-rw-r--r--src/libstd/cmp.rs6
-rw-r--r--src/test/run-pass/cmp-default.rs15
2 files changed, 18 insertions, 3 deletions
diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs
index 77d4e945aae..8a13cab28c3 100644
--- a/src/libstd/cmp.rs
+++ b/src/libstd/cmp.rs
@@ -21,6 +21,7 @@ and `Eq` to overload the `==` and `!=` operators.
 */
 
 #[allow(missing_doc)];
+#[allow(default_methods)]; // NOTE: Remove when allowed in stage0
 
 /**
 * Trait for values that can be compared for equality and inequality.
@@ -29,12 +30,14 @@ and `Eq` to overload the `==` and `!=` operators.
 * 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).
 *
+* Eq only requires the `eq` method to be implemented; `ne` is its negation by default.
+*
 * Eventually, this will be implemented by default for types that implement `TotalEq`.
 */
 #[lang="eq"]
 pub trait Eq {
     fn eq(&self, other: &Self) -> bool;
-    fn ne(&self, other: &Self) -> bool;
+    fn ne(&self, other: &Self) -> bool { !self.eq(other) }
 }
 
 /// 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 {
 * for compatibility with floating-point NaN semantics
 * (cf. IEEE 754-2008 section 5.11).
 */
-#[allow(default_methods)] // NOTE: Remove when allowed in stage0
 #[lang="ord"]
 pub trait Ord {
     fn lt(&self, other: &Self) -> bool;
diff --git a/src/test/run-pass/cmp-default.rs b/src/test/run-pass/cmp-default.rs
index 0dc5e7eb6ce..92b14dc64b8 100644
--- a/src/test/run-pass/cmp-default.rs
+++ b/src/test/run-pass/cmp-default.rs
@@ -8,8 +8,16 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// Test default methods in Ord
+// Test default methods in Ord and Eq
 //
+struct Fool(bool);
+
+impl Eq for Fool {
+    fn eq(&self, other: &Fool) -> bool {
+        **self != **other
+    }
+}
+
 struct Int(int);
 
 impl Ord for Int {
@@ -40,4 +48,9 @@ pub fn main() {
     assert!(RevInt(1) >  RevInt(2));
     assert!(RevInt(1) >= RevInt(2));
     assert!(RevInt(1) >= RevInt(1));
+
+    assert!(Fool(true)  == Fool(false));
+    assert!(Fool(true)  != Fool(true));
+    assert!(Fool(false) != Fool(false));
+    assert!(Fool(false) == Fool(true));
 }