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
macro_rules! impls_tuple {
    ($($t:ident),+) => {
        #[allow(non_camel_case_types)]
        impl<$($t: crate::ByteEncode,)+> crate::ByteEncode for ($($t,)+)
        {
            fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>)
            {
                let ($($t,)*) = self;
                $(
                    $t.encode(input, cattr, fattr);
                )*        
            }
        }


        #[allow(non_camel_case_types)]
        impl<$($t: crate::BorrowByteEncode,)+> crate::BorrowByteEncode for ($($t,)+)
        {
            fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>)
            {
                let ($($t,)*) = self;
                $(
                    $t.encode(input, cattr, fattr);
                )*        
            }
        }
    };

    () => {
        impls_tuple!(t1);
        impls_tuple!(t1, t2);
        impls_tuple!(t1, t2, t3);
        impls_tuple!(t1, t2, t3, t4);
        impls_tuple!(t1, t2, t3, t4, t5);
        impls_tuple!(t1, t2, t3, t4, t5, t6);
        impls_tuple!(t1, t2, t3, t4, t5, t6, t7);
        impls_tuple!(t1, t2, t3, t4, t5, t6, t7, t8);
        impls_tuple!(t1, t2, t3, t4, t5, t6, t7, t8, t9);
    };
}


impls_tuple!();


#[cfg(test)]
mod tests {
    use crate::{encode::ByteEncode, FieldAttrModifiers, ByteOrder};

    #[test]
    fn test_tuple_encode() {
        let value = (1 as u16, 2 as u16);

        let mut buf = vec![];
        value.encode(&mut buf, None, None);
        assert_eq!(buf, vec![0x00, 0x01, 0x00, 0x02]);

        let fattr = FieldAttrModifiers { byteorder: Some(ByteOrder::Le), ..Default::default() };
        let mut buf = vec![];
        value.encode(&mut buf, None, Some(&fattr));
        assert_eq!(buf, vec![0x01, 0x00, 0x02, 0x00]);

        let value = (1 as u8, 2 as u16);

        let mut buf = vec![];
        value.encode(&mut buf, None, None);
        assert_eq!(buf, vec![0x01, 0x00, 0x02]);
    }
}