Skip to content

Constify Eq, Ord, PartialOrd #144847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const fn size_align<T>() -> (usize, usize) {
/// like this are met, use specific allocators with looser
/// requirements, or use the more lenient `Allocator` interface.)
#[stable(feature = "alloc_layout", since = "1.28.0")]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Copy, Debug, Hash)]
#[derive_const(Clone, PartialEq, Eq)]
#[lang = "alloc_layout"]
pub struct Layout {
// size of the requested block of memory, measured in bytes.
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use crate::ptr::{self, NonNull};
/// something wrong when combining the given input arguments with this
/// allocator.
#[unstable(feature = "allocator_api", issue = "32838")]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Debug)]
#[derive_const(Clone, PartialEq, Eq)]
pub struct AllocError;

#[unstable(
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ impl dyn Any + Send + Sync {
/// std::mem::forget(fake_one_ring);
/// }
/// ```
#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
#[derive(Copy, PartialOrd, Ord)]
#[derive_const(Clone, Eq)]
#[stable(feature = "rust1", since = "1.0.0")]
#[lang = "type_id"]
pub struct TypeId {
Expand Down
46 changes: 29 additions & 17 deletions library/core/src/array/equality.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::cmp::BytewiseEq;

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U, const N: usize> PartialEq<[U; N]> for [T; N]
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl<T, U, const N: usize> const PartialEq<[U; N]> for [T; N]
where
T: PartialEq<U>,
T: ~const PartialEq<U>,
{
#[inline]
fn eq(&self, other: &[U; N]) -> bool {
Expand All @@ -16,9 +17,10 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U, const N: usize> PartialEq<[U]> for [T; N]
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl<T, U, const N: usize> const PartialEq<[U]> for [T; N]
where
T: PartialEq<U>,
T: ~const PartialEq<U>,
{
#[inline]
fn eq(&self, other: &[U]) -> bool {
Expand All @@ -39,9 +41,10 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U, const N: usize> PartialEq<[U; N]> for [T]
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl<T, U, const N: usize> const PartialEq<[U; N]> for [T]
where
T: PartialEq<U>,
T: ~const PartialEq<U>,
{
#[inline]
fn eq(&self, other: &[U; N]) -> bool {
Expand All @@ -62,9 +65,10 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U, const N: usize> PartialEq<&[U]> for [T; N]
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl<T, U, const N: usize> const PartialEq<&[U]> for [T; N]
where
T: PartialEq<U>,
T: ~const PartialEq<U>,
{
#[inline]
fn eq(&self, other: &&[U]) -> bool {
Expand All @@ -77,9 +81,10 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U, const N: usize> PartialEq<[U; N]> for &[T]
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl<T, U, const N: usize> const PartialEq<[U; N]> for &[T]
where
T: PartialEq<U>,
T: ~const PartialEq<U>,
{
#[inline]
fn eq(&self, other: &[U; N]) -> bool {
Expand All @@ -92,9 +97,10 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U, const N: usize> PartialEq<&mut [U]> for [T; N]
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl<T, U, const N: usize> const PartialEq<&mut [U]> for [T; N]
where
T: PartialEq<U>,
T: ~const PartialEq<U>,
{
#[inline]
fn eq(&self, other: &&mut [U]) -> bool {
Expand All @@ -107,9 +113,10 @@ where
}

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

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

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

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

impl<T: BytewiseEq<U>, U, const N: usize> SpecArrayEq<U, N> for T {
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
impl<T: ~const BytewiseEq<U>, U, const N: usize> const SpecArrayEq<U, N> for T {
fn spec_eq(a: &[T; N], b: &[U; N]) -> bool {
// SAFETY: Arrays are compared element-wise, and don't add any padding
// between elements, so when the elements are `BytewiseEq`, we can
Expand Down
18 changes: 12 additions & 6 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,17 @@ impl const From<Infallible> for TryFromSliceError {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, const N: usize> AsRef<[T]> for [T; N] {
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
impl<T, const N: usize> const AsRef<[T]> for [T; N] {
#[inline]
fn as_ref(&self) -> &[T] {
&self[..]
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, const N: usize> AsMut<[T]> for [T; N] {
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
impl<T, const N: usize> const AsMut<[T]> for [T; N] {
#[inline]
fn as_mut(&mut self) -> &mut [T] {
&mut self[..]
Expand Down Expand Up @@ -248,7 +250,8 @@ impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
/// ```
#[stable(feature = "try_from", since = "1.34.0")]
impl<T, const N: usize> TryFrom<&[T]> for [T; N]
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
impl<T, const N: usize> const TryFrom<&[T]> for [T; N]
where
T: Copy,
{
Expand All @@ -273,7 +276,8 @@ where
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
/// ```
#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")]
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
impl<T, const N: usize> const TryFrom<&mut [T]> for [T; N]
where
T: Copy,
{
Expand All @@ -298,7 +302,8 @@ where
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
/// ```
#[stable(feature = "try_from", since = "1.34.0")]
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
impl<'a, T, const N: usize> const TryFrom<&'a [T]> for &'a [T; N] {
type Error = TryFromSliceError;

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

#[inline]
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/ascii/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ use crate::{assert_unsafe_precondition, fmt};
/// [chart]: https://www.unicode.org/charts/PDF/U0000.pdf
/// [NIST FIPS 1-2]: https://nvlpubs.nist.gov/nistpubs/Legacy/FIPS/fipspub1-2-1977.pdf
/// [NamesList]: https://www.unicode.org/Public/15.0.0/ucd/NamesList.txt
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Copy, Hash)]
#[derive_const(Clone, Eq, PartialEq, Ord, PartialOrd)]
#[unstable(feature = "ascii_char", issue = "110998")]
#[repr(u8)]
pub enum AsciiChar {
Expand Down
9 changes: 6 additions & 3 deletions library/core/src/char/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,14 @@ impl const From<u8> for char {
///
/// This `struct` is created when using the [`char::from_str`] method.
#[stable(feature = "char_from_str", since = "1.20.0")]
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Debug)]
#[derive_const(Clone, PartialEq, Eq)]
pub struct ParseCharError {
kind: CharErrorKind,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Debug)]
#[derive_const(Clone, PartialEq, Eq)]
enum CharErrorKind {
EmptyString,
TooManyChars,
Expand Down Expand Up @@ -266,7 +268,8 @@ impl const TryFrom<u32> for char {
/// This `struct` is created by the [`char::try_from<u32>`](char#impl-TryFrom<u32>-for-char) method.
/// See its documentation for more.
#[stable(feature = "try_from", since = "1.34.0")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Copy, Debug)]
#[derive_const(Clone, PartialEq, Eq)]
pub struct CharTryFromError(());

#[stable(feature = "try_from", since = "1.34.0")]
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/char/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ where
///
/// This `struct` is created when using the [`DecodeUtf16`] type.
#[stable(feature = "decode_utf16", since = "1.9.0")]
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Clone, Debug)]
#[derive_const(Eq, PartialEq)]
pub struct DecodeUtf16Error {
code: u16,
}
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/char/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ impl fmt::Display for CaseMappingIter {

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

#[stable(feature = "u8_from_char", since = "1.59.0")]
Expand Down
Loading
Loading