-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathas_bytes.rs
92 lines (88 loc) · 2.95 KB
/
as_bytes.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
80
81
82
83
84
85
86
87
88
89
90
91
92
/// A type which can be legally converted to raw bytes.
///
/// Types that implement `AsBytes` require the following traits to also
/// be implemented:
/// * [`NoUninit`]: It is undefined behavior to view uninitialized memory so `AsByte` types
/// must not contain any uninitialized memory.
/// * [`FixedLayout`]: In order to be sure that a type does not have uninitialized memory,
/// it must have a fixed layout otherwise it is possible that the compiler rearranges the type
/// to have padding (and thus uninitialized memory).
///
/// [`FixedLayout`]: trait.FixedLayout.html
/// [`NoUninit`]: trait.InvariantFree.html
pub unsafe trait AsBytes: crate::NoUninit + crate::FixedLayout {}
macro_rules! as_bytes_impl {
($($type:ty),*) => {
$(unsafe impl AsBytes for $type {})*
};
}
as_bytes_impl!(
(),
u8,
u16,
u32,
u64,
u128,
i8,
i16,
i32,
i64,
i128,
usize,
isize,
bool,
char,
core::num::NonZeroI8,
core::num::NonZeroU8,
core::num::NonZeroI16,
core::num::NonZeroU16,
core::num::NonZeroI32,
core::num::NonZeroU32,
core::num::NonZeroI64,
core::num::NonZeroU64,
core::num::NonZeroI128,
core::num::NonZeroU128,
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>,
f32,
f64
);
unsafe impl<T> AsBytes for *const T {}
unsafe impl<T> AsBytes for *mut T {}
unsafe impl<T> AsBytes for &T {}
unsafe impl<T> AsBytes for &mut T {}
unsafe impl<T> AsBytes for core::ptr::NonNull<T> {}
unsafe impl<T> AsBytes for Option<core::ptr::NonNull<T>> {}
unsafe impl<T: AsBytes> AsBytes for [T; 0] {}
unsafe impl<T: AsBytes> AsBytes for [T; 1] {}
unsafe impl<T: AsBytes> AsBytes for [T; 2] {}
unsafe impl<T: AsBytes> AsBytes for [T; 3] {}
unsafe impl<T: AsBytes> AsBytes for [T; 4] {}
unsafe impl<T: AsBytes> AsBytes for [T; 5] {}
unsafe impl<T: AsBytes> AsBytes for [T; 6] {}
unsafe impl<T: AsBytes> AsBytes for [T; 7] {}
unsafe impl<T: AsBytes> AsBytes for [T; 8] {}
unsafe impl<T: AsBytes> AsBytes for [T; 9] {}
unsafe impl<T: AsBytes> AsBytes for [T; 10] {}
unsafe impl<T: AsBytes> AsBytes for [T; 11] {}
unsafe impl<T: AsBytes> AsBytes for [T; 12] {}
unsafe impl<T: AsBytes> AsBytes for [T; 13] {}
unsafe impl<T: AsBytes> AsBytes for [T; 14] {}
unsafe impl<T: AsBytes> AsBytes for [T; 15] {}
unsafe impl<T: AsBytes> AsBytes for [T; 16] {}
unsafe impl<T: AsBytes> AsBytes for [T; 17] {}
unsafe impl<T: AsBytes> AsBytes for [T; 18] {}
unsafe impl<T: AsBytes> AsBytes for [T; 19] {}
unsafe impl<T: AsBytes> AsBytes for [T; 20] {}
unsafe impl<T: AsBytes> AsBytes for [T; 21] {}
unsafe impl<T: AsBytes> AsBytes for [T; 22] {}
unsafe impl<T: AsBytes> AsBytes for [T; 23] {}
unsafe impl<T: AsBytes> AsBytes for [T; 24] {}