Skip to content

Override PartialOrd methods for bool #138945

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
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
50 changes: 23 additions & 27 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1810,9 +1810,9 @@ mod impls {
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for $t {
#[inline]
fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
fn eq(&self, other: &Self) -> bool { *self == *other }
#[inline]
fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
fn ne(&self, other: &Self) -> bool { *self != *other }
}
)*)
}
Expand Down Expand Up @@ -1842,8 +1842,18 @@ mod impls {

eq_impl! { () bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }

macro_rules! chaining_methods_impl {
($t:ty) => {
#[rustfmt::skip]
macro_rules! partial_ord_methods_primitive_impl {
() => {
#[inline(always)]
fn lt(&self, other: &Self) -> bool { *self < *other }
#[inline(always)]
fn le(&self, other: &Self) -> bool { *self <= *other }
#[inline(always)]
fn gt(&self, other: &Self) -> bool { *self > *other }
#[inline(always)]
fn ge(&self, other: &Self) -> bool { *self >= *other }

// These implementations are the same for `Ord` or `PartialOrd` types
// because if either is NAN the `==` test will fail so we end up in
// the `Break` case and the comparison will correctly return `false`.
Expand Down Expand Up @@ -1876,24 +1886,16 @@ mod impls {
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (*self <= *other, *self >= *other) {
(false, false) => None,
(false, true) => Some(Greater),
(true, false) => Some(Less),
(true, true) => Some(Equal),
}
}
#[inline(always)]
fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
#[inline(always)]
fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
#[inline(always)]
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
#[inline(always)]
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }

chaining_methods_impl!($t);

partial_ord_methods_primitive_impl!();
}
)*)
}
Expand All @@ -1912,6 +1914,8 @@ mod impls {
fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
Some(self.cmp(other))
}

partial_ord_methods_primitive_impl!();
Copy link
Member

Choose a reason for hiding this comment

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

Oh, I was also adding the chaining ones here in https://github.com/rust-lang/rust/pull/138881/files#diff-792af5fefd1cfdfab121523d464c37a6793eea3b804ac5a9cb3aaa8aa1b2ade4R1916, but I like this version instead that merges the lt/le/etc in too.

I'll rebase mine atop yours; this can go in first.

}

partial_ord_impl! { f16 f32 f64 f128 }
Expand All @@ -1921,25 +1925,17 @@ mod impls {
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(crate::intrinsics::three_way_compare(*self, *other))
}
#[inline(always)]
fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
#[inline(always)]
fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
#[inline(always)]
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
#[inline(always)]
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }

chaining_methods_impl!($t);

partial_ord_methods_primitive_impl!();
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for $t {
#[inline]
fn cmp(&self, other: &$t) -> Ordering {
fn cmp(&self, other: &Self) -> Ordering {
crate::intrinsics::three_way_compare(*self, *other)
}
}
Expand Down
Loading