Skip to content

Commit 1308d51

Browse files
committed
constify the Eq, PartialOrd and Ord traits
Also constify the impls of basic types. One potentially controversial part of this change is making Eq, a marker trait, const. I chose to do this ease user adoption. Otherwise, code which already has an Eq bound and uses it to proxy a PartialEq bound would need to add a separate bound. This would cause a litering of bounds in downstream types.
1 parent a1208bf commit 1308d51

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

library/core/src/cmp.rs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod bytewise;
2929
pub(crate) use bytewise::BytewiseEq;
3030

3131
use self::Ordering::*;
32-
use crate::marker::PointeeSized;
32+
use crate::marker::{Destruct, PointeeSized};
3333
use crate::ops::ControlFlow;
3434

3535
/// Trait for comparisons using the equality operator.
@@ -335,7 +335,8 @@ pub macro PartialEq($item:item) {
335335
#[doc(alias = "!=")]
336336
#[stable(feature = "rust1", since = "1.0.0")]
337337
#[rustc_diagnostic_item = "Eq"]
338-
pub trait Eq: PartialEq<Self> + PointeeSized {
338+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
339+
pub const trait Eq: [const] PartialEq<Self> + PointeeSized {
339340
// this method is used solely by `impl Eq or #[derive(Eq)]` to assert that every component of a
340341
// type implements `Eq` itself. The current deriving infrastructure means doing this assertion
341342
// without using a method on this trait is nearly impossible.
@@ -958,7 +959,8 @@ impl<T: Clone> Clone for Reverse<T> {
958959
#[doc(alias = ">=")]
959960
#[stable(feature = "rust1", since = "1.0.0")]
960961
#[rustc_diagnostic_item = "Ord"]
961-
pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
962+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
963+
pub const trait Ord: [const] Eq + [const] PartialOrd<Self> + PointeeSized {
962964
/// This method returns an [`Ordering`] between `self` and `other`.
963965
///
964966
/// By convention, `self.cmp(&other)` returns the ordering matching the expression
@@ -1012,7 +1014,7 @@ pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
10121014
#[rustc_diagnostic_item = "cmp_ord_max"]
10131015
fn max(self, other: Self) -> Self
10141016
where
1015-
Self: Sized,
1017+
Self: Sized + [const] Destruct,
10161018
{
10171019
if other < self { self } else { other }
10181020
}
@@ -1051,7 +1053,7 @@ pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
10511053
#[rustc_diagnostic_item = "cmp_ord_min"]
10521054
fn min(self, other: Self) -> Self
10531055
where
1054-
Self: Sized,
1056+
Self: Sized + [const] Destruct,
10551057
{
10561058
if other < self { other } else { self }
10571059
}
@@ -1077,7 +1079,7 @@ pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
10771079
#[stable(feature = "clamp", since = "1.50.0")]
10781080
fn clamp(self, min: Self, max: Self) -> Self
10791081
where
1080-
Self: Sized,
1082+
Self: Sized + [const] Destruct,
10811083
{
10821084
assert!(min <= max);
10831085
if self < min {
@@ -1342,7 +1344,10 @@ pub macro Ord($item:item) {
13421344
)]
13431345
#[rustc_diagnostic_item = "PartialOrd"]
13441346
#[allow(multiple_supertrait_upcastable)] // FIXME(sized_hierarchy): remove this
1345-
pub trait PartialOrd<Rhs: PointeeSized = Self>: PartialEq<Rhs> + PointeeSized {
1347+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1348+
pub const trait PartialOrd<Rhs: PointeeSized = Self>:
1349+
[const] PartialEq<Rhs> + PointeeSized
1350+
{
13461351
/// This method returns an ordering between `self` and `other` values if one exists.
13471352
///
13481353
/// # Examples
@@ -1482,14 +1487,12 @@ pub trait PartialOrd<Rhs: PointeeSized = Self>: PartialEq<Rhs> + PointeeSized {
14821487
}
14831488
}
14841489

1485-
fn default_chaining_impl<T, U>(
1486-
lhs: &T,
1487-
rhs: &U,
1488-
p: impl FnOnce(Ordering) -> bool,
1489-
) -> ControlFlow<bool>
1490+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1491+
const fn default_chaining_impl<T, U, F>(lhs: &T, rhs: &U, p: F) -> ControlFlow<bool>
14901492
where
1491-
T: PartialOrd<U> + PointeeSized,
1493+
T: [const] PartialOrd<U> + PointeeSized,
14921494
U: PointeeSized,
1495+
F: [const] Destruct + [const] FnOnce(Ordering) -> bool,
14931496
{
14941497
// It's important that this only call `partial_cmp` once, not call `eq` then
14951498
// one of the relational operators. We don't want to `bcmp`-then-`memcp` a
@@ -1831,7 +1834,8 @@ mod impls {
18311834
}
18321835

18331836
#[stable(feature = "rust1", since = "1.0.0")]
1834-
impl PartialEq for () {
1837+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1838+
impl const PartialEq for () {
18351839
#[inline]
18361840
fn eq(&self, _other: &()) -> bool {
18371841
true
@@ -1849,7 +1853,8 @@ mod impls {
18491853
macro_rules! eq_impl {
18501854
($($t:ty)*) => ($(
18511855
#[stable(feature = "rust1", since = "1.0.0")]
1852-
impl Eq for $t {}
1856+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1857+
impl const Eq for $t {}
18531858
)*)
18541859
}
18551860

@@ -1897,7 +1902,8 @@ mod impls {
18971902
macro_rules! partial_ord_impl {
18981903
($($t:ty)*) => ($(
18991904
#[stable(feature = "rust1", since = "1.0.0")]
1900-
impl PartialOrd for $t {
1905+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1906+
impl const PartialOrd for $t {
19011907
#[inline]
19021908
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
19031909
match (*self <= *other, *self >= *other) {
@@ -1914,15 +1920,17 @@ mod impls {
19141920
}
19151921

19161922
#[stable(feature = "rust1", since = "1.0.0")]
1917-
impl PartialOrd for () {
1923+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1924+
impl const PartialOrd for () {
19181925
#[inline]
19191926
fn partial_cmp(&self, _: &()) -> Option<Ordering> {
19201927
Some(Equal)
19211928
}
19221929
}
19231930

19241931
#[stable(feature = "rust1", since = "1.0.0")]
1925-
impl PartialOrd for bool {
1932+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1933+
impl const PartialOrd for bool {
19261934
#[inline]
19271935
fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
19281936
Some(self.cmp(other))
@@ -1936,7 +1944,8 @@ mod impls {
19361944
macro_rules! ord_impl {
19371945
($($t:ty)*) => ($(
19381946
#[stable(feature = "rust1", since = "1.0.0")]
1939-
impl PartialOrd for $t {
1947+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1948+
impl const PartialOrd for $t {
19401949
#[inline]
19411950
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
19421951
Some(crate::intrinsics::three_way_compare(*self, *other))
@@ -1946,7 +1955,8 @@ mod impls {
19461955
}
19471956

19481957
#[stable(feature = "rust1", since = "1.0.0")]
1949-
impl Ord for $t {
1958+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1959+
impl const Ord for $t {
19501960
#[inline]
19511961
fn cmp(&self, other: &Self) -> Ordering {
19521962
crate::intrinsics::three_way_compare(*self, *other)
@@ -1956,15 +1966,17 @@ mod impls {
19561966
}
19571967

19581968
#[stable(feature = "rust1", since = "1.0.0")]
1959-
impl Ord for () {
1969+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1970+
impl const Ord for () {
19601971
#[inline]
19611972
fn cmp(&self, _other: &()) -> Ordering {
19621973
Equal
19631974
}
19641975
}
19651976

19661977
#[stable(feature = "rust1", since = "1.0.0")]
1967-
impl Ord for bool {
1978+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1979+
impl const Ord for bool {
19681980
#[inline]
19691981
fn cmp(&self, other: &bool) -> Ordering {
19701982
// Casting to i8's and converting the difference to an Ordering generates
@@ -2043,9 +2055,10 @@ mod impls {
20432055
}
20442056
}
20452057
#[stable(feature = "rust1", since = "1.0.0")]
2046-
impl<A: PointeeSized, B: PointeeSized> PartialOrd<&B> for &A
2058+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2059+
impl<A: PointeeSized, B: PointeeSized> const PartialOrd<&B> for &A
20472060
where
2048-
A: PartialOrd<B>,
2061+
A: [const] PartialOrd<B>,
20492062
{
20502063
#[inline]
20512064
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
@@ -2085,17 +2098,19 @@ mod impls {
20852098
}
20862099
}
20872100
#[stable(feature = "rust1", since = "1.0.0")]
2088-
impl<A: PointeeSized> Ord for &A
2101+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2102+
impl<A: PointeeSized> const Ord for &A
20892103
where
2090-
A: Ord,
2104+
A: [const] Ord,
20912105
{
20922106
#[inline]
20932107
fn cmp(&self, other: &Self) -> Ordering {
20942108
Ord::cmp(*self, *other)
20952109
}
20962110
}
20972111
#[stable(feature = "rust1", since = "1.0.0")]
2098-
impl<A: PointeeSized> Eq for &A where A: Eq {}
2112+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2113+
impl<A: PointeeSized> const Eq for &A where A: [const] Eq {}
20992114

21002115
// &mut pointers
21012116

@@ -2115,9 +2130,10 @@ mod impls {
21152130
}
21162131
}
21172132
#[stable(feature = "rust1", since = "1.0.0")]
2118-
impl<A: PointeeSized, B: PointeeSized> PartialOrd<&mut B> for &mut A
2133+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2134+
impl<A: PointeeSized, B: PointeeSized> const PartialOrd<&mut B> for &mut A
21192135
where
2120-
A: PartialOrd<B>,
2136+
A: [const] PartialOrd<B>,
21212137
{
21222138
#[inline]
21232139
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
@@ -2157,17 +2173,19 @@ mod impls {
21572173
}
21582174
}
21592175
#[stable(feature = "rust1", since = "1.0.0")]
2160-
impl<A: PointeeSized> Ord for &mut A
2176+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2177+
impl<A: PointeeSized> const Ord for &mut A
21612178
where
2162-
A: Ord,
2179+
A: [const] Ord,
21632180
{
21642181
#[inline]
21652182
fn cmp(&self, other: &Self) -> Ordering {
21662183
Ord::cmp(*self, *other)
21672184
}
21682185
}
21692186
#[stable(feature = "rust1", since = "1.0.0")]
2170-
impl<A: PointeeSized> Eq for &mut A where A: Eq {}
2187+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2188+
impl<A: PointeeSized> const Eq for &mut A where A: [const] Eq {}
21712189

21722190
#[stable(feature = "rust1", since = "1.0.0")]
21732191
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]

0 commit comments

Comments
 (0)