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
This commit is contained in:
Steven Fackler 2014-06-17 23:25:51 -07:00
parent bb5695b95c
commit 55cae0a094
28 changed files with 343 additions and 161 deletions

View file

@ -107,8 +107,8 @@ impl<K: Ord, V: Eq> PartialEq for BTree<K, V> {
impl<K: Ord, V: Eq> Eq for BTree<K, V> {}
impl<K: Ord, V: Eq> PartialOrd for BTree<K, V> {
fn lt(&self, other: &BTree<K, V>) -> bool {
self.cmp(other) == Less
fn partial_cmp(&self, other: &BTree<K, V>) -> Option<Ordering> {
Some(self.cmp(other))
}
}
@ -229,8 +229,8 @@ impl<K: Ord, V: Eq> PartialEq for Node<K, V> {
impl<K: Ord, V: Eq> Eq for Node<K, V> {}
impl<K: Ord, V: Eq> PartialOrd for Node<K, V> {
fn lt(&self, other: &Node<K, V>) -> bool {
self.cmp(other) == Less
fn partial_cmp(&self, other: &Node<K, V>) -> Option<Ordering> {
Some(self.cmp(other))
}
}
@ -408,8 +408,8 @@ impl<K: Ord, V: Eq> PartialEq for Leaf<K, V> {
impl<K: Ord, V: Eq> Eq for Leaf<K, V> {}
impl<K: Ord, V: Eq> PartialOrd for Leaf<K, V> {
fn lt(&self, other: &Leaf<K, V>) -> bool {
self.cmp(other) == Less
fn partial_cmp(&self, other: &Leaf<K, V>) -> Option<Ordering> {
Some(self.cmp(other))
}
}
@ -638,8 +638,8 @@ impl<K: Ord, V: Eq> PartialEq for Branch<K, V> {
impl<K: Ord, V: Eq> Eq for Branch<K, V> {}
impl<K: Ord, V: Eq> PartialOrd for Branch<K, V> {
fn lt(&self, other: &Branch<K, V>) -> bool {
self.cmp(other) == Less
fn partial_cmp(&self, other: &Branch<K, V>) -> Option<Ordering> {
Some(self.cmp(other))
}
}
@ -706,8 +706,8 @@ impl<K: Ord, V: Eq> PartialEq for LeafElt<K, V> {
impl<K: Ord, V: Eq> Eq for LeafElt<K, V> {}
impl<K: Ord, V: Eq> PartialOrd for LeafElt<K, V> {
fn lt(&self, other: &LeafElt<K, V>) -> bool {
self.cmp(other) == Less
fn partial_cmp(&self, other: &LeafElt<K, V>) -> Option<Ordering> {
Some(self.cmp(other))
}
}
@ -755,8 +755,8 @@ impl<K: Ord, V: Eq> PartialEq for BranchElt<K, V>{
impl<K: Ord, V: Eq> Eq for BranchElt<K, V>{}
impl<K: Ord, V: Eq> PartialOrd for BranchElt<K, V> {
fn lt(&self, other: &BranchElt<K, V>) -> bool {
self.cmp(other) == Less
fn partial_cmp(&self, other: &BranchElt<K, V>) -> Option<Ordering> {
Some(self.cmp(other))
}
}

View file

@ -595,17 +595,8 @@ impl<A: PartialEq> PartialEq for DList<A> {
}
impl<A: PartialOrd> PartialOrd for DList<A> {
fn lt(&self, other: &DList<A>) -> bool {
iter::order::lt(self.iter(), other.iter())
}
fn le(&self, other: &DList<A>) -> bool {
iter::order::le(self.iter(), other.iter())
}
fn gt(&self, other: &DList<A>) -> bool {
iter::order::gt(self.iter(), other.iter())
}
fn ge(&self, other: &DList<A>) -> bool {
iter::order::ge(self.iter(), other.iter())
fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
}
}

View file

@ -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<Ordering> {
Some(self.cmp(other))
}
}

View file

@ -56,23 +56,11 @@ impl<K: PartialEq + Ord, V: PartialEq> PartialEq for TreeMap<K, V> {
}
}
// Lexicographical comparison
fn lt<K: PartialOrd + Ord, V: PartialOrd>(a: &TreeMap<K, V>,
b: &TreeMap<K, V>) -> 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<K: PartialOrd + Ord, V: PartialOrd> PartialOrd for TreeMap<K, V> {
impl<K: Ord, V: PartialOrd> PartialOrd for TreeMap<K, V> {
#[inline]
fn lt(&self, other: &TreeMap<K, V>) -> bool { lt(self, other) }
fn partial_cmp(&self, other: &TreeMap<K, V>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
}
}
impl<K: Ord + Show, V: Show> Show for TreeMap<K, V> {
@ -568,9 +556,11 @@ impl<T: PartialEq + Ord> PartialEq for TreeSet<T> {
fn eq(&self, other: &TreeSet<T>) -> bool { self.map == other.map }
}
impl<T: PartialOrd + Ord> PartialOrd for TreeSet<T> {
impl<T: Ord> PartialOrd for TreeSet<T> {
#[inline]
fn lt(&self, other: &TreeSet<T>) -> bool { self.map < other.map }
fn partial_cmp(&self, other: &TreeSet<T>) -> Option<Ordering> {
self.map.partial_cmp(&other.map)
}
}
impl<T: Ord + Show> Show for TreeSet<T> {

View file

@ -389,8 +389,8 @@ impl<T: PartialEq> PartialEq for Vec<T> {
impl<T: PartialOrd> PartialOrd for Vec<T> {
#[inline]
fn lt(&self, other: &Vec<T>) -> bool {
self.as_slice() < other.as_slice()
fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
self.as_slice().partial_cmp(&other.as_slice())
}
}