about summary refs log tree commit diff
path: root/src/libcore/tuple.rs
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2014-06-17 23:25:51 -0700
committerSteven Fackler <sfackler@gmail.com>2014-06-29 21:42:09 -0700
commit55cae0a094bbdcd0e9d5e697ce4f38cbd783bbc7 (patch)
tree3385d84daae977e0d2bf08decdaf807e1d03337d /src/libcore/tuple.rs
parentbb5695b95c288c442dbe528f7e1c1b08f79f033d (diff)
downloadrust-55cae0a094bbdcd0e9d5e697ce4f38cbd783bbc7.tar.gz
rust-55cae0a094bbdcd0e9d5e697ce4f38cbd783bbc7.zip
Implement RFC#28: Add PartialOrd::partial_cmp
I ended up altering the semantics of Json's PartialOrd implementation.
It used to be the case that Null < Null, but I can't think of any reason
for an ordering other than the default one so I just switched it over to
using the derived implementation.

This also fixes broken `PartialOrd` implementations for `Vec` and
`TreeMap`.

RFC: 0028-partial-cmp
Diffstat (limited to 'src/libcore/tuple.rs')
-rw-r--r--src/libcore/tuple.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/libcore/tuple.rs b/src/libcore/tuple.rs
index f44bce33547..0e3722894bc 100644
--- a/src/libcore/tuple.rs
+++ b/src/libcore/tuple.rs
@@ -64,6 +64,7 @@
 use clone::Clone;
 use cmp::*;
 use default::Default;
+use option::{Option, Some};
 
 // macro for implementing n-ary tuple functions and operations
 macro_rules! tuple_impls {
@@ -126,6 +127,10 @@ macro_rules! tuple_impls {
 
             impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
                 #[inline]
+                fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
+                    lexical_partial_cmp!($(self.$refN(), other.$refN()),+)
+                }
+                #[inline]
                 fn lt(&self, other: &($($T,)+)) -> bool {
                     lexical_ord!(lt, $(self.$refN(), other.$refN()),+)
                 }
@@ -172,6 +177,16 @@ macro_rules! lexical_ord {
     ($rel: ident, $a:expr, $b:expr) => { (*$a) . $rel ($b) };
 }
 
+macro_rules! lexical_partial_cmp {
+    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
+        match ($a).partial_cmp($b) {
+            Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
+            ordering   => ordering
+        }
+    };
+    ($a:expr, $b:expr) => { ($a).partial_cmp($b) };
+}
+
 macro_rules! lexical_cmp {
     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
         match ($a).cmp($b) {