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