Thanks to visit codestin.com
Credit goes to doc.rust-lang.org

core/array/
equality.rs

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