@@ -999,7 +999,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
999
999
where
1000
1000
Self : Sized ,
1001
1001
{
1002
- max_by ( self , other, Ord :: cmp )
1002
+ if other < self { self } else { other }
1003
1003
}
1004
1004
1005
1005
/// Compares and returns the minimum of two values.
@@ -1038,7 +1038,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
1038
1038
where
1039
1039
Self : Sized ,
1040
1040
{
1041
- min_by ( self , other, Ord :: cmp )
1041
+ if other < self { other } else { self }
1042
1042
}
1043
1043
1044
1044
/// Restrict a value to a certain interval.
@@ -1500,10 +1500,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
1500
1500
#[ must_use]
1501
1501
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1502
1502
pub fn min_by < T , F : FnOnce ( & T , & T ) -> Ordering > ( v1 : T , v2 : T , compare : F ) -> T {
1503
- match compare ( & v1, & v2) {
1504
- Ordering :: Less | Ordering :: Equal => v1,
1505
- Ordering :: Greater => v2,
1506
- }
1503
+ if compare ( & v2, & v1) . is_lt ( ) { v2 } else { v1 }
1507
1504
}
1508
1505
1509
1506
/// Returns the element that gives the minimum value from the specified function.
@@ -1528,7 +1525,7 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1528
1525
#[ must_use]
1529
1526
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1530
1527
pub fn min_by_key < T , F : FnMut ( & T ) -> K , K : Ord > ( v1 : T , v2 : T , mut f : F ) -> T {
1531
- min_by ( v1 , v2 , |v1 , v2| f ( v1) . cmp ( & f ( v2 ) ) )
1528
+ if f ( & v2 ) < f ( & v1) { v2 } else { v1 }
1532
1529
}
1533
1530
1534
1531
/// Compares and returns the maximum of two values.
@@ -1595,10 +1592,7 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
1595
1592
#[ must_use]
1596
1593
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1597
1594
pub fn max_by < T , F : FnOnce ( & T , & T ) -> Ordering > ( v1 : T , v2 : T , compare : F ) -> T {
1598
- match compare ( & v1, & v2) {
1599
- Ordering :: Less | Ordering :: Equal => v2,
1600
- Ordering :: Greater => v1,
1601
- }
1595
+ if compare ( & v2, & v1) . is_lt ( ) { v1 } else { v2 }
1602
1596
}
1603
1597
1604
1598
/// Returns the element that gives the maximum value from the specified function.
@@ -1623,7 +1617,7 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1623
1617
#[ must_use]
1624
1618
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1625
1619
pub fn max_by_key < T , F : FnMut ( & T ) -> K , K : Ord > ( v1 : T , v2 : T , mut f : F ) -> T {
1626
- max_by ( v1 , v2 , |v1 , v2| f ( v1) . cmp ( & f ( v2 ) ) )
1620
+ if f ( & v2 ) < f ( & v1) { v1 } else { v2 }
1627
1621
}
1628
1622
1629
1623
/// Compares and sorts two values, returning minimum and maximum.
@@ -1670,7 +1664,7 @@ pub fn minmax<T>(v1: T, v2: T) -> [T; 2]
1670
1664
where
1671
1665
T : Ord ,
1672
1666
{
1673
- if v1 <= v2 { [ v1 , v2 ] } else { [ v2 , v1 ] }
1667
+ if v2 < v1 { [ v2 , v1 ] } else { [ v1 , v2 ] }
1674
1668
}
1675
1669
1676
1670
/// Returns minimum and maximum values with respect to the specified comparison function.
@@ -1701,7 +1695,7 @@ pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
1701
1695
where
1702
1696
F : FnOnce ( & T , & T ) -> Ordering ,
1703
1697
{
1704
- if compare ( & v1 , & v2 ) . is_le ( ) { [ v1 , v2 ] } else { [ v2 , v1 ] }
1698
+ if compare ( & v2 , & v1 ) . is_lt ( ) { [ v2 , v1 ] } else { [ v1 , v2 ] }
1705
1699
}
1706
1700
1707
1701
/// Returns minimum and maximum values with respect to the specified key function.
@@ -1730,7 +1724,7 @@ where
1730
1724
F : FnMut ( & T ) -> K ,
1731
1725
K : Ord ,
1732
1726
{
1733
- minmax_by ( v1 , v2, |v1 , v2| f ( v1 ) . cmp ( & f ( v2 ) ) )
1727
+ if f ( & v2 ) < f ( & v1 ) { [ v2, v1 ] } else { [ v1 , v2 ] }
1734
1728
}
1735
1729
1736
1730
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
0 commit comments