Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert 102245 #109817

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 14 additions & 30 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,10 @@ pub trait Ord: Eq + PartialOrd<Self> {
Self: Sized,
Self: ~const Destruct,
{
max_by(self, other, Ord::cmp)
match self.cmp(&other) {
Ordering::Less | Ordering::Equal => other,
Ordering::Greater => self,
}
}

/// Compares and returns the minimum of two values.
Expand All @@ -821,7 +824,10 @@ pub trait Ord: Eq + PartialOrd<Self> {
Self: Sized,
Self: ~const Destruct,
{
min_by(self, other, Ord::cmp)
match self.cmp(&other) {
Ordering::Less | Ordering::Equal => self,
Ordering::Greater => other,
}
}

/// Restrict a value to a certain interval.
Expand Down Expand Up @@ -1184,12 +1190,7 @@ pub const fn min<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
#[inline]
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
pub const fn min_by<T, F: ~const FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T
where
T: ~const Destruct,
F: ~const Destruct,
{
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
match compare(&v1, &v2) {
Ordering::Less | Ordering::Equal => v1,
Ordering::Greater => v2,
Expand All @@ -1211,14 +1212,8 @@ where
#[inline]
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
pub const fn min_by_key<T, F: ~const FnMut(&T) -> K, K: ~const Ord>(v1: T, v2: T, mut f: F) -> T
where
T: ~const Destruct,
F: ~const Destruct,
K: ~const Destruct,
{
min_by(v1, v2, const |v1, v2| f(v1).cmp(&f(v2)))
pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
}

/// Compares and returns the maximum of two values.
Expand Down Expand Up @@ -1259,12 +1254,7 @@ pub const fn max<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
#[inline]
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
pub const fn max_by<T, F: ~const FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T
where
T: ~const Destruct,
F: ~const Destruct,
{
pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
match compare(&v1, &v2) {
Ordering::Less | Ordering::Equal => v2,
Ordering::Greater => v1,
Expand All @@ -1286,14 +1276,8 @@ where
#[inline]
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
pub const fn max_by_key<T, F: ~const FnMut(&T) -> K, K: ~const Ord>(v1: T, v2: T, mut f: F) -> T
where
T: ~const Destruct,
F: ~const Destruct,
K: ~const Destruct,
{
max_by(v1, v2, const |v1, v2| f(v1).cmp(&f(v2)))
pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
}

// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass

type I32Cmp = fn(&i32, &i32) -> core::cmp::Ordering;
pub const fn min_by_i32() -> fn(i32, i32, I32Cmp) -> i32 {
core::cmp::min_by
}

fn main() {}