Skip to content

Commit 34e5239

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 0d0f4ea commit 34e5239

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.
@@ -334,7 +334,8 @@ pub macro PartialEq($item:item) {
334334
#[doc(alias = "!=")]
335335
#[stable(feature = "rust1", since = "1.0.0")]
336336
#[rustc_diagnostic_item = "Eq"]
337-
pub trait Eq: PartialEq<Self> + PointeeSized {
337+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
338+
pub const trait Eq: [const] PartialEq<Self> + PointeeSized {
338339
// this method is used solely by `impl Eq or #[derive(Eq)]` to assert that every component of a
339340
// type implements `Eq` itself. The current deriving infrastructure means doing this assertion
340341
// without using a method on this trait is nearly impossible.
@@ -957,7 +958,8 @@ impl<T: Clone> Clone for Reverse<T> {
957958
#[doc(alias = ">=")]
958959
#[stable(feature = "rust1", since = "1.0.0")]
959960
#[rustc_diagnostic_item = "Ord"]
960-
pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
961+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
962+
pub const trait Ord: [const] Eq + [const] PartialOrd<Self> + PointeeSized {
961963
/// This method returns an [`Ordering`] between `self` and `other`.
962964
///
963965
/// By convention, `self.cmp(&other)` returns the ordering matching the expression
@@ -1011,7 +1013,7 @@ pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
10111013
#[rustc_diagnostic_item = "cmp_ord_max"]
10121014
fn max(self, other: Self) -> Self
10131015
where
1014-
Self: Sized,
1016+
Self: Sized + [const] Destruct,
10151017
{
10161018
if other < self { self } else { other }
10171019
}
@@ -1050,7 +1052,7 @@ pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
10501052
#[rustc_diagnostic_item = "cmp_ord_min"]
10511053
fn min(self, other: Self) -> Self
10521054
where
1053-
Self: Sized,
1055+
Self: Sized + [const] Destruct,
10541056
{
10551057
if other < self { other } else { self }
10561058
}
@@ -1076,7 +1078,7 @@ pub trait Ord: Eq + PartialOrd<Self> + PointeeSized {
10761078
#[stable(feature = "clamp", since = "1.50.0")]
10771079
fn clamp(self, min: Self, max: Self) -> Self
10781080
where
1079-
Self: Sized,
1081+
Self: Sized + [const] Destruct,
10801082
{
10811083
assert!(min <= max);
10821084
if self < min {
@@ -1341,7 +1343,10 @@ pub macro Ord($item:item) {
13411343
)]
13421344
#[rustc_diagnostic_item = "PartialOrd"]
13431345
#[allow(multiple_supertrait_upcastable)] // FIXME(sized_hierarchy): remove this
1344-
pub trait PartialOrd<Rhs: PointeeSized = Self>: PartialEq<Rhs> + PointeeSized {
1346+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1347+
pub const trait PartialOrd<Rhs: PointeeSized = Self>:
1348+
[const] PartialEq<Rhs> + PointeeSized
1349+
{
13451350
/// This method returns an ordering between `self` and `other` values if one exists.
13461351
///
13471352
/// # Examples
@@ -1481,14 +1486,12 @@ pub trait PartialOrd<Rhs: PointeeSized = Self>: PartialEq<Rhs> + PointeeSized {
14811486
}
14821487
}
14831488

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

18321835
#[stable(feature = "rust1", since = "1.0.0")]
1833-
impl PartialEq for () {
1836+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1837+
impl const PartialEq for () {
18341838
#[inline]
18351839
fn eq(&self, _other: &()) -> bool {
18361840
true
@@ -1848,7 +1852,8 @@ mod impls {
18481852
macro_rules! eq_impl {
18491853
($($t:ty)*) => ($(
18501854
#[stable(feature = "rust1", since = "1.0.0")]
1851-
impl Eq for $t {}
1855+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1856+
impl const Eq for $t {}
18521857
)*)
18531858
}
18541859

@@ -1896,7 +1901,8 @@ mod impls {
18961901
macro_rules! partial_ord_impl {
18971902
($($t:ty)*) => ($(
18981903
#[stable(feature = "rust1", since = "1.0.0")]
1899-
impl PartialOrd for $t {
1904+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1905+
impl const PartialOrd for $t {
19001906
#[inline]
19011907
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
19021908
match (*self <= *other, *self >= *other) {
@@ -1913,15 +1919,17 @@ mod impls {
19131919
}
19141920

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

19231930
#[stable(feature = "rust1", since = "1.0.0")]
1924-
impl PartialOrd for bool {
1931+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1932+
impl const PartialOrd for bool {
19251933
#[inline]
19261934
fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
19271935
Some(self.cmp(other))
@@ -1935,7 +1943,8 @@ mod impls {
19351943
macro_rules! ord_impl {
19361944
($($t:ty)*) => ($(
19371945
#[stable(feature = "rust1", since = "1.0.0")]
1938-
impl PartialOrd for $t {
1946+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1947+
impl const PartialOrd for $t {
19391948
#[inline]
19401949
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
19411950
Some(crate::intrinsics::three_way_compare(*self, *other))
@@ -1945,7 +1954,8 @@ mod impls {
19451954
}
19461955

19471956
#[stable(feature = "rust1", since = "1.0.0")]
1948-
impl Ord for $t {
1957+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1958+
impl const Ord for $t {
19491959
#[inline]
19501960
fn cmp(&self, other: &Self) -> Ordering {
19511961
crate::intrinsics::three_way_compare(*self, *other)
@@ -1955,15 +1965,17 @@ mod impls {
19551965
}
19561966

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

19651976
#[stable(feature = "rust1", since = "1.0.0")]
1966-
impl Ord for bool {
1977+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1978+
impl const Ord for bool {
19671979
#[inline]
19681980
fn cmp(&self, other: &bool) -> Ordering {
19691981
// Casting to i8's and converting the difference to an Ordering generates
@@ -2042,9 +2054,10 @@ mod impls {
20422054
}
20432055
}
20442056
#[stable(feature = "rust1", since = "1.0.0")]
2045-
impl<A: PointeeSized, B: PointeeSized> PartialOrd<&B> for &A
2057+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2058+
impl<A: PointeeSized, B: PointeeSized> const PartialOrd<&B> for &A
20462059
where
2047-
A: PartialOrd<B>,
2060+
A: [const] PartialOrd<B>,
20482061
{
20492062
#[inline]
20502063
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
@@ -2084,17 +2097,19 @@ mod impls {
20842097
}
20852098
}
20862099
#[stable(feature = "rust1", since = "1.0.0")]
2087-
impl<A: PointeeSized> Ord for &A
2100+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2101+
impl<A: PointeeSized> const Ord for &A
20882102
where
2089-
A: Ord,
2103+
A: [const] Ord,
20902104
{
20912105
#[inline]
20922106
fn cmp(&self, other: &Self) -> Ordering {
20932107
Ord::cmp(*self, *other)
20942108
}
20952109
}
20962110
#[stable(feature = "rust1", since = "1.0.0")]
2097-
impl<A: PointeeSized> Eq for &A where A: Eq {}
2111+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2112+
impl<A: PointeeSized> const Eq for &A where A: [const] Eq {}
20982113

20992114
// &mut pointers
21002115

@@ -2114,9 +2129,10 @@ mod impls {
21142129
}
21152130
}
21162131
#[stable(feature = "rust1", since = "1.0.0")]
2117-
impl<A: PointeeSized, B: PointeeSized> PartialOrd<&mut B> for &mut A
2132+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2133+
impl<A: PointeeSized, B: PointeeSized> const PartialOrd<&mut B> for &mut A
21182134
where
2119-
A: PartialOrd<B>,
2135+
A: [const] PartialOrd<B>,
21202136
{
21212137
#[inline]
21222138
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
@@ -2156,17 +2172,19 @@ mod impls {
21562172
}
21572173
}
21582174
#[stable(feature = "rust1", since = "1.0.0")]
2159-
impl<A: PointeeSized> Ord for &mut A
2175+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2176+
impl<A: PointeeSized> const Ord for &mut A
21602177
where
2161-
A: Ord,
2178+
A: [const] Ord,
21622179
{
21632180
#[inline]
21642181
fn cmp(&self, other: &Self) -> Ordering {
21652182
Ord::cmp(*self, *other)
21662183
}
21672184
}
21682185
#[stable(feature = "rust1", since = "1.0.0")]
2169-
impl<A: PointeeSized> Eq for &mut A where A: Eq {}
2186+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2187+
impl<A: PointeeSized> const Eq for &mut A where A: [const] Eq {}
21702188

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

0 commit comments

Comments
 (0)