diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-04-07 00:49:10 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-04-07 01:02:51 -0400 |
| commit | a3f40184bdc0d094ac76b15f66bfbf87a699a705 (patch) | |
| tree | 0b095ee097ce8b517ed215ad66b21060967fc31b /src | |
| parent | c47d80304e7b3726352183f466ab0756c24d7431 (diff) | |
| download | rust-a3f40184bdc0d094ac76b15f66bfbf87a699a705.tar.gz rust-a3f40184bdc0d094ac76b15f66bfbf87a699a705.zip | |
cmp: add Ord+TotalOrd impls for Ordering itself
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/cmp.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index fa2fcd18560..f96575aaf41 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -65,13 +65,31 @@ totaleq_impl!(int) totaleq_impl!(uint) #[deriving(Clone, Eq)] -pub enum Ordering { Less, Equal, Greater } +pub enum Ordering { Less = -1, Equal = 0, Greater = 1 } /// Trait for types that form a total order pub trait TotalOrd: TotalEq { fn cmp(&self, other: &Self) -> Ordering; } +impl TotalOrd for Ordering { + #[inline(always)] + fn cmp(&self, other: &Ordering) -> Ordering { + (*self as int).cmp(&(*other as int)) + } +} + +impl Ord for Ordering { + #[inline(always)] + fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) } + #[inline(always)] + fn le(&self, other: &Ordering) -> bool { (*self as int) <= (*other as int) } + #[inline(always)] + fn gt(&self, other: &Ordering) -> bool { (*self as int) > (*other as int) } + #[inline(always)] + fn ge(&self, other: &Ordering) -> bool { (*self as int) >= (*other as int) } +} + macro_rules! totalord_impl( ($t:ty) => { impl TotalOrd for $t { @@ -180,4 +198,10 @@ mod test { assert!(5.equals(&5)); assert!(!2.equals(&17)); } + + #[test] + fn test_ordering_order() { + assert!(Less < Equal); + assert_eq!(Greater.cmp(&Less), Greater); + } } |
