-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbyte_complete.rs
79 lines (75 loc) · 3.11 KB
/
byte_complete.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::Zeroable;
/// A marker trait representing types are valid no matter what byte values represent them in memory
///
/// Types like `u8` are `ByteComplete` because no matter what value the byte that represents the `u8`
/// is in memory, it's guranteed to be a valid `u8`. `bool` is not `ByteComplete` because only the bytes
/// `0b1` and `0b0` are valid `bool`s.
///
/// `ByteComplete` is a more general version of `Zeroable` which has the same constraints but only for
/// zero bytes.
///
/// # `ByteComplete` vs `FromBytes`
/// `ByteComplete` types are not guranteed to have fixed layouts so creating types from bytes
/// may not be predictable though it will always yield a valid value. If you want predictability,
/// use `FromBytes`.
///
/// So why is `ByteComplete` useful? There may be situations where you simply want to allocate
/// `ByteComplete` values and do not want to ensure that their memory is in some given state before use.
/// `ByteComplete` means that types are always in some valid state no matter their memory.
pub unsafe trait ByteComplete: Zeroable {}
macro_rules! byte_complete_impl {
($($type:ty),*) => {
$(unsafe impl ByteComplete for $type {})*
};
}
byte_complete_impl!(
(),
u8,
u16,
u32,
u64,
u128,
i8,
i16,
i32,
i64,
i128,
isize,
usize,
Option<core::num::NonZeroI8>,
Option<core::num::NonZeroU8>,
Option<core::num::NonZeroI16>,
Option<core::num::NonZeroU16>,
Option<core::num::NonZeroI32>,
Option<core::num::NonZeroU32>,
Option<core::num::NonZeroI64>,
Option<core::num::NonZeroU64>,
Option<core::num::NonZeroI128>,
Option<core::num::NonZeroU128>
);
unsafe impl<T> ByteComplete for Option<core::ptr::NonNull<T>> {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 0] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 1] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 2] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 3] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 4] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 5] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 6] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 7] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 8] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 9] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 10] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 11] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 12] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 13] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 14] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 15] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 16] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 17] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 18] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 19] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 20] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 21] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 22] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 23] {}
unsafe impl<T: ByteComplete> ByteComplete for [T; 24] {}