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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use core::{
mem::{size_of, MaybeUninit},
ptr,
};
pub const fn zero<T>() -> T {
unsafe { MaybeUninit::zeroed().assume_init() }
}
pub const fn ones<T>() -> T {
unsafe {
let mut x = MaybeUninit::uninit();
ptr::write_bytes(x.as_mut_ptr(), 0xFF, 1);
x.assume_init()
}
}
pub const fn make_zero<T>(v: &mut T) {
unsafe { ptr::write_bytes(v, 0, 1) }
}
pub const fn read<D, T: Copy>(data: *const D, offset: usize) -> T {
unsafe { *data.cast::<u8>().add(offset).cast() }
}
pub const fn write<D, T: Copy>(data: *mut D, offset: usize, val: T) {
unsafe { data.cast::<u8>().add(offset).cast::<T>().write(val) }
}
pub trait MemValue: Sized + Copy {
fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self;
fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self;
fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self;
fn to_le_bytes(self) -> [u8; size_of::<Self>()];
fn to_be_bytes(self) -> [u8; size_of::<Self>()];
fn to_ne_bytes(self) -> [u8; size_of::<Self>()];
/// # Safety
/// The given pointer must be [valid] for `Self` reads and point to a properly initialized value
/// of `Self`.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn read_le(ptr: *const Self) -> Self;
/// # Safety
/// The given pointer must be aligned to a `Self` boundary, be [valid] for `Self` reads and
/// point to a properly initialized value of `Self`.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn read_le_aligned(ptr: *const Self) -> Self;
/// # Safety
/// The given pointer must be [valid] for `Self` reads and point to a properly initialized value
/// of `Self`.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn read_be(ptr: *const Self) -> Self;
/// # Safety
/// The given pointer must be aligned to a `Self` boundary, be [valid] for `Self` reads and
/// point to a properly initialized value of `Self`.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn read_be_aligned(ptr: *const Self) -> Self;
/// # Safety
/// The given pointer must be [valid] for `Self` reads and point to a properly initialized value
/// of `Self`.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn read_ne(ptr: *const Self) -> Self;
/// # Safety
/// The given pointer must be aligned to a `Self` boundary, be [valid] for `Self` reads and
/// point to a properly initialized value of `Self`.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn read_ne_aligned(ptr: *const Self) -> Self;
/// # Safety
/// The given pointer must be [valid] for `Self` writes.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn write_le(self, ptr: *mut Self);
/// # Safety
/// The given pointer must be aligned to a `Self` boundary and be [valid] for `Self` writes.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn write_le_aligned(self, ptr: *mut Self);
/// # Safety
/// The given pointer must be [valid] for `Self` writes.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn write_be(self, ptr: *mut Self);
/// # Safety
/// The given pointer must be aligned to a `Self` boundary and be [valid] for `Self` writes.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn write_be_aligned(self, ptr: *mut Self);
/// # Safety
/// The given pointer must be [valid] for `Self` writes.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn write_ne(self, ptr: *mut Self);
/// # Safety
/// The given pointer must be aligned to a `Self` boundary and be [valid] for `Self` writes.
///
/// [valid]: https://doc.rust-lang.org/stable/std/ptr/index.html#safety
unsafe fn write_ne_aligned(self, ptr: *mut Self);
}
macro_rules! impl_mem_value {
($($ty: ty),*) => {
$(
#[allow(unused_mut)]
impl MemValue for $ty {
#[inline]
fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
<$ty>::from_le_bytes(bytes)
}
#[inline]
fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
<$ty>::from_be_bytes(bytes)
}
#[inline]
fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
<$ty>::from_ne_bytes(bytes)
}
#[inline]
fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
<$ty>::to_le_bytes(self)
}
#[inline]
fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
<$ty>::to_be_bytes(self)
}
#[inline]
fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
<$ty>::to_ne_bytes(self)
}
#[inline]
unsafe fn read_le(ptr: *const Self) -> Self {
let mut res = ptr.read_unaligned();
#[cfg(not(target_endian = "little"))]
{
res = res.swap_bytes();
}
res
}
#[inline]
unsafe fn read_le_aligned(ptr: *const Self) -> Self {
let mut res = ptr.read();
#[cfg(not(target_endian = "little"))]
{
res = res.swap_bytes();
}
res
}
#[inline]
unsafe fn read_be(ptr: *const Self) -> Self {
let mut res = ptr.read_unaligned();
#[cfg(not(target_endian = "big"))]
{
res = res.swap_bytes();
}
res
}
#[inline]
unsafe fn read_be_aligned(ptr: *const Self) -> Self {
let mut res = ptr.read();
#[cfg(not(target_endian = "big"))]
{
res = res.swap_bytes();
}
res
}
#[inline]
unsafe fn read_ne(ptr: *const Self) -> Self {
ptr.read_unaligned()
}
#[inline]
unsafe fn read_ne_aligned(ptr: *const Self) -> Self {
ptr.read()
}
#[inline]
unsafe fn write_le(mut self, ptr: *mut Self) {
#[cfg(not(target_endian = "little"))]
{
self = self.swap_bytes();
}
ptr.write_unaligned(self);
}
#[inline]
unsafe fn write_le_aligned(mut self, ptr: *mut Self) {
#[cfg(not(target_endian = "little"))]
{
self = self.swap_bytes();
}
ptr.write(self);
}
#[inline]
unsafe fn write_be(mut self, ptr: *mut Self) {
#[cfg(not(target_endian = "big"))]
{
self = self.swap_bytes();
}
ptr.write_unaligned(self);
}
#[inline]
unsafe fn write_be_aligned(mut self, ptr: *mut Self) {
#[cfg(not(target_endian = "big"))]
{
self = self.swap_bytes();
}
ptr.write(self);
}
#[inline]
unsafe fn write_ne(self, ptr: *mut Self) {
ptr.write_unaligned(self);
}
#[inline]
unsafe fn write_ne_aligned(self, ptr: *mut Self) {
ptr.write(self);
}
}
)*
};
}
impl_mem_value!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize);