core/array/
equality.rs

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