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")]
14const impl<T, U> 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")]
34const impl<T: [const] Eq> Eq for [T] {}
35
36#[stable(feature = "rust1", since = "1.0.0")]
38#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
39const impl<T: [const] Ord> Ord for [T] {
40 #[ferrocene::prevalidated]
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")]
60const impl<T: [const] PartialOrd> 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")]
118const impl<A, B> 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")]
149const impl<A, B> 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")]
186const impl<A: [const] PartialOrd> SlicePartialOrd for A {
187 default fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
188 let elem_chain = const |a, b| match PartialOrd::partial_cmp(a, b) {
189 Some(Ordering::Equal) => ControlFlow::Continue(()),
190 non_eq => ControlFlow::Break(non_eq),
191 };
192
193 let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::partial_cmp(a, b));
194
195 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
196 b
197 }
198}
199
200#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
201const impl<A: [const] PartialOrd> SliceChain for A {
202 default fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
203 chaining_impl(left, right, PartialOrd::__chaining_lt, usize::__chaining_lt)
204 }
205 default fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
206 chaining_impl(left, right, PartialOrd::__chaining_le, usize::__chaining_le)
207 }
208 default fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
209 chaining_impl(left, right, PartialOrd::__chaining_gt, usize::__chaining_gt)
210 }
211 default fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
212 chaining_impl(left, right, PartialOrd::__chaining_ge, usize::__chaining_ge)
213 }
214}
215
216#[ferrocene::prevalidated]
217#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
218#[inline]
219const fn chaining_impl<'l, 'r, A: PartialOrd, B, C>(
220 left: &'l [A],
221 right: &'r [A],
222 elem_chain: impl [const] Fn(&'l A, &'r A) -> ControlFlow<B> + [const] Destruct,
223 len_chain: impl for<'a> [const] FnOnce(&'a usize, &'a usize) -> ControlFlow<B, C> + [const] Destruct,
224) -> ControlFlow<B, C> {
225 let l = cmp::min(left.len(), right.len());
226
227 let lhs = &left[..l];
230 let rhs = &right[..l];
231
232 let mut i: usize = 0;
234 while i < l {
235 elem_chain(&lhs[i], &rhs[i])?;
236 i += 1;
237 }
238
239 len_chain(&left.len(), &right.len())
240}
241
242#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
256const impl<A: [const] AlwaysApplicableOrd> SlicePartialOrd for A {
257 fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
258 Some(SliceOrd::compare(left, right))
259 }
260}
261
262#[rustc_specialization_trait]
263#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
264const trait AlwaysApplicableOrd: [const] SliceOrd + [const] Ord {}
265
266macro_rules! always_applicable_ord {
267 ($([$($p:tt)*] $t:ty,)*) => {
268 $(impl<$($p)*> AlwaysApplicableOrd for $t {})*
269 }
270}
271
272always_applicable_ord! {
273 [] u8, [] u16, [] u32, [] u64, [] u128, [] usize,
274 [] i8, [] i16, [] i32, [] i64, [] i128, [] isize,
275 [] bool, [] char,
276 [T: ?Sized] *const T, [T: ?Sized] *mut T,
277 [T: AlwaysApplicableOrd] &T,
278 [T: AlwaysApplicableOrd] &mut T,
279 [T: AlwaysApplicableOrd] Option<T>,
280}
281
282#[doc(hidden)]
283#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
284const trait SliceOrd: Sized {
286 fn compare(left: &[Self], right: &[Self]) -> Ordering;
287}
288
289#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
290const impl<A: [const] Ord> SliceOrd for A {
291 #[ferrocene::prevalidated]
292 default fn compare(left: &[Self], right: &[Self]) -> Ordering {
293 let elem_chain = const |a, b| match Ord::cmp(a, b) {
294 Ordering::Equal => ControlFlow::Continue(()),
295 non_eq => ControlFlow::Break(non_eq),
296 };
297
298 let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::cmp(a, b));
299
300 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
301 b
302 }
303}
304
305#[rustc_specialization_trait]
313const unsafe trait UnsignedBytewiseOrd: [const] Ord {}
314
315#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
316const unsafe impl UnsignedBytewiseOrd for bool {}
317#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
318const unsafe impl UnsignedBytewiseOrd for u8 {}
319#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
320const unsafe impl UnsignedBytewiseOrd for NonZero<u8> {}
321#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
322const unsafe impl UnsignedBytewiseOrd for Option<NonZero<u8>> {}
323#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
324const unsafe impl UnsignedBytewiseOrd for ascii::Char {}
325
326#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
329const impl<A: [const] Ord + [const] UnsignedBytewiseOrd> SliceOrd for A {
330 #[inline]
331 fn compare(left: &[Self], right: &[Self]) -> Ordering {
332 let diff = left.len() as isize - right.len() as isize;
335 let len = if left.len() < right.len() { left.len() } else { right.len() };
338 let left = left.as_ptr().cast();
339 let right = right.as_ptr().cast();
340 let mut order = unsafe { compare_bytes(left, right, len) as isize };
346 if order == 0 {
347 order = diff;
348 }
349 order.cmp(&0)
350 }
351}
352
353#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
356const impl<A: [const] PartialOrd + [const] UnsignedBytewiseOrd> SliceChain for A {
357 #[inline]
358 fn chaining_lt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
359 match SliceOrd::compare(left, right) {
360 Ordering::Equal => ControlFlow::Continue(()),
361 ne => ControlFlow::Break(ne.is_lt()),
362 }
363 }
364 #[inline]
365 fn chaining_le(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
366 match SliceOrd::compare(left, right) {
367 Ordering::Equal => ControlFlow::Continue(()),
368 ne => ControlFlow::Break(ne.is_le()),
369 }
370 }
371 #[inline]
372 fn chaining_gt(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
373 match SliceOrd::compare(left, right) {
374 Ordering::Equal => ControlFlow::Continue(()),
375 ne => ControlFlow::Break(ne.is_gt()),
376 }
377 }
378 #[inline]
379 fn chaining_ge(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
380 match SliceOrd::compare(left, right) {
381 Ordering::Equal => ControlFlow::Continue(()),
382 ne => ControlFlow::Break(ne.is_ge()),
383 }
384 }
385}
386
387pub(super) trait SliceContains: Sized {
388 fn slice_contains(&self, x: &[Self]) -> bool;
389}
390
391impl<T> SliceContains for T
392where
393 T: PartialEq,
394{
395 default fn slice_contains(&self, x: &[Self]) -> bool {
396 x.iter().any(|y| *y == *self)
397 }
398}
399
400impl SliceContains for u8 {
401 #[inline]
402 fn slice_contains(&self, x: &[Self]) -> bool {
403 memchr::memchr(*self, x).is_some()
404 }
405}
406
407impl SliceContains for i8 {
408 #[inline]
409 fn slice_contains(&self, x: &[Self]) -> bool {
410 let byte = *self as u8;
411 let bytes: &[u8] = unsafe { from_raw_parts(x.as_ptr() as *const u8, x.len()) };
416 memchr::memchr(byte, bytes).is_some()
417 }
418}
419
420macro_rules! impl_slice_contains {
421 ($($t:ty),*) => {
422 $(
423 impl SliceContains for $t {
424 #[inline]
425 fn slice_contains(&self, arr: &[$t]) -> bool {
426 const LANE_COUNT: usize = 4 * (128 / (size_of::<$t>() * 8));
429 let mut chunks = arr.chunks_exact(LANE_COUNT);
431 for chunk in &mut chunks {
432 if chunk.iter().fold(false, |acc, x| acc | (*x == *self)) {
433 return true;
434 }
435 }
436 return chunks.remainder().iter().any(|x| *x == *self);
438 }
439 }
440 )*
441 };
442}
443
444impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize, char);