diff options
| author | Steven Fackler <sfackler@gmail.com> | 2014-06-17 23:25:51 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2014-06-29 21:42:09 -0700 |
| commit | 55cae0a094bbdcd0e9d5e697ce4f38cbd783bbc7 (patch) | |
| tree | 3385d84daae977e0d2bf08decdaf807e1d03337d /src/libcollections | |
| parent | bb5695b95c288c442dbe528f7e1c1b08f79f033d (diff) | |
| download | rust-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/libcollections')
| -rw-r--r-- | src/libcollections/btree.rs | 24 | ||||
| -rw-r--r-- | src/libcollections/dlist.rs | 13 | ||||
| -rw-r--r-- | src/libcollections/str.rs | 4 | ||||
| -rw-r--r-- | src/libcollections/treemap.rs | 26 | ||||
| -rw-r--r-- | src/libcollections/vec.rs | 4 |
5 files changed, 26 insertions, 45 deletions
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<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)) } } 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<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()) } } 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<Ordering> { + 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<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> { 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<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()) } } |
