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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use crate::{LayoutType, MemLoc, MemLocType, Word};
use core::borrow::Borrow;
use core::ops::Index;
use core::ops::IndexMut;
use core::ops::Range;
pub const WORD_SIZE: usize = core::mem::size_of::<Word>();
const fn check_range_bounds<const ARR: usize, const ADDR: usize, const SIZE: usize>() -> Range<usize> {
assert!(
ARR >= ADDR + SIZE,
"ARR length must be greater than or equal to sub arrays ADDR + SIZE"
);
ADDR..ADDR + SIZE
}
pub(super) trait SubArray<const ARR: usize, const ADDR: usize, const SIZE: usize>:
Index<Range<usize>, Output = [u8]>
{
const RANGE: Range<usize> = check_range_bounds::<ARR, ADDR, SIZE>();
fn sub_array(&self) -> [u8; SIZE] {
self[Self::RANGE]
.try_into()
.expect("This can't ever fail due to the compile-time check")
}
fn sized_slice(&self) -> &[u8; SIZE] {
(&self[Self::RANGE])
.try_into()
.expect("This can't ever fail due to the compile-time check")
}
}
pub(super) trait SubArrayMut<const ARR: usize, const ADDR: usize, const SIZE: usize>:
SubArray<ARR, ADDR, SIZE> + IndexMut<Range<usize>>
{
fn sized_slice_mut(&mut self) -> &mut [u8; SIZE] {
(&mut self[Self::RANGE])
.try_into()
.expect("This can't ever fail due to the compile-time check")
}
}
impl<const ARR: usize, const ADDR: usize, const SIZE: usize> SubArray<ARR, ADDR, SIZE> for [u8; ARR] {}
impl<const ARR: usize, const ADDR: usize, const SIZE: usize> SubArrayMut<ARR, ADDR, SIZE> for [u8; ARR] {}
pub fn store_number_at<const ARR: usize, const ADDR: usize, const SIZE: usize, T>(
buf: &mut [u8; ARR],
layout: LayoutType<ADDR, SIZE, T>,
number: T::Type,
) where
T: MemLocType<ADDR, SIZE>,
<T as MemLocType<ADDR, SIZE>>::Type: Into<Word>,
{
from_loc_mut(layout.loc(), buf).copy_from_slice(&number.into().to_be_bytes());
}
pub fn restore_number_at<const ARR: usize, const ADDR: usize, T>(
buf: &[u8; ARR],
loc: LayoutType<ADDR, WORD_SIZE, T>,
) -> T::Type
where
T: MemLocType<ADDR, WORD_SIZE>,
Word: Into<<T as MemLocType<ADDR, WORD_SIZE>>::Type>,
{
Word::from_be_bytes(from_loc(loc.loc(), buf)).into()
}
pub fn restore_word_at<const ARR: usize, const ADDR: usize, T>(
buf: &[u8; ARR],
loc: LayoutType<ADDR, WORD_SIZE, T>,
) -> Word
where
T: MemLocType<ADDR, WORD_SIZE, Type = Word>,
{
Word::from_be_bytes(from_loc(loc.loc(), buf))
}
pub fn restore_u8_at<const ARR: usize, const ADDR: usize, T>(buf: &[u8; ARR], loc: LayoutType<ADDR, WORD_SIZE, T>) -> u8
where
T: MemLocType<ADDR, WORD_SIZE, Type = u8>,
{
Word::from_be_bytes(from_loc(loc.loc(), buf)) as u8
}
pub fn restore_u16_at<const ARR: usize, const ADDR: usize, T>(
buf: &[u8; ARR],
loc: LayoutType<ADDR, WORD_SIZE, T>,
) -> u16
where
T: MemLocType<ADDR, WORD_SIZE, Type = u16>,
{
Word::from_be_bytes(from_loc(loc.loc(), buf)) as u16
}
pub fn restore_u32_at<const ARR: usize, const ADDR: usize, T>(
buf: &[u8; ARR],
loc: LayoutType<ADDR, WORD_SIZE, T>,
) -> u32
where
T: MemLocType<ADDR, WORD_SIZE, Type = u32>,
{
Word::from_be_bytes(from_loc(loc.loc(), buf)) as u32
}
pub fn restore_usize_at<const ARR: usize, const ADDR: usize, T>(
buf: &[u8; ARR],
loc: LayoutType<ADDR, WORD_SIZE, T>,
) -> usize
where
T: MemLocType<ADDR, WORD_SIZE, Type = Word>,
{
Word::from_be_bytes(from_loc(loc.loc(), buf)) as usize
}
pub fn store_at<const ARR: usize, const ADDR: usize, const SIZE: usize, T>(
buf: &mut [u8; ARR],
layout: LayoutType<ADDR, SIZE, T>,
array: &[u8; SIZE],
) where
T: MemLocType<ADDR, SIZE>,
<T as MemLocType<ADDR, SIZE>>::Type: Borrow<[u8; SIZE]>,
{
from_loc_mut(layout.loc(), buf).copy_from_slice(array);
}
pub fn restore_at<const ARR: usize, const ADDR: usize, const SIZE: usize, T>(
buf: &[u8; ARR],
loc: LayoutType<ADDR, SIZE, T>,
) -> [u8; SIZE]
where
T: MemLocType<ADDR, SIZE>,
[u8; SIZE]: From<<T as MemLocType<ADDR, SIZE>>::Type>,
{
from_loc(loc.loc(), buf)
}
pub fn from_array<const ARR: usize, const SIZE: usize>(buf: &[u8; ARR]) -> [u8; SIZE] {
SubArray::<ARR, 0, SIZE>::sub_array(buf)
}
pub fn from_loc<const ARR: usize, const ADDR: usize, const SIZE: usize>(
_layout: MemLoc<ADDR, SIZE>,
buf: &[u8; ARR],
) -> [u8; SIZE] {
SubArray::<ARR, ADDR, SIZE>::sub_array(buf)
}
pub fn from_array_ref<const ARR: usize, const SIZE: usize>(buf: &[u8; ARR]) -> &[u8; SIZE] {
SubArray::<ARR, 0, SIZE>::sized_slice(buf)
}
pub fn from_array_mut<const ARR: usize, const SIZE: usize>(buf: &mut [u8; ARR]) -> &mut [u8; SIZE] {
SubArrayMut::<ARR, 0, SIZE>::sized_slice_mut(buf)
}
pub fn from_loc_ref<const ARR: usize, const ADDR: usize, const SIZE: usize>(
_layout: MemLoc<ADDR, SIZE>,
buf: &[u8; ARR],
) -> &[u8; SIZE] {
SubArray::<ARR, ADDR, SIZE>::sized_slice(buf)
}
pub fn from_loc_mut<const ARR: usize, const ADDR: usize, const SIZE: usize>(
_layout: MemLoc<ADDR, SIZE>,
buf: &mut [u8; ARR],
) -> &mut [u8; SIZE] {
SubArrayMut::<ARR, ADDR, SIZE>::sized_slice_mut(buf)
}