Skip to content

Commit

Permalink
cmp: Use default methods in trait Eq, require only Eq::eq
Browse files Browse the repository at this point in the history
  • Loading branch information
blake2-ppc committed Jul 15, 2013
1 parent 68a32aa commit 6999b53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/libstd/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion src/test/run-pass/cmp-default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}

5 comments on commit 6999b53

@bors
Copy link
Contributor

@bors bors commented on 6999b53 Jul 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 6999b53 Jul 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging blake2-ppc/rust/eq-default = 6999b53 into auto

@bors
Copy link
Contributor

@bors bors commented on 6999b53 Jul 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blake2-ppc/rust/eq-default = 6999b53 merged ok, testing candidate = 9c22f65

@bors
Copy link
Contributor

@bors bors commented on 6999b53 Jul 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 9c22f65

Please sign in to comment.