1use crate::cmp::BytewiseEq;
2use crate::convert::TryInto;
3
4#[stable(feature = "rust1", since = "1.0.0")]
5impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]
6where
7 A: PartialEq<B>,
8{
9 #[inline]
10 fn eq(&self, other: &[B; N]) -> bool {
11 SpecArrayEq::spec_eq(self, b:other)
12 }
13 #[inline]
14 fn ne(&self, other: &[B; N]) -> bool {
15 SpecArrayEq::spec_ne(self, b:other)
16 }
17}
18
19#[stable(feature = "rust1", since = "1.0.0")]
20impl<A, B, const N: usize> PartialEq<[B]> for [A; N]
21where
22 A: PartialEq<B>,
23{
24 #[inline]
25 fn eq(&self, other: &[B]) -> bool {
26 let b: Result<&[B; N], _> = other.try_into();
27 match b {
28 Ok(b: &[B; N]) => *self == *b,
29 Err(_) => false,
30 }
31 }
32 #[inline]
33 fn ne(&self, other: &[B]) -> bool {
34 let b: Result<&[B; N], _> = other.try_into();
35 match b {
36 Ok(b: &[B; N]) => *self != *b,
37 Err(_) => true,
38 }
39 }
40}
41
42#[stable(feature = "rust1", since = "1.0.0")]
43impl<A, B, const N: usize> PartialEq<[A; N]> for [B]
44where
45 B: PartialEq<A>,
46{
47 #[inline]
48 fn eq(&self, other: &[A; N]) -> bool {
49 let b: Result<&[B; N], _> = self.try_into();
50 match b {
51 Ok(b: &[B; N]) => *b == *other,
52 Err(_) => false,
53 }
54 }
55 #[inline]
56 fn ne(&self, other: &[A; N]) -> bool {
57 let b: Result<&[B; N], _> = self.try_into();
58 match b {
59 Ok(b: &[B; N]) => *b != *other,
60 Err(_) => true,
61 }
62 }
63}
64
65#[stable(feature = "rust1", since = "1.0.0")]
66impl<A, B, const N: usize> PartialEq<&[B]> for [A; N]
67where
68 A: PartialEq<B>,
69{
70 #[inline]
71 fn eq(&self, other: &&[B]) -> bool {
72 *self == **other
73 }
74 #[inline]
75 fn ne(&self, other: &&[B]) -> bool {
76 *self != **other
77 }
78}
79
80#[stable(feature = "rust1", since = "1.0.0")]
81impl<A, B, const N: usize> PartialEq<[A; N]> for &[B]
82where
83 B: PartialEq<A>,
84{
85 #[inline]
86 fn eq(&self, other: &[A; N]) -> bool {
87 **self == *other
88 }
89 #[inline]
90 fn ne(&self, other: &[A; N]) -> bool {
91 **self != *other
92 }
93}
94
95#[stable(feature = "rust1", since = "1.0.0")]
96impl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N]
97where
98 A: PartialEq<B>,
99{
100 #[inline]
101 fn eq(&self, other: &&mut [B]) -> bool {
102 *self == **other
103 }
104 #[inline]
105 fn ne(&self, other: &&mut [B]) -> bool {
106 *self != **other
107 }
108}
109
110#[stable(feature = "rust1", since = "1.0.0")]
111impl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B]
112where
113 B: PartialEq<A>,
114{
115 #[inline]
116 fn eq(&self, other: &[A; N]) -> bool {
117 **self == *other
118 }
119 #[inline]
120 fn ne(&self, other: &[A; N]) -> bool {
121 **self != *other
122 }
123}
124
125// NOTE: some less important impls are omitted to reduce code bloat
126// __impl_slice_eq2! { [A; $N], &'b [B; $N] }
127// __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
128
129#[stable(feature = "rust1", since = "1.0.0")]
130impl<T: Eq, const N: usize> Eq for [T; N] {}
131
132trait SpecArrayEq<Other, const N: usize>: Sized {
133 fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool;
134 fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool;
135}
136
137impl<T: PartialEq<Other>, Other, const N: usize> SpecArrayEq<Other, N> for T {
138 default fn spec_eq(a: &[Self; N], b: &[Other; N]) -> bool {
139 a[..] == b[..]
140 }
141 default fn spec_ne(a: &[Self; N], b: &[Other; N]) -> bool {
142 a[..] != b[..]
143 }
144}
145
146impl<T: BytewiseEq<U>, U, const N: usize> SpecArrayEq<U, N> for T {
147 fn spec_eq(a: &[T; N], b: &[U; N]) -> bool {
148 // SAFETY: Arrays are compared element-wise, and don't add any padding
149 // between elements, so when the elements are `BytewiseEq`, we can
150 // compare the entire array at once.
151 unsafe { crate::intrinsics::raw_eq(a, b:crate::mem::transmute(src:b)) }
152 }
153 fn spec_ne(a: &[T; N], b: &[U; N]) -> bool {
154 !Self::spec_eq(a, b)
155 }
156}
157