bitcode/ext/
mod.rs

1#[cfg(feature = "arrayvec")]
2mod arrayvec;
3#[cfg(feature = "glam")]
4#[rustfmt::skip] // Makes impl_struct! calls way longer.
5mod glam;
6
7#[allow(unused)]
8macro_rules! impl_struct {
9    ($t:ident, $new:ident, $($f:ident, $ft:ty),+) => {
10        const _: () = {
11            #[derive(Default)]
12            pub struct StructEncoder {
13                $(
14                    $f: <$ft as crate::Encode>::Encoder,
15                )+
16            }
17            impl crate::coder::Encoder<$t> for StructEncoder {
18                #[inline(always)]
19                fn encode(&mut self, t: &$t) {
20                    $(
21                        self.$f.encode(&t.$f);
22                    )+
23                }
24            }
25            impl crate::coder::Buffer for StructEncoder {
26                fn collect_into(&mut self, out: &mut Vec<u8>) {
27                    $(
28                        self.$f.collect_into(out);
29                    )+
30                }
31
32                fn reserve(&mut self, additional: core::num::NonZeroUsize) {
33                    $(
34                        self.$f.reserve(additional);
35                    )+
36                }
37            }
38            impl crate::Encode for $t {
39                type Encoder = StructEncoder;
40            }
41
42            #[derive(Default)]
43            pub struct StructDecoder<'a> {
44                $(
45                    $f: <$ft as crate::Decode<'a>>::Decoder,
46                )+
47            }
48            impl<'a> crate::coder::View<'a> for StructDecoder<'a> {
49                fn populate(&mut self, input: &mut &'a [u8], length: usize) -> crate::coder::Result<()> {
50                    $(
51                        self.$f.populate(input, length)?;
52                    )+
53                    Ok(())
54                }
55            }
56            impl<'a> crate::coder::Decoder<'a, $t> for StructDecoder<'a> {
57                // TODO use decode_in_place instead.
58                #[inline(always)]
59                fn decode(&mut self) -> $t {
60                    $t::$new($(self.$f.decode()),+)
61                }
62            }
63            impl<'a> crate::Decode<'a> for $t {
64                type Decoder = StructDecoder<'a>;
65            }
66        };
67    }
68}
69#[allow(unused)]
70pub(crate) use impl_struct;