Skip to content

Commit

Permalink
Partially revert optimization done to array equality with raw_eq
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Dec 4, 2021
1 parent e6e2f15 commit 644b292
Showing 1 changed file with 2 additions and 46 deletions.
48 changes: 2 additions & 46 deletions library/core/src/array/equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ where
{
#[inline]
fn eq(&self, other: &[B; N]) -> bool {
SpecArrayEq::spec_eq(self, other)
self[..] == other[..]
}
#[inline]
fn ne(&self, other: &[B; N]) -> bool {
SpecArrayEq::spec_ne(self, other)
self[..] != other[..]
}
}

Expand Down Expand Up @@ -109,47 +109,3 @@ where

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

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 {
default fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool {
a[..] == b[..]
}
default fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool {
a[..] != b[..]
}
}

impl<T: PartialEq<U> + IsRawEqComparable<U>, U, const N: usize> SpecArrayEq<U, N> for T {
fn spec_eq(a: &[T; N], b: &[U; N]) -> bool {
// SAFETY: This is why `IsRawEqComparable` is an `unsafe trait`.
unsafe {
let b = &*b.as_ptr().cast::<[T; N]>();
crate::intrinsics::raw_eq(a, b)
}
}
fn spec_ne(a: &[T; N], b: &[U; N]) -> bool {
!Self::spec_eq(a, b)
}
}

/// `U` exists on here mostly because `min_specialization` didn't let me
/// repeat the `T` type parameter in the above specialization, so instead
/// the `T == U` constraint comes from the impls on this.
/// # Safety
/// - Neither `Self` nor `U` has any padding.
/// - `Self` and `U` have the same layout.
/// - `Self: PartialEq<U>` is byte-wise (this means no floats, among other things)
#[rustc_specialization_trait]
unsafe trait IsRawEqComparable<U> {}

macro_rules! is_raw_comparable {
($($t:ty),+) => {$(
unsafe impl IsRawEqComparable<$t> for $t {}
)+};
}
is_raw_comparable!(bool, char, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

0 comments on commit 644b292

Please sign in to comment.