]> git.lizzy.rs Git - rust.git/commitdiff
cmp: Use default methods in trait Eq, require only Eq::eq
authorblake2-ppc <blake2-ppc>
Mon, 15 Jul 2013 02:00:39 +0000 (04:00 +0200)
committerblake2-ppc <blake2-ppc>
Mon, 15 Jul 2013 11:24:35 +0000 (13:24 +0200)
src/libstd/cmp.rs
src/test/run-pass/cmp-default.rs

index 77d4e945aae1706cf58d7b80caf23a13b32c2f5f..8a13cab28c3bf782afa38b5a4c73e2fbda2fe7e6 100644 (file)
@@ -21,6 +21,7 @@
 */
 
 #[allow(missing_doc)];
+#[allow(default_methods)]; // NOTE: Remove when allowed in stage0
 
 /**
 * Trait for values that can be compared for equality and inequality.
 * 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;
index 0dc5e7eb6ce22956afd97e63b786eb95113f9ca5..92b14dc64b8cf8b808dbaf0815fd3920ca561895 100644 (file)
@@ -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));
 }