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