Skip to content

Commit 32e5ed4

Browse files
committed
Constify Eq, Ord, PartialOrd
1 parent 5b9564a commit 32e5ed4

38 files changed

+300
-185
lines changed

library/core/src/alloc/layout.rs

Lines changed: 2 additions & 1 deletion
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.

library/core/src/alloc/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ use crate::ptr::{self, NonNull};
2828
/// something wrong when combining the given input arguments with this
2929
/// allocator.
3030
#[unstable(feature = "allocator_api", issue = "32838")]
31-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
31+
#[derive(Copy, Debug)]
32+
#[derive_const(Clone, PartialEq, Eq)]
3233
pub struct AllocError;
3334

3435
#[unstable(

library/core/src/any.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,8 @@ impl dyn Any + Send + Sync {
705705
/// std::mem::forget(fake_one_ring);
706706
/// }
707707
/// ```
708-
#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
708+
#[derive(Copy, PartialOrd, Ord)]
709+
#[derive_const(Clone, Eq)]
709710
#[stable(feature = "rust1", since = "1.0.0")]
710711
#[lang = "type_id"]
711712
pub struct TypeId {

library/core/src/array/equality.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::cmp::BytewiseEq;
22

33
#[stable(feature = "rust1", since = "1.0.0")]
4-
impl<T, U, const N: usize> PartialEq<[U; N]> for [T; N]
4+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
5+
impl<T, U, const N: usize> const PartialEq<[U; N]> for [T; N]
56
where
6-
T: PartialEq<U>,
7+
T: ~const PartialEq<U>,
78
{
89
#[inline]
910
fn eq(&self, other: &[U; N]) -> bool {
@@ -16,9 +17,10 @@ where
1617
}
1718

1819
#[stable(feature = "rust1", since = "1.0.0")]
19-
impl<T, U, const N: usize> PartialEq<[U]> for [T; N]
20+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
21+
impl<T, U, const N: usize> const PartialEq<[U]> for [T; N]
2022
where
21-
T: PartialEq<U>,
23+
T: ~const PartialEq<U>,
2224
{
2325
#[inline]
2426
fn eq(&self, other: &[U]) -> bool {
@@ -39,9 +41,10 @@ where
3941
}
4042

4143
#[stable(feature = "rust1", since = "1.0.0")]
42-
impl<T, U, const N: usize> PartialEq<[U; N]> for [T]
44+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
45+
impl<T, U, const N: usize> const PartialEq<[U; N]> for [T]
4346
where
44-
T: PartialEq<U>,
47+
T: ~const PartialEq<U>,
4548
{
4649
#[inline]
4750
fn eq(&self, other: &[U; N]) -> bool {
@@ -62,9 +65,10 @@ where
6265
}
6366

6467
#[stable(feature = "rust1", since = "1.0.0")]
65-
impl<T, U, const N: usize> PartialEq<&[U]> for [T; N]
68+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
69+
impl<T, U, const N: usize> const PartialEq<&[U]> for [T; N]
6670
where
67-
T: PartialEq<U>,
71+
T: ~const PartialEq<U>,
6872
{
6973
#[inline]
7074
fn eq(&self, other: &&[U]) -> bool {
@@ -77,9 +81,10 @@ where
7781
}
7882

7983
#[stable(feature = "rust1", since = "1.0.0")]
80-
impl<T, U, const N: usize> PartialEq<[U; N]> for &[T]
84+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
85+
impl<T, U, const N: usize> const PartialEq<[U; N]> for &[T]
8186
where
82-
T: PartialEq<U>,
87+
T: ~const PartialEq<U>,
8388
{
8489
#[inline]
8590
fn eq(&self, other: &[U; N]) -> bool {
@@ -92,9 +97,10 @@ where
9297
}
9398

9499
#[stable(feature = "rust1", since = "1.0.0")]
95-
impl<T, U, const N: usize> PartialEq<&mut [U]> for [T; N]
100+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
101+
impl<T, U, const N: usize> const PartialEq<&mut [U]> for [T; N]
96102
where
97-
T: PartialEq<U>,
103+
T: ~const PartialEq<U>,
98104
{
99105
#[inline]
100106
fn eq(&self, other: &&mut [U]) -> bool {
@@ -107,9 +113,10 @@ where
107113
}
108114

109115
#[stable(feature = "rust1", since = "1.0.0")]
110-
impl<T, U, const N: usize> PartialEq<[U; N]> for &mut [T]
116+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
117+
impl<T, U, const N: usize> const PartialEq<[U; N]> for &mut [T]
111118
where
112-
T: PartialEq<U>,
119+
T: ~const PartialEq<U>,
113120
{
114121
#[inline]
115122
fn eq(&self, other: &[U; N]) -> bool {
@@ -126,14 +133,18 @@ where
126133
// __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
127134

128135
#[stable(feature = "rust1", since = "1.0.0")]
129-
impl<T: Eq, const N: usize> Eq for [T; N] {}
136+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
137+
impl<T: ~const Eq, const N: usize> const Eq for [T; N] {}
130138

139+
#[const_trait]
140+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
131141
trait SpecArrayEq<Other, const N: usize>: Sized {
132142
fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool;
133143
fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool;
134144
}
135145

136-
impl<T: PartialEq<Other>, Other, const N: usize> SpecArrayEq<Other, N> for T {
146+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
147+
impl<T: ~const PartialEq<Other>, Other, const N: usize> const SpecArrayEq<Other, N> for T {
137148
default fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool {
138149
a[..] == b[..]
139150
}
@@ -142,7 +153,8 @@ impl<T: PartialEq<Other>, Other, const N: usize> SpecArrayEq<Other, N> for T {
142153
}
143154
}
144155

145-
impl<T: BytewiseEq<U>, U, const N: usize> SpecArrayEq<U, N> for T {
156+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
157+
impl<T: ~const BytewiseEq<U>, U, const N: usize> const SpecArrayEq<U, N> for T {
146158
fn spec_eq(a: &[T; N], b: &[U; N]) -> bool {
147159
// SAFETY: Arrays are compared element-wise, and don't add any padding
148160
// between elements, so when the elements are `BytewiseEq`, we can

library/core/src/array/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,17 @@ impl const From<Infallible> for TryFromSliceError {
206206
}
207207

208208
#[stable(feature = "rust1", since = "1.0.0")]
209-
impl<T, const N: usize> AsRef<[T]> for [T; N] {
209+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
210+
impl<T, const N: usize> const AsRef<[T]> for [T; N] {
210211
#[inline]
211212
fn as_ref(&self) -> &[T] {
212213
&self[..]
213214
}
214215
}
215216

216217
#[stable(feature = "rust1", since = "1.0.0")]
217-
impl<T, const N: usize> AsMut<[T]> for [T; N] {
218+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
219+
impl<T, const N: usize> const AsMut<[T]> for [T; N] {
218220
#[inline]
219221
fn as_mut(&mut self) -> &mut [T] {
220222
&mut self[..]
@@ -248,7 +250,8 @@ impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
248250
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
249251
/// ```
250252
#[stable(feature = "try_from", since = "1.34.0")]
251-
impl<T, const N: usize> TryFrom<&[T]> for [T; N]
253+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
254+
impl<T, const N: usize> const TryFrom<&[T]> for [T; N]
252255
where
253256
T: Copy,
254257
{
@@ -273,7 +276,8 @@ where
273276
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
274277
/// ```
275278
#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")]
276-
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
279+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
280+
impl<T, const N: usize> const TryFrom<&mut [T]> for [T; N]
277281
where
278282
T: Copy,
279283
{
@@ -298,7 +302,8 @@ where
298302
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
299303
/// ```
300304
#[stable(feature = "try_from", since = "1.34.0")]
301-
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
305+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
306+
impl<'a, T, const N: usize> const TryFrom<&'a [T]> for &'a [T; N] {
302307
type Error = TryFromSliceError;
303308

304309
#[inline]
@@ -320,7 +325,8 @@ impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
320325
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
321326
/// ```
322327
#[stable(feature = "try_from", since = "1.34.0")]
323-
impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {
328+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
329+
impl<'a, T, const N: usize> const TryFrom<&'a mut [T]> for &'a mut [T; N] {
324330
type Error = TryFromSliceError;
325331

326332
#[inline]

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, Hash)]
58+
#[derive_const(Clone, Eq, PartialEq, Ord, PartialOrd)]
5859
#[unstable(feature = "ascii_char", issue = "110998")]
5960
#[repr(u8)]
6061
pub enum AsciiChar {

library/core/src/char/convert.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ impl const From<u8> for char {
181181
///
182182
/// This `struct` is created when using the [`char::from_str`] method.
183183
#[stable(feature = "char_from_str", since = "1.20.0")]
184-
#[derive(Clone, Debug, PartialEq, Eq)]
184+
#[derive(Debug)]
185+
#[derive_const(Clone, PartialEq, Eq)]
185186
pub struct ParseCharError {
186187
kind: CharErrorKind,
187188
}
188189

189-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
190+
#[derive(Copy, Debug)]
191+
#[derive_const(Clone, PartialEq, Eq)]
190192
enum CharErrorKind {
191193
EmptyString,
192194
TooManyChars,
@@ -266,7 +268,8 @@ impl const TryFrom<u32> for char {
266268
/// This `struct` is created by the [`char::try_from<u32>`](char#impl-TryFrom<u32>-for-char) method.
267269
/// See its documentation for more.
268270
#[stable(feature = "try_from", since = "1.34.0")]
269-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
271+
#[derive(Copy, Debug)]
272+
#[derive_const(Clone, PartialEq, Eq)]
270273
pub struct CharTryFromError(());
271274

272275
#[stable(feature = "try_from", since = "1.34.0")]

library/core/src/char/decode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ where
2424
///
2525
/// This `struct` is created when using the [`DecodeUtf16`] type.
2626
#[stable(feature = "decode_utf16", since = "1.9.0")]
27-
#[derive(Debug, Clone, Eq, PartialEq)]
27+
#[derive(Clone, Debug)]
28+
#[derive_const(Eq, PartialEq)]
2829
pub struct DecodeUtf16Error {
2930
code: u16,
3031
}

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

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

0 commit comments

Comments
 (0)