Skip to content

Commit f9d2512

Browse files
committed
implement all min/max fns in terms of <=/is_le
1 parent 99768c8 commit f9d2512

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

library/core/src/cmp.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
981981
where
982982
Self: Sized,
983983
{
984-
max_by(self, other, Ord::cmp)
984+
if self <= other { other } else { self }
985985
}
986986

987987
/// Compares and returns the minimum of two values.
@@ -1002,7 +1002,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
10021002
where
10031003
Self: Sized,
10041004
{
1005-
min_by(self, other, Ord::cmp)
1005+
if self <= other { self } else { other }
10061006
}
10071007

10081008
/// Restrict a value to a certain interval.
@@ -1441,10 +1441,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
14411441
#[must_use]
14421442
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
14431443
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1444-
match compare(&v1, &v2) {
1445-
Ordering::Less | Ordering::Equal => v1,
1446-
Ordering::Greater => v2,
1447-
}
1444+
if compare(&v1, &v2).is_le() { v1 } else { v2 }
14481445
}
14491446

14501447
/// Returns the element that gives the minimum value from the specified function.
@@ -1466,7 +1463,7 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
14661463
#[must_use]
14671464
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
14681465
pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1469-
min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
1466+
if f(&v1) <= f(&v2) { v1 } else { v2 }
14701467
}
14711468

14721469
/// Compares and returns the maximum of two values.
@@ -1510,10 +1507,7 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
15101507
#[must_use]
15111508
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
15121509
pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1513-
match compare(&v1, &v2) {
1514-
Ordering::Less | Ordering::Equal => v2,
1515-
Ordering::Greater => v1,
1516-
}
1510+
if compare(&v1, &v2).is_le() { v2 } else { v1 }
15171511
}
15181512

15191513
/// Returns the element that gives the maximum value from the specified function.
@@ -1535,7 +1529,7 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
15351529
#[must_use]
15361530
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
15371531
pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1538-
max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
1532+
if f(&v1) <= f(&v2) { v2 } else { v1 }
15391533
}
15401534

15411535
/// Compares and sorts two values, returning minimum and maximum.
@@ -1620,7 +1614,7 @@ where
16201614
F: FnMut(&T) -> K,
16211615
K: Ord,
16221616
{
1623-
minmax_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
1617+
if f(&v1) <= f(&v2) { [v1, v2] } else { [v2, v1] }
16241618
}
16251619

16261620
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types

0 commit comments

Comments
 (0)