Skip to content

Commit 5e570ce

Browse files
committed
auto merge of #5766 : thestinger/rust/cmp, r=brson
It was simpler to just give the variants a value instead of listing out all the cases for (*self, *other) in a match statement or writing spaghetti code. This makes the `cmp` method easier to use with FFI too, since you're a cast away from an idiomatic C comparator function. It would be fine implemented another way though.
2 parents 81ba65d + a3f4018 commit 5e570ce

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/libcore/cmp.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,32 @@ totaleq_impl!(i64)
6464
totaleq_impl!(int)
6565
totaleq_impl!(uint)
6666

67-
#[deriving(Eq)]
68-
pub enum Ordering { Less, Equal, Greater }
67+
#[deriving(Clone, Eq)]
68+
pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }
6969

7070
/// Trait for types that form a total order
7171
pub trait TotalOrd: TotalEq {
7272
fn cmp(&self, other: &Self) -> Ordering;
7373
}
7474

75+
impl TotalOrd for Ordering {
76+
#[inline(always)]
77+
fn cmp(&self, other: &Ordering) -> Ordering {
78+
(*self as int).cmp(&(*other as int))
79+
}
80+
}
81+
82+
impl Ord for Ordering {
83+
#[inline(always)]
84+
fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) }
85+
#[inline(always)]
86+
fn le(&self, other: &Ordering) -> bool { (*self as int) <= (*other as int) }
87+
#[inline(always)]
88+
fn gt(&self, other: &Ordering) -> bool { (*self as int) > (*other as int) }
89+
#[inline(always)]
90+
fn ge(&self, other: &Ordering) -> bool { (*self as int) >= (*other as int) }
91+
}
92+
7593
macro_rules! totalord_impl(
7694
($t:ty) => {
7795
impl TotalOrd for $t {
@@ -180,4 +198,10 @@ mod test {
180198
assert!(5.equals(&5));
181199
assert!(!2.equals(&17));
182200
}
201+
202+
#[test]
203+
fn test_ordering_order() {
204+
assert!(Less < Equal);
205+
assert_eq!(Greater.cmp(&Less), Greater);
206+
}
183207
}

0 commit comments

Comments
 (0)