Skip to content

Commit 86da55e

Browse files
author
blake2-ppc
committed
std: Fix Ord for Option, using iterator::order
1 parent 9cac4cc commit 86da55e

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/libstd/option.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use ops::Add;
4747
use util;
4848
use num::Zero;
4949
use iterator::Iterator;
50+
use iterator;
5051
use str::{StrSlice, OwnedStr};
5152
use to_str::ToStr;
5253
use clone::DeepClone;
@@ -58,31 +59,21 @@ pub enum Option<T> {
5859
Some(T),
5960
}
6061

61-
impl<T:Ord> Ord for Option<T> {
62+
impl<T: Eq + Ord> Ord for Option<T> {
6263
fn lt(&self, other: &Option<T>) -> bool {
63-
match (self, other) {
64-
(&None, &None) => false,
65-
(&None, &Some(_)) => true,
66-
(&Some(_), &None) => false,
67-
(&Some(ref a), &Some(ref b)) => *a < *b
68-
}
64+
iterator::order::lt(self.iter(), other.iter())
6965
}
7066

7167
fn le(&self, other: &Option<T>) -> bool {
72-
match (self, other) {
73-
(&None, &None) => true,
74-
(&None, &Some(_)) => true,
75-
(&Some(_), &None) => false,
76-
(&Some(ref a), &Some(ref b)) => *a <= *b
77-
}
68+
iterator::order::le(self.iter(), other.iter())
7869
}
7970

8071
fn ge(&self, other: &Option<T>) -> bool {
81-
!(self < other)
72+
iterator::order::ge(self.iter(), other.iter())
8273
}
8374

8475
fn gt(&self, other: &Option<T>) -> bool {
85-
!(self <= other)
76+
iterator::order::gt(self.iter(), other.iter())
8677
}
8778
}
8879

@@ -553,6 +544,18 @@ mod tests {
553544
assert!(it.next().is_none());
554545
}
555546

547+
#[test]
548+
fn test_ord() {
549+
let small = Some(1.0);
550+
let big = Some(5.0);
551+
let nan = Some(0.0/0.0);
552+
assert!(!(nan < big));
553+
assert!(!(nan > big));
554+
assert!(small < big);
555+
assert!(None < big);
556+
assert!(big > None);
557+
}
558+
556559
#[test]
557560
fn test_mutate() {
558561
let mut x = Some(3i);

0 commit comments

Comments
 (0)