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