From 4e1c2158f29fbb6c2bdb4d50f1fdc6a30685bc3c Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 8 Mar 2014 01:10:32 -0500 Subject: cmp: switch `min` and `max` to `TotalOrd` The `Float` trait provides correct `min` and `max` methods on floating point types, providing a consistent result regardless of the order the parameters are passed. These generic functions do not take the necessary performance hit to correctly support a partial order, so the true requirement should be given as a type bound. Closes #12712 --- src/libstd/cmp.rs | 4 ++-- src/libstd/iter.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index 6975c9da3f0..023b2028703 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -184,12 +184,12 @@ pub trait Equiv { } #[inline] -pub fn min(v1: T, v2: T) -> T { +pub fn min(v1: T, v2: T) -> T { if v1 < v2 { v1 } else { v2 } } #[inline] -pub fn max(v1: T, v2: T) -> T { +pub fn max(v1: T, v2: T) -> T { if v1 > v2 { v1 } else { v2 } } diff --git a/src/libstd/iter.rs b/src/libstd/iter.rs index 11d7bc6c1bf..6bcac425420 100644 --- a/src/libstd/iter.rs +++ b/src/libstd/iter.rs @@ -68,7 +68,7 @@ use cmp; use num::{Zero, One, CheckedAdd, CheckedSub, Saturating, ToPrimitive, Int}; use option::{Option, Some, None}; use ops::{Add, Mul, Sub}; -use cmp::{Eq, Ord}; +use cmp::{Eq, Ord, TotalOrd}; use clone::Clone; use uint; use mem; @@ -626,7 +626,7 @@ pub trait Iterator { /// assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10); /// ``` #[inline] - fn max_by(&mut self, f: |&A| -> B) -> Option { + fn max_by(&mut self, f: |&A| -> B) -> Option { self.fold(None, |max: Option<(A, B)>, x| { let x_val = f(&x); match max { @@ -650,7 +650,7 @@ pub trait Iterator { /// assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0); /// ``` #[inline] - fn min_by(&mut self, f: |&A| -> B) -> Option { + fn min_by(&mut self, f: |&A| -> B) -> Option { self.fold(None, |min: Option<(A, B)>, x| { let x_val = f(&x); match min { @@ -917,7 +917,7 @@ pub trait OrdIterator { fn min_max(&mut self) -> MinMaxResult; } -impl> OrdIterator for T { +impl> OrdIterator for T { #[inline] fn max(&mut self) -> Option { self.fold(None, |max, x| { -- cgit 1.4.1-3-g733a5