rs_zephyr_common/
wrapping.rs1use crate::to_fixed;
2
3pub struct WrappedMaxBytes(pub [u8; 8]);
4
5impl From<i64> for WrappedMaxBytes {
6 fn from(value: i64) -> Self {
7 let mut buf = [0; 8];
8 buf[..8].copy_from_slice(&value.to_be_bytes());
9
10 Self(buf)
11 }
12}
13
14impl From<&i64> for WrappedMaxBytes {
15 fn from(value: &i64) -> Self {
16 let mut buf = [0; 8];
17 buf[..8].copy_from_slice(&value.to_be_bytes());
18
19 Self(buf)
20 }
21}
22
23impl Into<i64> for WrappedMaxBytes {
24 fn into(self) -> i64 {
25 u64::from_be_bytes(self.0[..8].try_into().unwrap()) as i64
26 }
27}
28
29impl WrappedMaxBytes {
30 pub fn array_from_max_parts<const N: usize>(parts: &[i64]) -> [u8; N] {
31 let mut buf = [0_u8; N];
32 let mut i = 0;
33
34 for part in parts {
35 let arr = Self::from(part).0;
36 buf[i..i + 8].copy_from_slice(&arr);
37 i += 8;
38 }
39
40 buf
41 }
42
43 pub fn array_to_max_parts<const N: usize>(array: &[u8]) -> [i64; N] {
44 let mut buf = [0_i64; N];
45 let mut i = 0;
46
47 for n in (0..array.len()).step_by(8) {
48 let part: i64 = Self(to_fixed(array[n..n + 8].to_vec())).into();
49 buf[i] = part;
50 i += 1;
51 }
52
53 buf
54 }
55}