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