1 | use crate::cmp::BytewiseEq; |
2 | |
3 | #[stable (feature = "rust1" , since = "1.0.0" )] |
4 | impl<T, U, const N: usize> PartialEq<[U; N]> for [T; N] |
5 | where |
6 | T: PartialEq<U>, |
7 | { |
8 | #[inline ] |
9 | fn eq(&self, other: &[U; N]) -> bool { |
10 | SpecArrayEq::spec_eq(self, b:other) |
11 | } |
12 | #[inline ] |
13 | fn ne(&self, other: &[U; N]) -> bool { |
14 | SpecArrayEq::spec_ne(self, b:other) |
15 | } |
16 | } |
17 | |
18 | #[stable (feature = "rust1" , since = "1.0.0" )] |
19 | impl<T, U, const N: usize> PartialEq<[U]> for [T; N] |
20 | where |
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: &[U; N]) => *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: &[U; N]) => *self != *b, |
36 | Err(_) => true, |
37 | } |
38 | } |
39 | } |
40 | |
41 | #[stable (feature = "rust1" , since = "1.0.0" )] |
42 | impl<T, U, const N: usize> PartialEq<[U; N]> for [T] |
43 | where |
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: &[T; N]) => *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: &[T; N]) => *b != *other, |
59 | Err(_) => true, |
60 | } |
61 | } |
62 | } |
63 | |
64 | #[stable (feature = "rust1" , since = "1.0.0" )] |
65 | impl<T, U, const N: usize> PartialEq<&[U]> for [T; N] |
66 | where |
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" )] |
80 | impl<T, U, const N: usize> PartialEq<[U; N]> for &[T] |
81 | where |
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" )] |
95 | impl<T, U, const N: usize> PartialEq<&mut [U]> for [T; N] |
96 | where |
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" )] |
110 | impl<T, U, const N: usize> PartialEq<[U; N]> for &mut [T] |
111 | where |
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" )] |
129 | impl<T: Eq, const N: usize> Eq for [T; N] {} |
130 | |
131 | trait 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 | |
136 | impl<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 | |
145 | impl<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, b:crate::mem::transmute(src:b)) } |
151 | } |
152 | fn spec_ne(a: &[T; N], b: &[U; N]) -> bool { |
153 | !Self::spec_eq(a, b) |
154 | } |
155 | } |
156 | |