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;

/// Memory size of a [`Word`]
pub const WORD_SIZE: usize = core::mem::size_of::<Word>();

/// Compile time assert that the array is big enough to contain the memory location.
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
}

/// A trait to get safe sub-arrays from a larger array.
pub(super) trait SubArray<const ARR: usize, const ADDR: usize, const SIZE: usize>:
    Index<Range<usize>, Output = [u8]>
{
    /// Compile-time checked range of the sub-array.
    const RANGE: Range<usize> = check_range_bounds::<ARR, ADDR, SIZE>();

    /// Creates a new sub-array from the parent array where the size is checked at compile time.
    fn sub_array(&self) -> [u8; SIZE] {
        self[Self::RANGE]
            .try_into()
            .expect("This can't ever fail due to the compile-time check")
    }

    /// Creates a new fixed size slice from the parent array where the size is checked at compile time.
    fn sized_slice(&self) -> &[u8; SIZE] {
        (&self[Self::RANGE])
            .try_into()
            .expect("This can't ever fail due to the compile-time check")
    }
}

/// A trait to get safe mutable sub-arrays from a larger array.
pub(super) trait SubArrayMut<const ARR: usize, const ADDR: usize, const SIZE: usize>:
    SubArray<ARR, ADDR, SIZE> + IndexMut<Range<usize>>
{
    /// Creates a new mutable sub-array from the parent array where the size is checked at compile time.
    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] {}

/// Store a number at a specific location in this buffer.
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());
}

/// Read a number from a specific location in a buffer.
/// Won't compile if the buffer is too small.
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()
}

/// Read a word from a specific location in a buffer.
/// Won't compile if the buffer is too small.
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))
}

/// Read a word-padded u8 from a specific location in a buffer.
/// Won't compile if the buffer is too small.
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
}

/// Read the a word-padded u16 from a specific location in a buffer.
/// Won't compile if the buffer is too small.
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
}

/// Read the a word-padded u32 from a specific location in a buffer.
/// Won't compile if the buffer is too small.
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
}

/// Read the a word-padded usize from a specific location in a buffer.
/// Won't compile if the buffer is too small.
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
}

/// Store an array at a specific location in a buffer.
/// Won't compile if the buffer is too small.
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);
}

/// Restore an array from a specific location in a buffer.
/// Won't compile if the buffer is too small.
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)
}

/// Get an array from a fixed sized slice.
/// Won't compile if the buffer is not large enough.
/// ```
/// # use fuel_types::bytes::from_array;
/// let mem = [0u8; 2];
/// let _: [u8; 2] = from_array(&mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_array;
/// let mem = [0u8; 1];
/// let _: [u8; 2] = from_array(&mem);
/// ```
pub fn from_array<const ARR: usize, const SIZE: usize>(buf: &[u8; ARR]) -> [u8; SIZE] {
    SubArray::<ARR, 0, SIZE>::sub_array(buf)
}

/// Get an array from a specific location in a fixed sized slice.
/// This won't compile if the buffer is not large enough.
/// ```
/// # use fuel_types::bytes::from_loc;
/// # use fuel_types::MemLoc;
/// let mem = [0u8; 32];
/// let _: [u8; 2] = from_loc(MemLoc::<1, 2>::new(), &mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_loc;
/// # use fuel_types::MemLoc;
/// let mem = [0u8; 32];
/// let _: [u8; 2] = from_loc(MemLoc::<31, 2>::new(), &mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_loc;
/// # use fuel_types::MemLoc;
/// let mem = [0u8; 32];
/// let _: [u8; 2] = from_loc(MemLoc::<34, 2>::new(), &mem);
/// ```
pub fn from_loc<const ARR: usize, const ADDR: usize, const SIZE: usize>(
    // MemLoc is a zero sized type that makes setting the const generic parameter easier.
    _layout: MemLoc<ADDR, SIZE>,
    buf: &[u8; ARR],
) -> [u8; SIZE] {
    SubArray::<ARR, ADDR, SIZE>::sub_array(buf)
}

/// Get a fixed sized slice from a fixed sized slice.
/// Won't compile if the buffer is not large enough.
/// ```
/// # use fuel_types::bytes::from_array_ref;
/// let mem = [0u8; 2];
/// let _: &[u8; 2] = from_array_ref(&mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_array_ref;
/// let mem = [0u8; 1];
/// let _: &[u8; 2] = from_array_ref(&mem);
/// ```
pub fn from_array_ref<const ARR: usize, const SIZE: usize>(buf: &[u8; ARR]) -> &[u8; SIZE] {
    SubArray::<ARR, 0, SIZE>::sized_slice(buf)
}

/// Get a fixed sized mutable slice from a fixed sized slice.
/// Won't compile if the buffer is not large enough.
/// ```
/// # use fuel_types::bytes::from_array_mut;
/// let mut mem = [0u8; 2];
/// let _: &mut [u8; 2] = from_array_mut(&mut mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_array_mut;
/// let mem = [0u8; 1];
/// let _: &mut [u8; 2] = from_array_mut(&mut mem);
/// ```
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)
}

/// Get a fixed sized slice from a specific location in a fixed sized slice.
/// Won't compile if the buffer is not large enough.
/// ```
/// # use fuel_types::bytes::from_loc_ref;
/// # use fuel_types::MemLoc;
/// let mem = [0u8; 32];
/// let _: &[u8; 2] = from_loc_ref(MemLoc::<1, 2>::new(), &mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_loc_ref;
/// # use fuel_types::MemLoc;
/// let mem = [0u8; 32];
/// let _: &[u8; 2] = from_loc_ref(MemLoc::<31, 2>::new(), &mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_loc_ref;
/// # use fuel_types::MemLoc;
/// let mem = [0u8; 32];
/// let _: &[u8; 2] = from_loc_ref(MemLoc::<34, 2>::new(), &mem);
/// ```
pub fn from_loc_ref<const ARR: usize, const ADDR: usize, const SIZE: usize>(
    // MemLoc is a zero sized type that makes setting the const generic parameter easier.
    _layout: MemLoc<ADDR, SIZE>,
    buf: &[u8; ARR],
) -> &[u8; SIZE] {
    SubArray::<ARR, ADDR, SIZE>::sized_slice(buf)
}

/// Get a fixed sized mutable slice from a specific location in a fixed sized slice.
/// Won't compile if the buffer is not large enough.
/// ```
/// # use fuel_types::bytes::from_loc_mut;
/// # use fuel_types::MemLoc;
/// let mut mem = [0u8; 32];
/// let _: &mut [u8; 2] = from_loc_mut(MemLoc::<1, 2>::new(), &mut mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_loc_mut;
/// # use fuel_types::MemLoc;
/// let mut mem = [0u8; 32];
/// let _: &mut [u8; 2] = from_loc_mut(MemLoc::<31, 2>::new(), &mut mem);
/// ```
/// ```compile_fail
/// # use fuel_types::bytes::from_loc_mut;
/// # use fuel_types::MemLoc;
/// let mut mem = [0u8; 32];
/// let _: &mut [u8; 2] = from_loc_mut(MemLoc::<34, 2>::new(), &mut mem);
/// ```
pub fn from_loc_mut<const ARR: usize, const ADDR: usize, const SIZE: usize>(
    // MemLoc is a zero sized type that makes setting the const generic parameter easier.
    _layout: MemLoc<ADDR, SIZE>,
    buf: &mut [u8; ARR],
) -> &mut [u8; SIZE] {
    SubArrayMut::<ARR, ADDR, SIZE>::sized_slice_mut(buf)
}