From 86da55e85b1f1dd520111b52d1f76c61ea43b382 Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Thu, 8 Aug 2013 22:07:22 +0200 Subject: [PATCH] std: Fix Ord for Option, using iterator::order --- src/libstd/option.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 66b30d8dd031..9af3645c7893 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -47,6 +47,7 @@ use ops::Add; use util; use num::Zero; use iterator::Iterator; +use iterator; use str::{StrSlice, OwnedStr}; use to_str::ToStr; use clone::DeepClone; @@ -58,31 +59,21 @@ pub enum Option { Some(T), } -impl Ord for Option { +impl Ord for Option { fn lt(&self, other: &Option) -> bool { - match (self, other) { - (&None, &None) => false, - (&None, &Some(_)) => true, - (&Some(_), &None) => false, - (&Some(ref a), &Some(ref b)) => *a < *b - } + iterator::order::lt(self.iter(), other.iter()) } fn le(&self, other: &Option) -> bool { - match (self, other) { - (&None, &None) => true, - (&None, &Some(_)) => true, - (&Some(_), &None) => false, - (&Some(ref a), &Some(ref b)) => *a <= *b - } + iterator::order::le(self.iter(), other.iter()) } fn ge(&self, other: &Option) -> bool { - !(self < other) + iterator::order::ge(self.iter(), other.iter()) } fn gt(&self, other: &Option) -> bool { - !(self <= other) + iterator::order::gt(self.iter(), other.iter()) } } @@ -553,6 +544,18 @@ mod tests { assert!(it.next().is_none()); } + #[test] + fn test_ord() { + let small = Some(1.0); + let big = Some(5.0); + let nan = Some(0.0/0.0); + assert!(!(nan < big)); + assert!(!(nan > big)); + assert!(small < big); + assert!(None < big); + assert!(big > None); + } + #[test] fn test_mutate() { let mut x = Some(3i);