Skip to content

Commit 168ccea

Browse files
committed
constify comparison traits on many simple types
This consists mostly of derive changes or simple impl changes.
1 parent 7eb8f00 commit 168ccea

File tree

21 files changed

+100
-50
lines changed

21 files changed

+100
-50
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, Debug, Hash)]
39+
#[derive_const(Clone, 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(Debug)]
550+
#[derive_const(Clone, PartialEq, Eq)]
549551
pub struct LayoutError;
550552

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

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)]
595+
#[derive_const(Clone, PartialEq, Eq)]
595596
pub struct TryFromCharError(pub(crate) ());
596597

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

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, Debug)]
25+
#[derive_const(Clone, 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, Debug, Hash)]
108+
#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
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, Debug)]
261+
#[derive_const(Clone, 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, Debug)]
273+
#[derive_const(Clone, 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, Debug)]
287+
#[derive_const(Clone, 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, Copy, Hash)]
1029+
#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
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, Debug, Hash)]
155+
#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
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
@@ -147,7 +147,8 @@ where
147147
/// `true`, the onus of the safety proof belongs to the programmer.
148148
#[unstable(feature = "transmutability", issue = "99571")]
149149
#[lang = "transmute_opts"]
150-
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
150+
#[derive(Copy, Debug)]
151+
#[derive_const(Clone, PartialEq, Eq)]
151152
pub struct Assume {
152153
/// When `false`, [`TransmuteFrom`] is not implemented for transmutations
153154
/// that might violate the alignment requirements of references; e.g.:

library/core/src/num/error.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use crate::fmt;
66

77
/// The error type returned when a checked integral type conversion fails.
88
#[stable(feature = "try_from", since = "1.34.0")]
9-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9+
#[derive(Debug, Copy)]
10+
#[derive_const(Clone, PartialEq, Eq)]
1011
pub struct TryFromIntError(pub(crate) ());
1112

1213
#[stable(feature = "try_from", since = "1.34.0")]
@@ -60,7 +61,8 @@ impl const From<!> for TryFromIntError {
6061
/// println!("Failed conversion to i32: {e}");
6162
/// }
6263
/// ```
63-
#[derive(Debug, Clone, PartialEq, Eq)]
64+
#[derive(Debug)]
65+
#[derive_const(Clone, PartialEq, Eq)]
6466
#[stable(feature = "rust1", since = "1.0.0")]
6567
pub struct ParseIntError {
6668
pub(super) kind: IntErrorKind,
@@ -78,7 +80,8 @@ pub struct ParseIntError {
7880
/// # }
7981
/// ```
8082
#[stable(feature = "int_error_matching", since = "1.55.0")]
81-
#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash)]
83+
#[derive(Debug, Copy, Hash)]
84+
#[derive_const(Clone, PartialEq, Eq)]
8285
#[non_exhaustive]
8386
pub enum IntErrorKind {
8487
/// Value being parsed is empty.

library/core/src/num/niche_types.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ macro_rules! define_valid_range_type {
1414
$(#[$m:meta])*
1515
$vis:vis struct $name:ident($int:ident as $uint:ident in $low:literal..=$high:literal);
1616
)+) => {$(
17-
#[derive(Clone, Copy, Eq)]
17+
#[derive(Copy)]
18+
#[derive_const(Clone, Eq)]
1819
#[repr(transparent)]
1920
#[rustc_layout_scalar_valid_range_start($low)]
2021
#[rustc_layout_scalar_valid_range_end($high)]
@@ -67,21 +68,24 @@ macro_rules! define_valid_range_type {
6768
// by <https://github.com/rust-lang/compiler-team/issues/807>.
6869
impl StructuralPartialEq for $name {}
6970

70-
impl PartialEq for $name {
71+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
72+
impl const PartialEq for $name {
7173
#[inline]
7274
fn eq(&self, other: &Self) -> bool {
7375
self.as_inner() == other.as_inner()
7476
}
7577
}
7678

77-
impl Ord for $name {
79+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
80+
impl const Ord for $name {
7881
#[inline]
7982
fn cmp(&self, other: &Self) -> Ordering {
8083
Ord::cmp(&self.as_inner(), &other.as_inner())
8184
}
8285
}
8386

84-
impl PartialOrd for $name {
87+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
88+
impl const PartialOrd for $name {
8589
#[inline]
8690
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
8791
Some(Ord::cmp(self, other))

library/core/src/num/saturating.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use crate::ops::{
3232
/// assert_eq!(u32::MAX, (max + one).0);
3333
/// ```
3434
#[stable(feature = "saturating_int_impl", since = "1.74.0")]
35-
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)]
35+
#[derive(Copy, Hash)]
36+
#[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
3637
#[repr(transparent)]
3738
#[rustc_diagnostic_item = "Saturating"]
3839
pub struct Saturating<T>(#[stable(feature = "saturating_int_impl", since = "1.74.0")] pub T);

0 commit comments

Comments
 (0)