1 use crate::convert::TryInto;
2 use crate::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
3 use crate::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
5 #[stable(feature = "rust1", since = "1.0.0")]
6 impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]
11 fn eq(&self, other: &[B; N]) -> bool {
12 SpecArrayEq::spec_eq(self, other)
15 fn ne(&self, other: &[B; N]) -> bool {
16 SpecArrayEq::spec_ne(self, other)
20 #[stable(feature = "rust1", since = "1.0.0")]
21 impl<A, B, const N: usize> PartialEq<[B]> for [A; N]
26 fn eq(&self, other: &[B]) -> bool {
27 let b: Result<&[B; N], _> = other.try_into();
34 fn ne(&self, other: &[B]) -> bool {
35 let b: Result<&[B; N], _> = other.try_into();
43 #[stable(feature = "rust1", since = "1.0.0")]
44 impl<A, B, const N: usize> PartialEq<[A; N]> for [B]
49 fn eq(&self, other: &[A; N]) -> bool {
50 let b: Result<&[B; N], _> = self.try_into();
52 Ok(b) => *b == *other,
57 fn ne(&self, other: &[A; N]) -> bool {
58 let b: Result<&[B; N], _> = self.try_into();
60 Ok(b) => *b != *other,
66 #[stable(feature = "rust1", since = "1.0.0")]
67 impl<A, B, const N: usize> PartialEq<&[B]> for [A; N]
72 fn eq(&self, other: &&[B]) -> bool {
76 fn ne(&self, other: &&[B]) -> bool {
81 #[stable(feature = "rust1", since = "1.0.0")]
82 impl<A, B, const N: usize> PartialEq<[A; N]> for &[B]
87 fn eq(&self, other: &[A; N]) -> bool {
91 fn ne(&self, other: &[A; N]) -> bool {
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N]
102 fn eq(&self, other: &&mut [B]) -> bool {
106 fn ne(&self, other: &&mut [B]) -> bool {
111 #[stable(feature = "rust1", since = "1.0.0")]
112 impl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B]
117 fn eq(&self, other: &[A; N]) -> bool {
121 fn ne(&self, other: &[A; N]) -> bool {
126 // NOTE: some less important impls are omitted to reduce code bloat
127 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
128 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
130 #[stable(feature = "rust1", since = "1.0.0")]
131 impl<T: Eq, const N: usize> Eq for [T; N] {}
133 trait SpecArrayEq<Other, const N: usize>: Sized {
134 fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool;
135 fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool;
138 impl<T: PartialEq<Other>, Other, const N: usize> SpecArrayEq<Other, N> for T {
139 default fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool {
142 default fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool {
147 impl<T: IsRawEqComparable<U>, U, const N: usize> SpecArrayEq<U, N> for T {
148 fn spec_eq(a: &[T; N], b: &[U; N]) -> bool {
149 // SAFETY: This is why `IsRawEqComparable` is an `unsafe trait`.
151 let b = &*b.as_ptr().cast::<[T; N]>();
152 crate::intrinsics::raw_eq(a, b)
155 fn spec_ne(a: &[T; N], b: &[U; N]) -> bool {
160 /// `U` exists on here mostly because `min_specialization` didn't let me
161 /// repeat the `T` type parameter in the above specialization, so instead
162 /// the `T == U` constraint comes from the impls on this.
164 /// - Neither `Self` nor `U` has any padding.
165 /// - `Self` and `U` have the same layout.
166 /// - `Self: PartialEq<U>` is byte-wise (this means no floats, among other things)
167 #[rustc_specialization_trait]
168 unsafe trait IsRawEqComparable<U>: PartialEq<U> {}
170 macro_rules! is_raw_eq_comparable {
171 ($($t:ty),+ $(,)?) => {$(
172 unsafe impl IsRawEqComparable<$t> for $t {}
176 // SAFETY: All the ordinary integer types have no padding, and are not pointers.
177 is_raw_eq_comparable!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
179 // SAFETY: bool and char have *niches*, but no *padding* (and these are not pointer types), so this
181 is_raw_eq_comparable!(bool, char);
183 // SAFETY: Similarly, the non-zero types have a niche, but no undef and no pointers,
184 // and they compare like their underlying numeric type.
185 is_raw_eq_comparable!(
200 // SAFETY: The NonZero types have the "null" optimization guaranteed, and thus
201 // are also safe to equality-compare bitwise inside an `Option`.
202 // The way `PartialOrd` is defined for `Option` means that this wouldn't work
203 // for `<` or `>` on the signed types, but since we only do `==` it's fine.
204 is_raw_eq_comparable!(
210 Option<NonZeroUsize>,
216 Option<NonZeroIsize>,