Skip to content

Commit 36f2042

Browse files
author
blake2-ppc
committed
cmp: Use default methods in trait Ord, only require Ord::lt
It will be simpler to implement only one method for Ord, while we also allow implementing all four Ord methods for semantics or performance reasons. We only supply three default methods (and not four), because don't have any nice error reporting for the case where at least one method must be implemented, but it's arbitrary which.
1 parent 1ee54a8 commit 36f2042

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

src/libstd/cmp.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,20 @@ pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
157157
/**
158158
* Trait for values that can be compared for a sort-order.
159159
*
160-
* Eventually this may be simplified to only require
161-
* an `le` method, with the others generated from
162-
* default implementations. However it should remain
163-
* possible to implement the others separately, for
164-
* compatibility with floating-point NaN semantics
160+
* Ord only requires implementation of the `lt` method,
161+
* with the others generated from default implementations.
162+
*
163+
* However it remains possible to implement the others separately,
164+
* for compatibility with floating-point NaN semantics
165165
* (cf. IEEE 754-2008 section 5.11).
166166
*/
167+
#[allow(default_methods)] // NOTE: Remove when allowed in stage0
167168
#[lang="ord"]
168169
pub trait Ord {
169170
fn lt(&self, other: &Self) -> bool;
170-
fn le(&self, other: &Self) -> bool;
171-
fn ge(&self, other: &Self) -> bool;
172-
fn gt(&self, other: &Self) -> bool;
171+
fn le(&self, other: &Self) -> bool { !other.lt(self) }
172+
fn gt(&self, other: &Self) -> bool { other.lt(self) }
173+
fn ge(&self, other: &Self) -> bool { !self.lt(other) }
173174
}
174175

175176
/// The equivalence relation. Two values may be equivalent even if they are

src/test/compile-fail/issue-3344.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
// except according to those terms.
1010

1111
struct thing(uint);
12-
impl Ord for thing { //~ ERROR missing method `gt`
13-
fn lt(&self, other: &thing) -> bool { **self < **other }
12+
impl Ord for thing { //~ ERROR missing method `lt`
1413
fn le(&self, other: &thing) -> bool { **self < **other }
1514
fn ge(&self, other: &thing) -> bool { **self < **other }
1615
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test default methods in Ord
12+
//
13+
struct Int(int);
14+
15+
impl Ord for Int {
16+
fn lt(&self, other: &Int) -> bool {
17+
**self < **other
18+
}
19+
}
20+
21+
struct RevInt(int);
22+
23+
impl Ord for RevInt {
24+
fn lt(&self, other: &RevInt) -> bool {
25+
**self > **other
26+
}
27+
}
28+
29+
pub fn main() {
30+
assert!(Int(2) > Int(1));
31+
assert!(Int(2) >= Int(1));
32+
assert!(Int(1) >= Int(1));
33+
assert!(Int(1) < Int(2));
34+
assert!(Int(1) <= Int(2));
35+
assert!(Int(1) <= Int(1));
36+
37+
assert!(RevInt(2) < RevInt(1));
38+
assert!(RevInt(2) <= RevInt(1));
39+
assert!(RevInt(1) <= RevInt(1));
40+
assert!(RevInt(1) > RevInt(2));
41+
assert!(RevInt(1) >= RevInt(2));
42+
assert!(RevInt(1) >= RevInt(1));
43+
}

0 commit comments

Comments
 (0)