Skip to content

Commit 8c88ae6

Browse files
committed
constify comparison traits on many simple types
1 parent 00d6037 commit 8c88ae6

31 files changed

+144
-87
lines changed

library/core/src/alloc/layout.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const fn size_align<T>() -> (usize, usize) {
3535
/// like this are met, use specific allocators with looser
3636
/// requirements, or use the more lenient `Allocator` interface.)
3737
#[stable(feature = "alloc_layout", since = "1.28.0")]
38-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
38+
#[derive(Copy, Clone, Debug, Hash)]
39+
#[derive_const(PartialEq, Eq)]
3940
#[lang = "alloc_layout"]
4041
pub struct Layout {
4142
// size of the requested block of memory, measured in bytes.
@@ -545,7 +546,8 @@ pub type LayoutErr = LayoutError;
545546
/// do not satisfy its documented constraints.
546547
#[stable(feature = "alloc_layout_error", since = "1.50.0")]
547548
#[non_exhaustive]
548-
#[derive(Clone, PartialEq, Eq, Debug)]
549+
#[derive(Clone, Debug)]
550+
#[derive_const(PartialEq, Eq)]
549551
pub struct LayoutError;
550552

551553
#[stable(feature = "alloc_layout", since = "1.28.0")]

library/core/src/ascii/ascii_char.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ use crate::{assert_unsafe_precondition, fmt};
5454
/// [chart]: https://www.unicode.org/charts/PDF/U0000.pdf
5555
/// [NIST FIPS 1-2]: https://nvlpubs.nist.gov/nistpubs/Legacy/FIPS/fipspub1-2-1977.pdf
5656
/// [NamesList]: https://www.unicode.org/Public/15.0.0/ucd/NamesList.txt
57-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
57+
#[derive(Copy, Clone, Hash)]
58+
#[derive_const(Eq, PartialEq, Ord, PartialOrd)]
5859
#[unstable(feature = "ascii_char", issue = "110998")]
5960
#[repr(u8)]
6061
pub enum AsciiChar {

library/core/src/char/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ impl fmt::Display for CaseMappingIter {
591591

592592
/// The error type returned when a checked char conversion fails.
593593
#[stable(feature = "u8_from_char", since = "1.59.0")]
594-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
594+
#[derive(Debug, Copy, Clone)]
595+
#[derive_const(PartialEq, Eq)]
595596
pub struct TryFromCharError(pub(crate) ());
596597

597598
#[stable(feature = "u8_from_char", since = "1.59.0")]

library/core/src/cmp.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,15 @@ impl Ordering {
660660
/// v.sort_by_key(|&num| (num > 3, Reverse(num)));
661661
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
662662
/// ```
663-
#[derive(PartialEq, Eq, Debug, Copy, Default, Hash)]
663+
#[derive(Debug, Copy, Default, Hash)]
664+
#[derive_const(PartialEq, Eq)]
664665
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
665666
#[repr(transparent)]
666667
pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
667668

668669
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
669-
impl<T: PartialOrd> PartialOrd for Reverse<T> {
670+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
671+
impl<T: [const] PartialOrd> const PartialOrd for Reverse<T> {
670672
#[inline]
671673
fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
672674
other.0.partial_cmp(&self.0)
@@ -691,7 +693,8 @@ impl<T: PartialOrd> PartialOrd for Reverse<T> {
691693
}
692694

693695
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
694-
impl<T: Ord> Ord for Reverse<T> {
696+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
697+
impl<T: [const] Ord> const Ord for Reverse<T> {
695698
#[inline]
696699
fn cmp(&self, other: &Reverse<T>) -> Ordering {
697700
other.0.cmp(&self.0)

library/core/src/convert/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,17 +963,20 @@ impl const PartialEq for Infallible {
963963
}
964964

965965
#[stable(feature = "convert_infallible", since = "1.34.0")]
966-
impl Eq for Infallible {}
966+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
967+
impl const Eq for Infallible {}
967968

968969
#[stable(feature = "convert_infallible", since = "1.34.0")]
969-
impl PartialOrd for Infallible {
970+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
971+
impl const PartialOrd for Infallible {
970972
fn partial_cmp(&self, _other: &Self) -> Option<crate::cmp::Ordering> {
971973
match *self {}
972974
}
973975
}
974976

975977
#[stable(feature = "convert_infallible", since = "1.34.0")]
976-
impl Ord for Infallible {
978+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
979+
impl const Ord for Infallible {
977980
fn cmp(&self, _other: &Self) -> crate::cmp::Ordering {
978981
match *self {}
979982
}

library/core/src/fmt/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ mod rt;
2121
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
2222
#[rustc_diagnostic_item = "Alignment"]
2323
/// Possible alignments returned by `Formatter::align`
24-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
24+
#[derive(Copy, Clone, Debug)]
25+
#[derive_const(PartialEq, Eq)]
2526
pub enum Alignment {
2627
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
2728
/// Indication that contents should be left-aligned.
@@ -103,7 +104,8 @@ pub type Result = result::Result<(), Error>;
103104
/// }
104105
/// ```
105106
#[stable(feature = "rust1", since = "1.0.0")]
106-
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
107+
#[derive(Copy, Clone, Debug, Default, Hash)]
108+
#[derive_const(Eq, Ord, PartialEq, PartialOrd)]
107109
pub struct Error;
108110

109111
/// A trait for writing or formatting into Unicode-accepting buffers or streams.
@@ -255,7 +257,8 @@ impl<W: Write + ?Sized> Write for &mut W {
255257
}
256258

257259
/// The signedness of a [`Formatter`] (or of a [`FormattingOptions`]).
258-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
260+
#[derive(Copy, Clone, Debug)]
261+
#[derive_const(PartialEq, Eq)]
259262
#[unstable(feature = "formatting_options", issue = "118117")]
260263
pub enum Sign {
261264
/// Represents the `+` flag.
@@ -266,7 +269,8 @@ pub enum Sign {
266269

267270
/// Specifies whether the [`Debug`] trait should use lower-/upper-case
268271
/// hexadecimal or normal integers.
269-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
272+
#[derive(Copy, Clone, Debug)]
273+
#[derive_const(PartialEq, Eq)]
270274
#[unstable(feature = "formatting_options", issue = "118117")]
271275
pub enum DebugAsHex {
272276
/// Use lower-case hexadecimal integers for the `Debug` trait (like [the `x?` type](../../std/fmt/index.html#formatting-traits)).
@@ -279,7 +283,8 @@ pub enum DebugAsHex {
279283
///
280284
/// `FormattingOptions` is a [`Formatter`] without an attached [`Write`] trait.
281285
/// It is mainly used to construct `Formatter` instances.
282-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
286+
#[derive(Copy, Clone, Debug)]
287+
#[derive_const(PartialEq, Eq)]
283288
#[unstable(feature = "formatting_options", issue = "118117")]
284289
pub struct FormattingOptions {
285290
/// Flags, with the following bit fields:

library/core/src/intrinsics/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, O
7171
/// A type for atomic ordering parameters for intrinsics. This is a separate type from
7272
/// `atomic::Ordering` so that we can make it `ConstParamTy` and fix the values used here without a
7373
/// risk of leaking that to stable code.
74-
#[derive(Debug, ConstParamTy, PartialEq, Eq)]
74+
#[derive(Debug, ConstParamTy)]
75+
#[derive_const(PartialEq, Eq)]
7576
pub enum AtomicOrdering {
7677
// These values must match the compiler's `AtomicOrdering` defined in
7778
// `rustc_middle/src/ty/consts/int.rs`!

library/core/src/marker.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -828,24 +828,28 @@ impl<T: PointeeSized> Hash for PhantomData<T> {
828828
}
829829

830830
#[stable(feature = "rust1", since = "1.0.0")]
831-
impl<T: PointeeSized> cmp::PartialEq for PhantomData<T> {
831+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
832+
impl<T: PointeeSized> const cmp::PartialEq for PhantomData<T> {
832833
fn eq(&self, _other: &PhantomData<T>) -> bool {
833834
true
834835
}
835836
}
836837

837838
#[stable(feature = "rust1", since = "1.0.0")]
838-
impl<T: PointeeSized> cmp::Eq for PhantomData<T> {}
839+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
840+
impl<T: PointeeSized> const cmp::Eq for PhantomData<T> {}
839841

840842
#[stable(feature = "rust1", since = "1.0.0")]
841-
impl<T: PointeeSized> cmp::PartialOrd for PhantomData<T> {
843+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
844+
impl<T: PointeeSized> const cmp::PartialOrd for PhantomData<T> {
842845
fn partial_cmp(&self, _other: &PhantomData<T>) -> Option<cmp::Ordering> {
843846
Option::Some(cmp::Ordering::Equal)
844847
}
845848
}
846849

847850
#[stable(feature = "rust1", since = "1.0.0")]
848-
impl<T: PointeeSized> cmp::Ord for PhantomData<T> {
851+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
852+
impl<T: PointeeSized> const cmp::Ord for PhantomData<T> {
849853
fn cmp(&self, _other: &PhantomData<T>) -> cmp::Ordering {
850854
cmp::Ordering::Equal
851855
}
@@ -1021,7 +1025,8 @@ pub auto trait Unpin {}
10211025
// marker in your struct acts as if you wrapped the entire struct in an `UnsafePinned`. This type
10221026
// will likely eventually be deprecated, and all new code should be using `UnsafePinned` instead.
10231027
#[stable(feature = "pin", since = "1.33.0")]
1024-
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
1028+
#[derive(Debug, Default, Copy, Clone, Hash)]
1029+
#[derive_const(Eq, PartialEq, Ord, PartialOrd)]
10251030
pub struct PhantomPinned;
10261031

10271032
#[stable(feature = "pin", since = "1.33.0")]

library/core/src/mem/manually_drop.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ use crate::ptr;
151151
/// [`MaybeUninit`]: crate::mem::MaybeUninit
152152
#[stable(feature = "manually_drop", since = "1.20.0")]
153153
#[lang = "manually_drop"]
154-
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
154+
#[derive(Copy, Clone, Debug, Default, Hash)]
155+
#[derive_const(PartialEq, Eq, PartialOrd, Ord)]
155156
#[repr(transparent)]
156157
#[rustc_pub_transparent]
157158
pub struct ManuallyDrop<T: ?Sized> {

library/core/src/mem/transmutability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ where
146146
/// `true`, the onus of the safety proof belongs to the programmer.
147147
#[unstable(feature = "transmutability", issue = "99571")]
148148
#[lang = "transmute_opts"]
149-
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
149+
#[derive(Clone, Copy, Debug)]
150+
#[derive_const(PartialEq, Eq)]
150151
pub struct Assume {
151152
/// When `false`, [`TransmuteFrom`] is not implemented for transmutations
152153
/// that might violate the alignment requirements of references; e.g.:

0 commit comments

Comments
 (0)