Skip to content

Experiment: add unstable RHS type to Ord, impl PartialOrd<[U]> for [T] #129870

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 22 additions & 18 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

#![stable(feature = "rust1", since = "1.0.0")]

mod bytewise;
pub(crate) use bytewise::BytewiseEq;
mod spec;
pub(crate) use spec::{AlwaysApplicableOrd, BytewiseEq, UnsignedBytewiseOrd};

use self::Ordering::*;

Expand Down Expand Up @@ -817,7 +817,9 @@ impl<T: Clone> Clone for Reverse<T> {
#[doc(alias = ">=")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "Ord"]
pub trait Ord: Eq + PartialOrd<Self> {
pub trait Ord<#[unstable(feature = "generic_ord", issue = "none")] Rhs: ?Sized = Self>:
Eq + PartialOrd<Rhs>
{
/// This method returns an [`Ordering`] between `self` and `other`.
///
/// By convention, `self.cmp(&other)` returns the ordering matching the expression
Expand All @@ -835,7 +837,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "ord_cmp_method"]
fn cmp(&self, other: &Self) -> Ordering;
fn cmp(&self, other: &Rhs) -> Ordering;

/// Compares and returns the maximum of two values.
///
Expand All @@ -851,11 +853,12 @@ pub trait Ord: Eq + PartialOrd<Self> {
#[inline]
#[must_use]
#[rustc_diagnostic_item = "cmp_ord_max"]
fn max(self, other: Self) -> Self
fn max(self, other: Rhs) -> Self
where
Self: Sized,
Rhs: Sized + Into<Self>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't entirely sure what the best way to handle these methods was, but since this is guaranteed by impl From<T> for T in the stable case, I figured it was okay.

{
max_by(self, other, Ord::cmp)
if self > other { self } else { other.into() }
}

/// Compares and returns the minimum of two values.
Expand All @@ -872,11 +875,12 @@ pub trait Ord: Eq + PartialOrd<Self> {
#[inline]
#[must_use]
#[rustc_diagnostic_item = "cmp_ord_min"]
fn min(self, other: Self) -> Self
fn min(self, other: Rhs) -> Self
where
Self: Sized,
Rhs: Sized + Into<Self>,
{
min_by(self, other, Ord::cmp)
if self <= other { self } else { other.into() }
}

/// Restrict a value to a certain interval.
Expand All @@ -898,16 +902,16 @@ pub trait Ord: Eq + PartialOrd<Self> {
#[must_use]
#[inline]
#[stable(feature = "clamp", since = "1.50.0")]
fn clamp(self, min: Self, max: Self) -> Self
fn clamp(self, min: Rhs, max: Rhs) -> Self
where
Self: Sized,
Self: PartialOrd,
Rhs: Sized + PartialOrd + Into<Self>,
{
assert!(min <= max);
if self < min {
min
min.into()
} else if self > max {
max
max.into()
} else {
self
}
Expand Down Expand Up @@ -1692,12 +1696,12 @@ mod impls {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Ord for &A
impl<A: ?Sized, B: ?Sized> Ord<&B> for &A
where
A: Ord,
A: Ord<B>,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
fn cmp(&self, other: &&B) -> Ordering {
Ord::cmp(*self, *other)
}
}
Expand Down Expand Up @@ -1747,12 +1751,12 @@ mod impls {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: ?Sized> Ord for &mut A
impl<A: ?Sized, B: ?Sized> Ord<&mut B> for &mut A
where
A: Ord,
A: Ord<B>,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
fn cmp(&self, other: &&mut B) -> Ordering {
Ord::cmp(*self, *other)
}
}
Expand Down
38 changes: 38 additions & 0 deletions library/core/src/cmp/bytewise.rs → library/core/src/cmp/spec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ascii;
use crate::num::NonZero;

/// Types where `==` & `!=` are equivalent to comparing their underlying bytes.
Expand Down Expand Up @@ -80,3 +81,40 @@ macro_rules! is_bytewise_comparable_array_length {
// error: specializing impl repeats parameter `N`
// so just do it for a couple of plausibly-common ones.
is_bytewise_comparable_array_length!(0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64);

/// Marks that a type should be treated as an unsigned byte for comparisons.
///
/// # Safety
/// * The type must be readable as an `u8`, meaning it has to have the same
/// layout as `u8` and always be initialized.
/// * For every `x` and `y` of this type, `Ord(x, y)` must return the same
/// value as `Ord::cmp(transmute::<_, u8>(x), transmute::<_, u8>(y))`.
#[rustc_specialization_trait]
pub(crate) unsafe trait UnsignedBytewiseOrd<Rhs = Self>: Ord<Rhs> + Sized {}

unsafe impl UnsignedBytewiseOrd for bool {}
unsafe impl UnsignedBytewiseOrd for u8 {}
unsafe impl UnsignedBytewiseOrd for NonZero<u8> {}
unsafe impl UnsignedBytewiseOrd for Option<NonZero<u8>> {}
unsafe impl UnsignedBytewiseOrd for ascii::Char {}

/// Marks that a type's [`Ord`] impl can always be used instead of its [`PartialOrd`] impl,
/// so that we can specialize slice `Ord`.
#[rustc_specialization_trait]
pub(crate) trait AlwaysApplicableOrd<Rhs: ?Sized = Self>: Ord<Rhs> {}

macro_rules! always_applicable_ord {
($($t:ty,)*) => {
$(impl AlwaysApplicableOrd for $t {})*
}
}

always_applicable_ord! {
u8, u16, u32, u64, u128, usize,
i8, i16, i32, i64, i128, isize,
bool, char,
}

// to ensure soundness, these must have differing types
impl<T: ?Sized, U: ?Sized> AlwaysApplicableOrd<&U> for &T where T: AlwaysApplicableOrd<U> {}
impl<T: ?Sized, U: ?Sized> AlwaysApplicableOrd<&mut U> for &mut T where T: AlwaysApplicableOrd<U> {}
Loading
Loading