core/array/
equality.rs

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