Skip to content

Commit fea8369

Browse files
committed
Revert "Partially revert optimization done to array equality with raw_eq"
This reverts commit 644b292.
1 parent 9410686 commit fea8369

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

library/core/src/array/equality.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ where
55
{
66
#[inline]
77
fn eq(&self, other: &[B; N]) -> bool {
8-
self[..] == other[..]
8+
SpecArrayEq::spec_eq(self, other)
99
}
1010
#[inline]
1111
fn ne(&self, other: &[B; N]) -> bool {
12-
self[..] != other[..]
12+
SpecArrayEq::spec_ne(self, other)
1313
}
1414
}
1515

@@ -109,3 +109,47 @@ where
109109

110110
#[stable(feature = "rust1", since = "1.0.0")]
111111
impl<T: Eq, const N: usize> Eq for [T; N] {}
112+
113+
trait SpecArrayEq<Other, const N: usize>: Sized {
114+
fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool;
115+
fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool;
116+
}
117+
118+
impl<T: PartialEq<Other>, Other, const N: usize> SpecArrayEq<Other, N> for T {
119+
default fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool {
120+
a[..] == b[..]
121+
}
122+
default fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool {
123+
a[..] != b[..]
124+
}
125+
}
126+
127+
impl<T: PartialEq<U> + IsRawEqComparable<U>, U, const N: usize> SpecArrayEq<U, N> for T {
128+
fn spec_eq(a: &[T; N], b: &[U; N]) -> bool {
129+
// SAFETY: This is why `IsRawEqComparable` is an `unsafe trait`.
130+
unsafe {
131+
let b = &*b.as_ptr().cast::<[T; N]>();
132+
crate::intrinsics::raw_eq(a, b)
133+
}
134+
}
135+
fn spec_ne(a: &[T; N], b: &[U; N]) -> bool {
136+
!Self::spec_eq(a, b)
137+
}
138+
}
139+
140+
/// `U` exists on here mostly because `min_specialization` didn't let me
141+
/// repeat the `T` type parameter in the above specialization, so instead
142+
/// the `T == U` constraint comes from the impls on this.
143+
/// # Safety
144+
/// - Neither `Self` nor `U` has any padding.
145+
/// - `Self` and `U` have the same layout.
146+
/// - `Self: PartialEq<U>` is byte-wise (this means no floats, among other things)
147+
#[rustc_specialization_trait]
148+
unsafe trait IsRawEqComparable<U> {}
149+
150+
macro_rules! is_raw_comparable {
151+
($($t:ty),+) => {$(
152+
unsafe impl IsRawEqComparable<$t> for $t {}
153+
)+};
154+
}
155+
is_raw_comparable!(bool, char, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

0 commit comments

Comments
 (0)