jppe/encode/
impls_tuple.rs

1use crate::std::*;
2
3macro_rules! impls_tuple {
4    ($($t:ident),+) => {
5        #[allow(non_camel_case_types)]
6        impl<$($t: crate::ByteEncode,)+> crate::ByteEncode for ($($t,)+)
7        {
8            fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>)
9            {
10                let ($($t,)*) = self;
11                $(
12                    $t.encode(input, cattr, fattr);
13                )*        
14            }
15        }
16
17
18        #[allow(non_camel_case_types)]
19        impl<$($t: crate::BorrowByteEncode,)+> crate::BorrowByteEncode for ($($t,)+)
20        {
21            fn encode(&self, input: &mut Vec<u8>, cattr: Option<&crate::ContainerAttrModifiers>, fattr: Option<&crate::FieldAttrModifiers>)
22            {
23                let ($($t,)*) = self;
24                $(
25                    $t.encode(input, cattr, fattr);
26                )*        
27            }
28        }
29    };
30
31    () => {
32        impls_tuple!(t1);
33        impls_tuple!(t1, t2);
34        impls_tuple!(t1, t2, t3);
35        impls_tuple!(t1, t2, t3, t4);
36        impls_tuple!(t1, t2, t3, t4, t5);
37        impls_tuple!(t1, t2, t3, t4, t5, t6);
38        impls_tuple!(t1, t2, t3, t4, t5, t6, t7);
39        impls_tuple!(t1, t2, t3, t4, t5, t6, t7, t8);
40        impls_tuple!(t1, t2, t3, t4, t5, t6, t7, t8, t9);
41    };
42}
43
44
45impls_tuple!();
46
47
48#[cfg(test)]
49mod tests {
50    use crate::{encode::ByteEncode, FieldAttrModifiers, ByteOrder};
51
52    #[test]
53    fn test_tuple_encode() {
54        let value = (1 as u16, 2 as u16);
55
56        let mut buf = vec![];
57        value.encode(&mut buf, None, None);
58        assert_eq!(buf, vec![0x00, 0x01, 0x00, 0x02]);
59
60        let fattr = FieldAttrModifiers { byteorder: Some(ByteOrder::Le), ..Default::default() };
61        let mut buf = vec![];
62        value.encode(&mut buf, None, Some(&fattr));
63        assert_eq!(buf, vec![0x01, 0x00, 0x02, 0x00]);
64
65        let value = (1 as u8, 2 as u16);
66
67        let mut buf = vec![];
68        value.encode(&mut buf, None, None);
69        assert_eq!(buf, vec![0x01, 0x00, 0x02]);
70    }
71}