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/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 @@ -37,9 +39,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 @@ -58,9 +61,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 @@ -73,9 +77,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 @@ -88,9 +93,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 @@ -103,9 +109,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 @@ -122,14 +129,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 @@ -138,7 +149,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
Loading
Loading