@@ -981,7 +981,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
981
981
where
982
982
Self : Sized ,
983
983
{
984
- max_by ( self , other, Ord :: cmp )
984
+ if self <= other { other } else { self }
985
985
}
986
986
987
987
/// Compares and returns the minimum of two values.
@@ -1002,7 +1002,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
1002
1002
where
1003
1003
Self : Sized ,
1004
1004
{
1005
- min_by ( self , other, Ord :: cmp )
1005
+ if self <= other { self } else { other }
1006
1006
}
1007
1007
1008
1008
/// Restrict a value to a certain interval.
@@ -1441,10 +1441,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
1441
1441
#[ must_use]
1442
1442
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1443
1443
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 }
1448
1445
}
1449
1446
1450
1447
/// 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 {
1466
1463
#[ must_use]
1467
1464
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1468
1465
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 }
1470
1467
}
1471
1468
1472
1469
/// Compares and returns the maximum of two values.
@@ -1510,10 +1507,7 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
1510
1507
#[ must_use]
1511
1508
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1512
1509
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 }
1517
1511
}
1518
1512
1519
1513
/// 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 {
1535
1529
#[ must_use]
1536
1530
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1537
1531
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 }
1539
1533
}
1540
1534
1541
1535
/// Compares and sorts two values, returning minimum and maximum.
@@ -1620,7 +1614,7 @@ where
1620
1614
F : FnMut ( & T ) -> K ,
1621
1615
K : Ord ,
1622
1616
{
1623
- minmax_by ( v1 , v2 , | v1, v2| f ( v1 ) . cmp ( & f ( v2 ) ) )
1617
+ if f ( & v1 ) <= f ( & v2 ) { [ v1, v2] } else { [ v2 , v1 ] }
1624
1618
}
1625
1619
1626
1620
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
0 commit comments