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
pub trait FunctionalVec {
    fn concat(&self, other: &[u8]) -> Vec<u8>;
    fn concat_byte(&self, other: u8) -> Vec<u8>;
}

impl FunctionalVec for Vec<u8> {
    fn concat(&self, other: &[u8]) -> Vec<u8> {
        let mut out = self.clone();
        out.extend_from_slice(other);
        out
    }

    fn concat_byte(&self, other: u8) -> Vec<u8> {
        let mut out = self.clone();
        Vec::<u8>::push(&mut out, other);
        out
    }
}

impl FunctionalVec for &[u8] {
    fn concat(&self, other: &[u8]) -> Vec<u8> {
        let mut out = self.to_vec();
        out.extend_from_slice(other);
        out
    }

    fn concat_byte(&self, other: u8) -> Vec<u8> {
        let mut out = self.to_vec();
        Vec::<u8>::push(&mut out, other);
        out
    }
}

pub trait Conversions {
    fn to_le_bytes(&self) -> Vec<u8>;
}

impl<const LEN: usize> Conversions for [u64; LEN] {
    fn to_le_bytes(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(LEN * 8);
        for item in self {
            out.extend_from_slice(&item.to_le_bytes());
        }
        out
    }
}

impl<const LEN: usize> Conversions for [u32; LEN] {
    fn to_le_bytes(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(LEN * 4);
        for item in self {
            out.extend_from_slice(&item.to_le_bytes());
        }
        out
    }
}

pub fn bytes_to_le_u32s(input: &[u8]) -> Vec<u32> {
    let mut out = Vec::new();
    for block in input.chunks(4) {
        out.push(u32::from_le_bytes(block.try_into().unwrap()));
    }
    out
}