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 #[ferrocene::prevalidated]
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 #[ferrocene::prevalidated]
127 default unsafe fn equal_same_length(lhs: *const Self, rhs: *const B, len: usize) -> bool {
128 let mut idx = 0;
133 while idx < len {
134 if unsafe { *lhs.add(idx) != *rhs.add(idx) } {
136 return false;
137 }
138 idx += 1;
139 }
140
141 true
142 }
143}
144
145#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
148impl<A, B> const SlicePartialEq<B> for A
149where
150 A: [const] BytewiseEq<B>,
151{
152 #[inline]
153 #[ferrocene::prevalidated]
154 unsafe fn equal_same_length(lhs: *const Self, rhs: *const B, len: usize) -> bool {
155 unsafe {
159 let size = crate::intrinsics::unchecked_mul(len, Self::SIZE);
160 compare_bytes(lhs as _, rhs as _, size) == 0
161 }
162 }
163}
164
165#[doc(hidden)]
166#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
167const trait SlicePartialOrd: Sized {
169 fn partial_compare(left: &[Self], right: &[Self]) -> Option<Ordering>;
170}
171
172#[doc(hidden)]
173#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
174const trait SliceChain: Sized {
176 fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
177 fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
178 fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
179 fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
180}
181
182type AlwaysBreak<B> = ControlFlow<B, crate::convert::Infallible>;
183
184#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
185impl<A: [const] PartialOrd> const SlicePartialOrd for A {
186 default fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
187 let elem_chain = const |a, b| match PartialOrd::partial_cmp(a, b) {
188 Some(Ordering::Equal) => ControlFlow::Continue(()),
189 non_eq => ControlFlow::Break(non_eq),
190 };
191
192 let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::partial_cmp(a, b));
193
194 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
195 b
196 }
197}
198
199#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
200impl<A: [const] PartialOrd> const SliceChain for A {
201 default fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
202 chaining_impl(left, right, PartialOrd::__chaining_lt, usize::__chaining_lt)
203 }
204 default fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
205 chaining_impl(left, right, PartialOrd::__chaining_le, usize::__chaining_le)
206 }
207 default fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
208 chaining_impl(left, right, PartialOrd::__chaining_gt, usize::__chaining_gt)
209 }
210 default fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
211 chaining_impl(left, right, PartialOrd::__chaining_ge, usize::__chaining_ge)
212 }
213}
214
215#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
216#[inline]
217const fn chaining_impl<'l, 'r, A: PartialOrd, B, C>(
218 left: &'l [A],
219 right: &'r [A],
220 elem_chain: impl [const] Fn(&'l A, &'r A) -> ControlFlow<B> + [const] Destruct,
221 len_chain: impl for<'a> [const] FnOnce(&'a usize, &'a usize) -> ControlFlow<B, C> + [const] Destruct,
222) -> ControlFlow<B, C> {
223 let l = cmp::min(left.len(), right.len());
224
225 let lhs = &left[..l];
228 let rhs = &right[..l];
229
230 let mut i: usize = 0;
232 while i < l {
233 elem_chain(&lhs[i], &rhs[i])?;
234 i += 1;
235 }
236
237 len_chain(&left.len(), &right.len())
238}
239
240#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
254impl<A: [const] AlwaysApplicableOrd> const SlicePartialOrd for A {
255 fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
256 Some(SliceOrd::compare(left, right))
257 }
258}
259
260#[rustc_specialization_trait]
261#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
262const trait AlwaysApplicableOrd: [const] SliceOrd + [const] Ord {}
263
264macro_rules! always_applicable_ord {
265 ($([$($p:tt)*] $t:ty,)*) => {
266 $(impl<$($p)*> AlwaysApplicableOrd for $t {})*
267 }
268}
269
270always_applicable_ord! {
271 [] u8, [] u16, [] u32, [] u64, [] u128, [] usize,
272 [] i8, [] i16, [] i32, [] i64, [] i128, [] isize,
273 [] bool, [] char,
274 [T: ?Sized] *const T, [T: ?Sized] *mut T,
275 [T: AlwaysApplicableOrd] &T,
276 [T: AlwaysApplicableOrd] &mut T,
277 [T: AlwaysApplicableOrd] Option<T>,
278}
279
280#[doc(hidden)]
281#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
282const trait SliceOrd: Sized {
284 fn compare(left: &[Self], right: &[Self]) -> Ordering;
285}
286
287#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
288impl<A: [const] Ord> const SliceOrd for A {
289 default fn compare(left: &[Self], right: &[Self]) -> Ordering {
290 let elem_chain = const |a, b| match Ord::cmp(a, b) {
291 Ordering::Equal => ControlFlow::Continue(()),
292 non_eq => ControlFlow::Break(non_eq),
293 };
294
295 let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::cmp(a, b));
296
297 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
298 b
299 }
300}
301
302#[rustc_specialization_trait]
310const unsafe trait UnsignedBytewiseOrd: [const] Ord {}
311
312#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
313unsafe impl const UnsignedBytewiseOrd for bool {}
314#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
315unsafe impl const UnsignedBytewiseOrd for u8 {}
316#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
317unsafe impl const UnsignedBytewiseOrd for NonZero<u8> {}
318#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
319unsafe impl const UnsignedBytewiseOrd for Option<NonZero<u8>> {}
320#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
321unsafe impl const UnsignedBytewiseOrd for ascii::Char {}
322
323#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
326impl<A: [const] Ord + [const] UnsignedBytewiseOrd> const SliceOrd for A {
327 #[inline]
328 fn compare(left: &[Self], right: &[Self]) -> Ordering {
329 let diff = left.len() as isize - right.len() as isize;
332 let len = if left.len() < right.len() { left.len() } else { right.len() };
335 let left = left.as_ptr().cast();
336 let right = right.as_ptr().cast();
337 let mut order = unsafe { compare_bytes(left, right, len) as isize };
343 if order == 0 {
344 order = diff;
345 }
346 order.cmp(&0)
347 }
348}
349
350#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
353impl<A: [const] PartialOrd + [const] UnsignedBytewiseOrd> const SliceChain for A {
354 #[inline]
355 fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
356 match SliceOrd::compare(left, right) {
357 Ordering::Equal => ControlFlow::Continue(()),
358 ne => ControlFlow::Break(ne.is_lt()),
359 }
360 }
361 #[inline]
362 fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
363 match SliceOrd::compare(left, right) {
364 Ordering::Equal => ControlFlow::Continue(()),
365 ne => ControlFlow::Break(ne.is_le()),
366 }
367 }
368 #[inline]
369 fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
370 match SliceOrd::compare(left, right) {
371 Ordering::Equal => ControlFlow::Continue(()),
372 ne => ControlFlow::Break(ne.is_gt()),
373 }
374 }
375 #[inline]
376 fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
377 match SliceOrd::compare(left, right) {
378 Ordering::Equal => ControlFlow::Continue(()),
379 ne => ControlFlow::Break(ne.is_ge()),
380 }
381 }
382}
383
384pub(super) trait SliceContains: Sized {
385 fn slice_contains(&self, x: &[Self]) -> bool;
386}
387
388impl<T> SliceContains for T
389where
390 T: PartialEq,
391{
392 default fn slice_contains(&self, x: &[Self]) -> bool {
393 x.iter().any(|y| *y == *self)
394 }
395}
396
397impl SliceContains for u8 {
398 #[inline]
399 fn slice_contains(&self, x: &[Self]) -> bool {
400 memchr::memchr(*self, x).is_some()
401 }
402}
403
404impl SliceContains for i8 {
405 #[inline]
406 fn slice_contains(&self, x: &[Self]) -> bool {
407 let byte = *self as u8;
408 let bytes: &[u8] = unsafe { from_raw_parts(x.as_ptr() as *const u8, x.len()) };
413 memchr::memchr(byte, bytes).is_some()
414 }
415}
416
417macro_rules! impl_slice_contains {
418 ($($t:ty),*) => {
419 $(
420 impl SliceContains for $t {
421 #[inline]
422 fn slice_contains(&self, arr: &[$t]) -> bool {
423 const LANE_COUNT: usize = 4 * (128 / (size_of::<$t>() * 8));
426 let mut chunks = arr.chunks_exact(LANE_COUNT);
428 for chunk in &mut chunks {
429 if chunk.iter().fold(false, |acc, x| acc | (*x == *self)) {
430 return true;
431 }
432 }
433 return chunks.remainder().iter().any(|x| *x == *self);
435 }
436 }
437 )*
438 };
439}
440
441impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize, char);