1use super::{from_raw_parts, memchr};
4use crate::ascii;
5use crate::cmp::{self, BytewiseEq, Ordering};
6use crate::intrinsics::compare_bytes;
7use crate::marker::Destruct;
8use crate::mem::SizedTypeProperties;
9use crate::num::NonZero;
10use crate::ops::ControlFlow;
11
12#[stable(feature = "rust1", since = "1.0.0")]
13#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
14impl<T, U> const PartialEq<[U]> for [T]
15where
16 T: [const] PartialEq<U>,
17{
18 #[inline]
19 fn eq(&self, other: &[U]) -> bool {
20 let len = self.len();
21 if len == other.len() {
22 unsafe { SlicePartialEq::equal_same_length(self.as_ptr(), other.as_ptr(), len) }
25 } else {
26 false
27 }
28 }
29}
30
31#[stable(feature = "rust1", since = "1.0.0")]
32#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
33impl<T: [const] Eq> const Eq for [T] {}
34
35#[stable(feature = "rust1", since = "1.0.0")]
37#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
38impl<T: [const] Ord> const Ord for [T] {
39 fn cmp(&self, other: &[T]) -> Ordering {
40 SliceOrd::compare(self, other)
41 }
42}
43
44#[inline]
45const fn as_underlying(x: ControlFlow<bool>) -> u8 {
46 unsafe { crate::mem::transmute(x) }
53}
54
55#[stable(feature = "rust1", since = "1.0.0")]
57#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
58impl<T: [const] PartialOrd> const PartialOrd for [T] {
59 #[inline]
60 fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
61 SlicePartialOrd::partial_compare(self, other)
62 }
63 #[inline]
64 fn lt(&self, other: &Self) -> bool {
65 as_underlying(self.__chaining_lt(other)) == 1
74 }
75 #[inline]
76 fn le(&self, other: &Self) -> bool {
77 as_underlying(self.__chaining_le(other)) != 0
78 }
79 #[inline]
80 fn gt(&self, other: &Self) -> bool {
81 as_underlying(self.__chaining_gt(other)) == 1
82 }
83 #[inline]
84 fn ge(&self, other: &Self) -> bool {
85 as_underlying(self.__chaining_ge(other)) != 0
86 }
87 #[inline]
88 fn __chaining_lt(&self, other: &Self) -> ControlFlow<bool> {
89 SliceChain::chaining_lt(self, other)
90 }
91 #[inline]
92 fn __chaining_le(&self, other: &Self) -> ControlFlow<bool> {
93 SliceChain::chaining_le(self, other)
94 }
95 #[inline]
96 fn __chaining_gt(&self, other: &Self) -> ControlFlow<bool> {
97 SliceChain::chaining_gt(self, other)
98 }
99 #[inline]
100 fn __chaining_ge(&self, other: &Self) -> ControlFlow<bool> {
101 SliceChain::chaining_ge(self, other)
102 }
103}
104
105#[doc(hidden)]
106#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
108const trait SlicePartialEq<B> {
109 unsafe fn equal_same_length(lhs: *const Self, rhs: *const B, len: usize) -> bool;
112}
113
114#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
116impl<A, B> const SlicePartialEq<B> for A
117where
118 A: [const] PartialEq<B>,
119{
120 #[rustc_no_mir_inline]
125 default unsafe fn equal_same_length(lhs: *const Self, rhs: *const B, len: usize) -> bool {
126 let mut idx = 0;
131 while idx < len {
132 if unsafe { *lhs.add(idx) != *rhs.add(idx) } {
134 return false;
135 }
136 idx += 1;
137 }
138
139 true
140 }
141}
142
143#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
146impl<A, B> const SlicePartialEq<B> for A
147where
148 A: [const] BytewiseEq<B>,
149{
150 #[inline]
151 unsafe fn equal_same_length(lhs: *const Self, rhs: *const B, len: usize) -> bool {
152 unsafe {
156 let size = crate::intrinsics::unchecked_mul(len, Self::SIZE);
157 compare_bytes(lhs as _, rhs as _, size) == 0
158 }
159 }
160}
161
162#[doc(hidden)]
163#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
164const trait SlicePartialOrd: Sized {
166 fn partial_compare(left: &[Self], right: &[Self]) -> Option<Ordering>;
167}
168
169#[doc(hidden)]
170#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
171const trait SliceChain: Sized {
173 fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
174 fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
175 fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
176 fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
177}
178
179type AlwaysBreak<B> = ControlFlow<B, crate::convert::Infallible>;
180
181#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
182impl<A: [const] PartialOrd> const SlicePartialOrd for A {
183 default fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
184 let elem_chain = const |a, b| match PartialOrd::partial_cmp(a, b) {
185 Some(Ordering::Equal) => ControlFlow::Continue(()),
186 non_eq => ControlFlow::Break(non_eq),
187 };
188
189 let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::partial_cmp(a, b));
190
191 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
192 b
193 }
194}
195
196#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
197impl<A: [const] PartialOrd> const SliceChain for A {
198 default fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
199 chaining_impl(left, right, PartialOrd::__chaining_lt, usize::__chaining_lt)
200 }
201 default fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
202 chaining_impl(left, right, PartialOrd::__chaining_le, usize::__chaining_le)
203 }
204 default fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
205 chaining_impl(left, right, PartialOrd::__chaining_gt, usize::__chaining_gt)
206 }
207 default fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
208 chaining_impl(left, right, PartialOrd::__chaining_ge, usize::__chaining_ge)
209 }
210}
211
212#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
213#[inline]
214const fn chaining_impl<'l, 'r, A: PartialOrd, B, C>(
215 left: &'l [A],
216 right: &'r [A],
217 elem_chain: impl [const] Fn(&'l A, &'r A) -> ControlFlow<B> + [const] Destruct,
218 len_chain: impl for<'a> [const] FnOnce(&'a usize, &'a usize) -> ControlFlow<B, C> + [const] Destruct,
219) -> ControlFlow<B, C> {
220 let l = cmp::min(left.len(), right.len());
221
222 let lhs = &left[..l];
225 let rhs = &right[..l];
226
227 let mut i: usize = 0;
229 while i < l {
230 elem_chain(&lhs[i], &rhs[i])?;
231 i += 1;
232 }
233
234 len_chain(&left.len(), &right.len())
235}
236
237#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
251impl<A: [const] AlwaysApplicableOrd> const SlicePartialOrd for A {
252 fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
253 Some(SliceOrd::compare(left, right))
254 }
255}
256
257#[rustc_specialization_trait]
258#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
259const trait AlwaysApplicableOrd: [const] SliceOrd + [const] Ord {}
260
261macro_rules! always_applicable_ord {
262 ($([$($p:tt)*] $t:ty,)*) => {
263 $(impl<$($p)*> AlwaysApplicableOrd for $t {})*
264 }
265}
266
267always_applicable_ord! {
268 [] u8, [] u16, [] u32, [] u64, [] u128, [] usize,
269 [] i8, [] i16, [] i32, [] i64, [] i128, [] isize,
270 [] bool, [] char,
271 [T: ?Sized] *const T, [T: ?Sized] *mut T,
272 [T: AlwaysApplicableOrd] &T,
273 [T: AlwaysApplicableOrd] &mut T,
274 [T: AlwaysApplicableOrd] Option<T>,
275}
276
277#[doc(hidden)]
278#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
279const trait SliceOrd: Sized {
281 fn compare(left: &[Self], right: &[Self]) -> Ordering;
282}
283
284#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
285impl<A: [const] Ord> const SliceOrd for A {
286 default fn compare(left: &[Self], right: &[Self]) -> Ordering {
287 let elem_chain = const |a, b| match Ord::cmp(a, b) {
288 Ordering::Equal => ControlFlow::Continue(()),
289 non_eq => ControlFlow::Break(non_eq),
290 };
291
292 let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::cmp(a, b));
293
294 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
295 b
296 }
297}
298
299#[rustc_specialization_trait]
307const unsafe trait UnsignedBytewiseOrd: [const] Ord {}
308
309#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
310unsafe impl const UnsignedBytewiseOrd for bool {}
311#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
312unsafe impl const UnsignedBytewiseOrd for u8 {}
313#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
314unsafe impl const UnsignedBytewiseOrd for NonZero<u8> {}
315#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
316unsafe impl const UnsignedBytewiseOrd for Option<NonZero<u8>> {}
317#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
318unsafe impl const UnsignedBytewiseOrd for ascii::Char {}
319
320#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
323impl<A: [const] Ord + [const] UnsignedBytewiseOrd> const SliceOrd for A {
324 #[inline]
325 fn compare(left: &[Self], right: &[Self]) -> Ordering {
326 let diff = left.len() as isize - right.len() as isize;
329 let len = if left.len() < right.len() { left.len() } else { right.len() };
332 let left = left.as_ptr().cast();
333 let right = right.as_ptr().cast();
334 let mut order = unsafe { compare_bytes(left, right, len) as isize };
340 if order == 0 {
341 order = diff;
342 }
343 order.cmp(&0)
344 }
345}
346
347#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
350impl<A: [const] PartialOrd + [const] UnsignedBytewiseOrd> const SliceChain for A {
351 #[inline]
352 fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
353 match SliceOrd::compare(left, right) {
354 Ordering::Equal => ControlFlow::Continue(()),
355 ne => ControlFlow::Break(ne.is_lt()),
356 }
357 }
358 #[inline]
359 fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
360 match SliceOrd::compare(left, right) {
361 Ordering::Equal => ControlFlow::Continue(()),
362 ne => ControlFlow::Break(ne.is_le()),
363 }
364 }
365 #[inline]
366 fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
367 match SliceOrd::compare(left, right) {
368 Ordering::Equal => ControlFlow::Continue(()),
369 ne => ControlFlow::Break(ne.is_gt()),
370 }
371 }
372 #[inline]
373 fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
374 match SliceOrd::compare(left, right) {
375 Ordering::Equal => ControlFlow::Continue(()),
376 ne => ControlFlow::Break(ne.is_ge()),
377 }
378 }
379}
380
381pub(super) trait SliceContains: Sized {
382 fn slice_contains(&self, x: &[Self]) -> bool;
383}
384
385impl<T> SliceContains for T
386where
387 T: PartialEq,
388{
389 default fn slice_contains(&self, x: &[Self]) -> bool {
390 x.iter().any(|y| *y == *self)
391 }
392}
393
394impl SliceContains for u8 {
395 #[inline]
396 fn slice_contains(&self, x: &[Self]) -> bool {
397 memchr::memchr(*self, x).is_some()
398 }
399}
400
401impl SliceContains for i8 {
402 #[inline]
403 fn slice_contains(&self, x: &[Self]) -> bool {
404 let byte = *self as u8;
405 let bytes: &[u8] = unsafe { from_raw_parts(x.as_ptr() as *const u8, x.len()) };
410 memchr::memchr(byte, bytes).is_some()
411 }
412}
413
414macro_rules! impl_slice_contains {
415 ($($t:ty),*) => {
416 $(
417 impl SliceContains for $t {
418 #[inline]
419 fn slice_contains(&self, arr: &[$t]) -> bool {
420 const LANE_COUNT: usize = 4 * (128 / (size_of::<$t>() * 8));
423 let mut chunks = arr.chunks_exact(LANE_COUNT);
425 for chunk in &mut chunks {
426 if chunk.iter().fold(false, |acc, x| acc | (*x == *self)) {
427 return true;
428 }
429 }
430 return chunks.remainder().iter().any(|x| *x == *self);
432 }
433 }
434 )*
435 };
436}
437
438impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize, char);