summaryrefslogtreecommitdiff
path: root/src/byte_encode.rs
blob: 4be57b2d28c99d7658d47ae8a1c0710fab6d9c62 (plain) (blame)
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
pub trait ByteEncode<const N: usize>: Sized + Copy
{
	fn from_le_bytes(buffer: &[u8; N]) -> Self;
	fn to_le_bytes(&self) -> [u8; N];
}

impl ByteEncode<4> for u32
{
    fn from_le_bytes(buffer: &[u8; 4]) -> Self {
		u32::from_le_bytes(*buffer)
    }
    fn to_le_bytes(&self) -> [u8; 4] {
        u32::to_le_bytes(*self)
    }
}
impl ByteEncode<2> for u16
{
    fn from_le_bytes(buffer: &[u8; 2]) -> Self {
		u16::from_le_bytes(*buffer)
    }
    fn to_le_bytes(&self) -> [u8; 2] {
        u16::to_le_bytes(*self)
    }
}
impl ByteEncode<1> for u8
{
    fn from_le_bytes(buffer: &[u8; 1]) -> Self {
		u8::from_le_bytes(*buffer)
    }
    fn to_le_bytes(&self) -> [u8; 1] {
        u8::to_le_bytes(*self)
    }
}