core/cmp/
bytewise.rs

1use 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]
20pub(crate) const unsafe trait BytewiseEq<Rhs = Self>:
21    [const] PartialEq<Rhs> + Sized
22{
23}
24
25macro_rules! is_bytewise_comparable {
26    ($($t:ty),+ $(,)?) => {$(
27        unsafe impl const BytewiseEq for $t {}
28    )+};
29}
30
31// SAFETY: All the ordinary integer types have no padding, and are not pointers.
32is_bytewise_comparable!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
33
34// SAFETY: These have *niches*, but no *padding* and no *provenance*,
35// so we can compare them directly.
36is_bytewise_comparable!(bool, char, super::Ordering);
37
38// SAFETY: Similarly, the `NonZero` type has a niche, but no undef and no pointers,
39// and they compare like their underlying numeric type.
40is_bytewise_comparable!(
41    NonZero<u8>,
42    NonZero<u16>,
43    NonZero<u32>,
44    NonZero<u64>,
45    NonZero<u128>,
46    NonZero<usize>,
47    NonZero<i8>,
48    NonZero<i16>,
49    NonZero<i32>,
50    NonZero<i64>,
51    NonZero<i128>,
52    NonZero<isize>,
53);
54
55// SAFETY: The `NonZero` type has the "null" optimization guaranteed, and thus
56// are also safe to equality-compare bitwise inside an `Option`.
57// The way `PartialOrd` is defined for `Option` means that this wouldn't work
58// for `<` or `>` on the signed types, but since we only do `==` it's fine.
59is_bytewise_comparable!(
60    Option<NonZero<u8>>,
61    Option<NonZero<u16>>,
62    Option<NonZero<u32>>,
63    Option<NonZero<u64>>,
64    Option<NonZero<u128>>,
65    Option<NonZero<usize>>,
66    Option<NonZero<i8>>,
67    Option<NonZero<i16>>,
68    Option<NonZero<i32>>,
69    Option<NonZero<i64>>,
70    Option<NonZero<i128>>,
71    Option<NonZero<isize>>,
72);
73
74macro_rules! is_bytewise_comparable_array_length {
75    ($($n:literal),+ $(,)?) => {$(
76        // SAFETY: Arrays have no padding between elements, so if the elements are
77        // `BytewiseEq`, then the whole array can be too.
78        unsafe impl<T: BytewiseEq<U>, U> BytewiseEq<[U; $n]> for [T; $n] {}
79    )+};
80}
81
82// Frustratingly, this can't be made const-generic as it gets
83//    error: specializing impl repeats parameter `N`
84// so just do it for a couple of plausibly-common ones.
85is_bytewise_comparable_array_length!(0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64);