bin_proto/impls/
tuple.rs

1use bitstream_io::{BitRead, BitWrite, Endianness};
2
3use crate::{BitDecode, BitEncode, Result};
4
5macro_rules! impl_tuple {
6    ($($idx:tt $t:ident),*) => {
7        #[cfg_attr(docsrs, doc(hidden))]
8        impl<Ctx, $($t,)*> $crate::BitDecode<Ctx> for ($($t,)*)
9        where
10            $($t: $crate::BitDecode<Ctx>,)*
11        {
12            fn decode<R, E>(
13                read: &mut R,
14                ctx: &mut Ctx,
15                (): (),
16            ) -> $crate::Result<Self>
17            where
18                R: ::bitstream_io::BitRead,
19                E: ::bitstream_io::Endianness,
20            {
21                Ok(($(<$t as $crate::BitDecode<Ctx>>::decode::<_, E>(read,  ctx, ())?,)*))
22            }
23        }
24
25        #[cfg_attr(docsrs, doc(hidden))]
26        impl<Ctx, $($t,)*> $crate::BitEncode<Ctx> for ($($t,)*)
27        where
28            $($t: $crate::BitEncode<Ctx>,)*
29        {
30            fn encode<W, E>(
31                &self,
32                write: &mut W,
33                ctx: &mut Ctx,
34                (): ()
35            ) -> $crate::Result<()>
36            where
37                W: ::bitstream_io::BitWrite,
38                E: ::bitstream_io::Endianness,
39            {
40                $(
41                    $crate::BitEncode::encode::<_, E>(&self.$idx, write,  ctx, ())?;
42                )*
43                Ok(())
44            }
45        }
46    };
47}
48
49#[cfg_attr(docsrs, doc(fake_variadic))]
50#[cfg_attr(
51    docsrs,
52    doc = "This trait is implemented for tuples with up to 16 items."
53)]
54impl<Ctx, Tag, T> BitDecode<Ctx, Tag> for (T,)
55where
56    T: BitDecode<Ctx, Tag>,
57{
58    fn decode<R, E>(read: &mut R, ctx: &mut Ctx, tag: Tag) -> Result<Self>
59    where
60        R: BitRead,
61        E: Endianness,
62    {
63        Ok((BitDecode::decode::<R, E>(read, ctx, tag)?,))
64    }
65}
66
67#[cfg_attr(
68    docsrs,
69    doc = "This trait is implemented for tuples with up to 16 items."
70)]
71#[cfg_attr(docsrs, doc(fake_variadic))]
72impl<Ctx, Tag, T> BitEncode<Ctx, Tag> for (T,)
73where
74    T: BitEncode<Ctx, Tag>,
75{
76    fn encode<W, E>(&self, write: &mut W, ctx: &mut Ctx, tag: Tag) -> Result<()>
77    where
78        W: BitWrite,
79        E: Endianness,
80    {
81        self.0.encode::<W, E>(write, ctx, tag)
82    }
83}
84
85impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7, 8 T8, 9 T9, 10 T10, 11 T11, 12 T12, 13 T13, 14 T14, 15 T15);
86impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7, 8 T8, 9 T9, 10 T10, 11 T11, 12 T12, 13 T13, 14 T14);
87impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7, 8 T8, 9 T9, 10 T10, 11 T11, 12 T12, 13 T13);
88impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7, 8 T8, 9 T9, 10 T10, 11 T11, 12 T12);
89impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7, 8 T8, 9 T9, 10 T10, 11 T11);
90impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7, 8 T8, 9 T9, 10 T10);
91impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7, 8 T8, 9 T9);
92impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7, 8 T8);
93impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6, 7 T7);
94impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5, 6 T6);
95impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4, 5 T5);
96impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3, 4 T4);
97impl_tuple!(0 T0, 1 T1, 2 T2, 3 T3);
98impl_tuple!(0 T0, 1 T1, 2 T2);
99impl_tuple!(0 T0, 1 T1);
100
101test_codec!((u8,); (1,) => [0x01]);
102test_roundtrip!((u8,));