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, transmute_copy};
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 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")]
33const impl<T: [const] Eq> Eq for [T] {}
34
35#[stable(feature = "rust1", since = "1.0.0")]
37#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
38const impl<T: [const] Ord> 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")]
58const impl<T: [const] PartialOrd> 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_less(other)) == 1
74 }
75 #[inline]
76 fn le(&self, other: &Self) -> bool {
77 as_underlying(self.__chaining_less(other)) != 0
78 }
79 #[inline]
80 fn gt(&self, other: &Self) -> bool {
81 as_underlying(self.__chaining_greater(other)) == 1
82 }
83 #[inline]
84 fn ge(&self, other: &Self) -> bool {
85 as_underlying(self.__chaining_greater(other)) != 0
86 }
87 #[inline]
88 fn __chaining_less(&self, other: &Self) -> ControlFlow<bool> {
89 SliceChain::chaining_less(self, other)
90 }
91 #[inline]
92 fn __chaining_greater(&self, other: &Self) -> ControlFlow<bool> {
93 SliceChain::chaining_greater(self, other)
94 }
95}
96
97#[doc(hidden)]
98#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
100const trait SlicePartialEq<B> {
101 unsafe fn equal_same_length(lhs: *const Self, rhs: *const B, len: usize) -> bool;
104}
105
106#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
108const impl<A, B> SlicePartialEq<B> for A
109where
110 A: [const] PartialEq<B>,
111{
112 #[rustc_no_mir_inline]
117 default unsafe fn equal_same_length(lhs: *const Self, rhs: *const B, len: usize) -> bool {
118 for idx in 0..len {
122 if unsafe { *lhs.add(idx) != *rhs.add(idx) } {
124 return false;
125 }
126 }
127
128 true
129 }
130}
131
132#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
135const impl<A, B> SlicePartialEq<B> for A
136where
137 A: [const] BytewiseEq<B>,
138{
139 #[inline]
140 unsafe fn equal_same_length(lhs: *const Self, rhs: *const B, len: usize) -> bool {
141 unsafe {
145 let size = crate::intrinsics::unchecked_mul(len, Self::SIZE);
146 compare_bytes(lhs as _, rhs as _, size) == 0
147 }
148 }
149}
150
151#[doc(hidden)]
152#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
153const trait SlicePartialOrd: Sized {
155 fn partial_compare(left: &[Self], right: &[Self]) -> Option<Ordering>;
156}
157
158#[doc(hidden)]
159#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
160const trait SliceChain: Sized {
162 fn chaining_less(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
163 fn chaining_greater(left: &[Self], right: &[Self]) -> ControlFlow<bool>;
164}
165
166type AlwaysBreak<B> = ControlFlow<B, !>;
167
168#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
169const impl<A: [const] PartialOrd> SlicePartialOrd for A {
170 default fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
171 let elem_chain = const |a, b| match PartialOrd::partial_cmp(a, b) {
172 Some(Ordering::Equal) => ControlFlow::Continue(()),
173 non_eq => ControlFlow::Break(non_eq),
174 };
175
176 let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::partial_cmp(a, b));
177
178 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
179 b
180 }
181}
182
183#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
184const impl<A: [const] PartialOrd> SliceChain for A {
185 default fn chaining_less(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
186 chaining_impl(left, right, PartialOrd::__chaining_less, usize::__chaining_less)
187 }
188 default fn chaining_greater(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
189 chaining_impl(left, right, PartialOrd::__chaining_greater, usize::__chaining_greater)
190 }
191}
192
193#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
194#[inline]
195const fn chaining_impl<'l, 'r, A: PartialOrd, B, C>(
196 left: &'l [A],
197 right: &'r [A],
198 elem_chain: impl [const] Fn(&'l A, &'r A) -> ControlFlow<B> + [const] Destruct,
199 len_chain: impl for<'a> [const] FnOnce(&'a usize, &'a usize) -> ControlFlow<B, C> + [const] Destruct,
200) -> ControlFlow<B, C> {
201 let l = cmp::min(left.len(), right.len());
202
203 let lhs = &left[..l];
206 let rhs = &right[..l];
207
208 for i in 0..l {
209 elem_chain(&lhs[i], &rhs[i])?;
210 }
211
212 len_chain(&left.len(), &right.len())
213}
214
215#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
229const impl<A: [const] AlwaysApplicableOrd> SlicePartialOrd for A {
230 fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
231 Some(SliceOrd::compare(left, right))
232 }
233}
234
235#[rustc_specialization_trait]
236#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
237const trait AlwaysApplicableOrd: [const] SliceOrd + [const] Ord {}
238
239macro_rules! always_applicable_ord {
240 ($([$($p:tt)*] $t:ty,)*) => {
241 $(impl<$($p)*> AlwaysApplicableOrd for $t {})*
242 }
243}
244
245always_applicable_ord! {
246 [] u8, [] u16, [] u32, [] u64, [] u128, [] usize,
247 [] i8, [] i16, [] i32, [] i64, [] i128, [] isize,
248 [] bool, [] char,
249 [T: ?Sized] *const T, [T: ?Sized] *mut T,
250 [T: AlwaysApplicableOrd] &T,
251 [T: AlwaysApplicableOrd] &mut T,
252 [T: AlwaysApplicableOrd] Option<T>,
253}
254
255#[doc(hidden)]
256#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
257const trait SliceOrd: Sized {
259 fn compare(left: &[Self], right: &[Self]) -> Ordering;
260}
261
262#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
263const impl<A: [const] Ord> SliceOrd for A {
264 default fn compare(left: &[Self], right: &[Self]) -> Ordering {
265 let elem_chain = const |a, b| match Ord::cmp(a, b) {
266 Ordering::Equal => ControlFlow::Continue(()),
267 non_eq => ControlFlow::Break(non_eq),
268 };
269
270 let len_chain = const |a: &_, b: &_| ControlFlow::Break(usize::cmp(a, b));
271
272 let AlwaysBreak::Break(b) = chaining_impl(left, right, elem_chain, len_chain);
273 b
274 }
275}
276
277#[rustc_specialization_trait]
285const unsafe trait UnsignedBytewiseOrd: [const] Ord {}
286
287#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
288const unsafe impl UnsignedBytewiseOrd for bool {}
289#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
290const unsafe impl UnsignedBytewiseOrd for u8 {}
291#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
292const unsafe impl UnsignedBytewiseOrd for NonZero<u8> {}
293#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
294const unsafe impl UnsignedBytewiseOrd for Option<NonZero<u8>> {}
295#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
296const unsafe impl UnsignedBytewiseOrd for ascii::Char {}
297
298#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
301const impl<A: [const] Ord + [const] UnsignedBytewiseOrd> SliceOrd for A {
302 #[inline]
303 fn compare(left: &[Self], right: &[Self]) -> Ordering {
304 let diff = left.len() as isize - right.len() as isize;
307 let len = if left.len() < right.len() { left.len() } else { right.len() };
310 let left = left.as_ptr().cast();
311 let right = right.as_ptr().cast();
312 let mut order = unsafe { compare_bytes(left, right, len) as isize };
318 if order == 0 {
319 order = diff;
320 }
321 order.cmp(&0)
322 }
323}
324
325#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
328const impl<A: [const] PartialOrd + [const] UnsignedBytewiseOrd> SliceChain for A {
329 #[inline]
330 fn chaining_less(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
331 match SliceOrd::compare(left, right) {
332 Ordering::Equal => ControlFlow::Continue(()),
333 ne => ControlFlow::Break(ne.is_lt()),
334 }
335 }
336 #[inline]
337 fn chaining_greater(left: &[Self], right: &[Self]) -> ControlFlow<bool> {
338 match SliceOrd::compare(left, right) {
339 Ordering::Equal => ControlFlow::Continue(()),
340 ne => ControlFlow::Break(ne.is_gt()),
341 }
342 }
343}
344
345pub(super) trait SliceContains: Sized {
346 fn slice_contains(&self, x: &[Self]) -> bool;
347}
348
349impl<T> SliceContains for T
350where
351 T: PartialEq,
352{
353 #[expect(clippy::manual_contains, reason = "implements slice_contains")]
354 default fn slice_contains(&self, x: &[Self]) -> bool {
355 x.iter().any(|y| *y == *self)
356 }
357}
358
359impl<T: BytewiseEq> SliceContains for T {
360 #[inline]
361 #[expect(clippy::manual_contains, reason = "implements slice_contains")]
362 default fn slice_contains(&self, x: &[Self]) -> bool {
363 if size_of::<T>() == 1 {
364 let (byte, bytes) = unsafe {
368 (transmute_copy::<T, u8>(self), from_raw_parts(x.as_ptr().cast::<u8>(), x.len()))
369 };
370 memchr::memchr(byte, bytes).is_some()
371 } else {
372 x.iter().any(|y| *y == *self)
373 }
374 }
375}
376
377macro_rules! impl_slice_contains {
378 ($($t:ty),*) => {
379 $(
380 impl SliceContains for $t {
381 #[inline]
382 fn slice_contains(&self, arr: &[$t]) -> bool {
383 const LANE_COUNT: usize = 4 * (128 / (size_of::<$t>() * 8));
386 let mut chunks = arr.chunks_exact(LANE_COUNT);
388 for chunk in &mut chunks {
389 if chunk.iter().fold(false, |acc, x| acc | (*x == *self)) {
390 return true;
391 }
392 }
393 return chunks.remainder().iter().any(|x| *x == *self);
395 }
396 }
397 )*
398 };
399}
400
401impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize, char);