From 55cae0a094bbdcd0e9d5e697ce4f38cbd783bbc7 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 17 Jun 2014 23:25:51 -0700 Subject: 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 --- src/libcollections/btree.rs | 24 ++++++++++++------------ src/libcollections/dlist.rs | 13 ++----------- src/libcollections/str.rs | 4 ++-- src/libcollections/treemap.rs | 26 ++++++++------------------ src/libcollections/vec.rs | 4 ++-- 5 files changed, 26 insertions(+), 45 deletions(-) (limited to 'src/libcollections') diff --git a/src/libcollections/btree.rs b/src/libcollections/btree.rs index 64bee05a379..92abfaad348 100644 --- a/src/libcollections/btree.rs +++ b/src/libcollections/btree.rs @@ -107,8 +107,8 @@ impl PartialEq for BTree { impl Eq for BTree {} impl PartialOrd for BTree { - fn lt(&self, other: &BTree) -> bool { - self.cmp(other) == Less + fn partial_cmp(&self, other: &BTree) -> Option { + Some(self.cmp(other)) } } @@ -229,8 +229,8 @@ impl PartialEq for Node { impl Eq for Node {} impl PartialOrd for Node { - fn lt(&self, other: &Node) -> bool { - self.cmp(other) == Less + fn partial_cmp(&self, other: &Node) -> Option { + Some(self.cmp(other)) } } @@ -408,8 +408,8 @@ impl PartialEq for Leaf { impl Eq for Leaf {} impl PartialOrd for Leaf { - fn lt(&self, other: &Leaf) -> bool { - self.cmp(other) == Less + fn partial_cmp(&self, other: &Leaf) -> Option { + Some(self.cmp(other)) } } @@ -638,8 +638,8 @@ impl PartialEq for Branch { impl Eq for Branch {} impl PartialOrd for Branch { - fn lt(&self, other: &Branch) -> bool { - self.cmp(other) == Less + fn partial_cmp(&self, other: &Branch) -> Option { + Some(self.cmp(other)) } } @@ -706,8 +706,8 @@ impl PartialEq for LeafElt { impl Eq for LeafElt {} impl PartialOrd for LeafElt { - fn lt(&self, other: &LeafElt) -> bool { - self.cmp(other) == Less + fn partial_cmp(&self, other: &LeafElt) -> Option { + Some(self.cmp(other)) } } @@ -755,8 +755,8 @@ impl PartialEq for BranchElt{ impl Eq for BranchElt{} impl PartialOrd for BranchElt { - fn lt(&self, other: &BranchElt) -> bool { - self.cmp(other) == Less + fn partial_cmp(&self, other: &BranchElt) -> Option { + Some(self.cmp(other)) } } diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index 7ea5c482e61..4114c8cb1c4 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -595,17 +595,8 @@ impl PartialEq for DList { } impl PartialOrd for DList { - fn lt(&self, other: &DList) -> bool { - iter::order::lt(self.iter(), other.iter()) - } - fn le(&self, other: &DList) -> bool { - iter::order::le(self.iter(), other.iter()) - } - fn gt(&self, other: &DList) -> bool { - iter::order::gt(self.iter(), other.iter()) - } - fn ge(&self, other: &DList) -> bool { - iter::order::ge(self.iter(), other.iter()) + fn partial_cmp(&self, other: &DList) -> Option { + iter::order::partial_cmp(self.iter(), other.iter()) } } diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 2d84c733b09..b5424d1683f 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -572,8 +572,8 @@ impl<'a> Eq for MaybeOwned<'a> {} impl<'a> PartialOrd for MaybeOwned<'a> { #[inline] - fn lt(&self, other: &MaybeOwned) -> bool { - self.as_slice().lt(&other.as_slice()) + fn partial_cmp(&self, other: &MaybeOwned) -> Option { + Some(self.cmp(other)) } } diff --git a/src/libcollections/treemap.rs b/src/libcollections/treemap.rs index 568baa6fc40..becceffe6d0 100644 --- a/src/libcollections/treemap.rs +++ b/src/libcollections/treemap.rs @@ -56,23 +56,11 @@ impl PartialEq for TreeMap { } } -// Lexicographical comparison -fn lt(a: &TreeMap, - b: &TreeMap) -> bool { - // the Zip iterator is as long as the shortest of a and b. - for ((key_a, value_a), (key_b, value_b)) in a.iter().zip(b.iter()) { - if *key_a < *key_b { return true; } - if *key_a > *key_b { return false; } - if *value_a < *value_b { return true; } - if *value_a > *value_b { return false; } - } - - a.len() < b.len() -} - -impl PartialOrd for TreeMap { +impl PartialOrd for TreeMap { #[inline] - fn lt(&self, other: &TreeMap) -> bool { lt(self, other) } + fn partial_cmp(&self, other: &TreeMap) -> Option { + iter::order::partial_cmp(self.iter(), other.iter()) + } } impl Show for TreeMap { @@ -568,9 +556,11 @@ impl PartialEq for TreeSet { fn eq(&self, other: &TreeSet) -> bool { self.map == other.map } } -impl PartialOrd for TreeSet { +impl PartialOrd for TreeSet { #[inline] - fn lt(&self, other: &TreeSet) -> bool { self.map < other.map } + fn partial_cmp(&self, other: &TreeSet) -> Option { + self.map.partial_cmp(&other.map) + } } impl Show for TreeSet { diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 8e6e86ce36e..2ffc168f82c 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -389,8 +389,8 @@ impl PartialEq for Vec { impl PartialOrd for Vec { #[inline] - fn lt(&self, other: &Vec) -> bool { - self.as_slice() < other.as_slice() + fn partial_cmp(&self, other: &Vec) -> Option { + self.as_slice().partial_cmp(&other.as_slice()) } } -- cgit 1.4.1-3-g733a5