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
use crate::{io::*, *};

macro_rules! impl_num {
    ($($ty:ty, )*) => {
        $(
            impl Fixed for $ty {
                fn size() -> u64 {
                    std::mem::size_of::<Self>() as u64
                }
            }

            impl Decode for $ty {
                fn decode<R: Read + Seek + IO>(reader: &mut R) -> Result<Self, R::Error> {
                    let mut raw = [0; std::mem::size_of::<Self>()];
                    reader.read_exact(&mut raw)?;

                    Ok(<$ty>::from_le_bytes(raw))
                }
            }

            impl Encode for $ty {
                fn encode<W: Write + Seek + IO>(&self, writer: &mut W) -> Result<(), W::Error> {
                    writer.write_all(&mut self.to_le_bytes())
                }
            }
        )*
    };
}

impl_num! { u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, }

macro_rules! impl_tuple {
    ($head:ident, ) => {

    };
    ($head:ident, $($tail:ident, )*) => {
        impl_tuple! { $($tail, )* }

        impl<$head: Fixed, $($tail: Fixed, )*> Fixed for ($head, $($tail, )*) {
            fn size() -> u64 {
                $head::size() $( + $tail::size())*
            }
        }

        impl<$head: Decode + Fixed, $($tail: Decode + Fixed, )*> Decode for ($head, $($tail, )*) {
            fn decode<R: Read + Seek + IO>(reader: &mut R) -> Result<Self, R::Error> {
                Ok((
                    $head::decode(reader)?,
                    $(
                        $tail::decode(reader)?,
                    )*
                ))
            }
        }

        #[allow(non_snake_case)]
        impl<$head: Encode + Fixed, $($tail: Encode + Fixed, )*> Encode for ($head, $($tail, )*) {
            fn encode<W: Write + Seek + IO>(&self, writer: &mut W) -> Result<(), W::Error> {
                let (
                    $head,
                    $(
                        $tail,
                    )*
                ) = self;

                $head.encode(writer)?;

                $(
                    $tail.encode(writer)?;
                )*

                Ok(())
            }
        }
    };
}

impl_tuple! { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, }