diff options
| author | Jacob Pratt <jacob@jhpratt.dev> | 2025-03-25 20:34:50 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-25 20:34:50 -0400 |
| commit | deb987b69dae0857a9e108f86bf24c60150a6e9f (patch) | |
| tree | db3a6110be08cb2f1b04be78da07ecbb10e67d15 | |
| parent | 04cbe2816df9ce5c9db762f9af7ef6f0a1a50a38 (diff) | |
| parent | 154cb083e705feca7509143352887dd293db61b2 (diff) | |
| download | rust-deb987b69dae0857a9e108f86bf24c60150a6e9f.tar.gz rust-deb987b69dae0857a9e108f86bf24c60150a6e9f.zip | |
Rollup merge of #138945 - DaniPopes:override-partialord-bool, r=scottmcm
Override PartialOrd methods for bool I noticed that `PartialOrd` implementation for `bool` does not override the individual operator methods, unlike the other primitive types like `char` and integers. This commit extracts these `PartialOrd` overrides shared by the other primitive types into a macro and calls it on `bool` too. CC `@scottmcm` for our recent adventures in `PartialOrd` land
| -rw-r--r-- | library/core/src/cmp.rs | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 0b0dbf723b6..0dc2cc72e06 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -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 } } )*) } @@ -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`. @@ -1876,7 +1886,7 @@ 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), @@ -1884,16 +1894,8 @@ mod impls { (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!(); } )*) } @@ -1912,6 +1914,8 @@ mod impls { fn partial_cmp(&self, other: &bool) -> Option<Ordering> { Some(self.cmp(other)) } + + partial_ord_methods_primitive_impl!(); } partial_ord_impl! { f16 f32 f64 f128 } @@ -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) } } |
