1 | use crate::num::NonZero; |
2 | |
3 | /// Types where `==` & `!=` are equivalent to comparing their underlying bytes. |
4 | /// |
5 | /// Importantly, this means no floating-point types, as those have different |
6 | /// byte representations (like `-0` and `+0`) which compare as the same. |
7 | /// Since byte arrays are `Eq`, that implies that these types are probably also |
8 | /// `Eq`, but that's not technically required to use this trait. |
9 | /// |
10 | /// `Rhs` is *de facto* always `Self`, but the separate parameter is important |
11 | /// to avoid the `specializing impl repeats parameter` error when consuming this. |
12 | /// |
13 | /// # Safety |
14 | /// |
15 | /// - `Self` and `Rhs` have no padding. |
16 | /// - `Self` and `Rhs` have the same layout (size and alignment). |
17 | /// - Neither `Self` nor `Rhs` have provenance, so integer comparisons are correct. |
18 | /// - `<Self as PartialEq<Rhs>>::{eq,ne}` are equivalent to comparing the bytes. |
19 | #[rustc_specialization_trait ] |
20 | pub(crate) unsafe trait BytewiseEq<Rhs = Self>: PartialEq<Rhs> + Sized {} |
21 | |
22 | macro_rules! is_bytewise_comparable { |
23 | ($($t:ty),+ $(,)?) => {$( |
24 | unsafe impl BytewiseEq for $t {} |
25 | )+}; |
26 | } |
27 | |
28 | // SAFETY: All the ordinary integer types have no padding, and are not pointers. |
29 | is_bytewise_comparable!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
30 | |
31 | // SAFETY: These have *niches*, but no *padding* and no *provenance*, |
32 | // so we can compare them directly. |
33 | is_bytewise_comparable!(bool, char, super::Ordering); |
34 | |
35 | // SAFETY: Similarly, the `NonZero` type has a niche, but no undef and no pointers, |
36 | // and they compare like their underlying numeric type. |
37 | is_bytewise_comparable!( |
38 | NonZero<u8>, |
39 | NonZero<u16>, |
40 | NonZero<u32>, |
41 | NonZero<u64>, |
42 | NonZero<u128>, |
43 | NonZero<usize>, |
44 | NonZero<i8>, |
45 | NonZero<i16>, |
46 | NonZero<i32>, |
47 | NonZero<i64>, |
48 | NonZero<i128>, |
49 | NonZero<isize>, |
50 | ); |
51 | |
52 | // SAFETY: The `NonZero` type has the "null" optimization guaranteed, and thus |
53 | // are also safe to equality-compare bitwise inside an `Option`. |
54 | // The way `PartialOrd` is defined for `Option` means that this wouldn't work |
55 | // for `<` or `>` on the signed types, but since we only do `==` it's fine. |
56 | is_bytewise_comparable!( |
57 | Option<NonZero<u8>>, |
58 | Option<NonZero<u16>>, |
59 | Option<NonZero<u32>>, |
60 | Option<NonZero<u64>>, |
61 | Option<NonZero<u128>>, |
62 | Option<NonZero<usize>>, |
63 | Option<NonZero<i8>>, |
64 | Option<NonZero<i16>>, |
65 | Option<NonZero<i32>>, |
66 | Option<NonZero<i64>>, |
67 | Option<NonZero<i128>>, |
68 | Option<NonZero<isize>>, |
69 | ); |
70 | |
71 | macro_rules! is_bytewise_comparable_array_length { |
72 | ($($n:literal),+ $(,)?) => {$( |
73 | // SAFETY: Arrays have no padding between elements, so if the elements are |
74 | // `BytewiseEq`, then the whole array can be too. |
75 | unsafe impl<T: BytewiseEq<U>, U> BytewiseEq<[U; $n]> for [T; $n] {} |
76 | )+}; |
77 | } |
78 | |
79 | // Frustratingly, this can't be made const-generic as it gets |
80 | // error: specializing impl repeats parameter `N` |
81 | // so just do it for a couple of plausibly-common ones. |
82 | is_bytewise_comparable_array_length!(0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64); |
83 | |