bitcode/ext/
mod.rs

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
#[cfg(feature = "arrayvec")]
mod arrayvec;
#[cfg(feature = "glam")]
#[rustfmt::skip] // Makes impl_struct! calls way longer.
mod glam;

#[allow(unused)]
macro_rules! impl_struct {
    ($t:ident, $new:ident, $($f:ident, $ft:ty),+) => {
        const _: () = {
            #[derive(Default)]
            pub struct StructEncoder {
                $(
                    $f: <$ft as crate::Encode>::Encoder,
                )+
            }
            impl crate::coder::Encoder<$t> for StructEncoder {
                #[inline(always)]
                fn encode(&mut self, t: &$t) {
                    $(
                        self.$f.encode(&t.$f);
                    )+
                }
            }
            impl crate::coder::Buffer for StructEncoder {
                fn collect_into(&mut self, out: &mut Vec<u8>) {
                    $(
                        self.$f.collect_into(out);
                    )+
                }

                fn reserve(&mut self, additional: core::num::NonZeroUsize) {
                    $(
                        self.$f.reserve(additional);
                    )+
                }
            }
            impl crate::Encode for $t {
                type Encoder = StructEncoder;
            }

            #[derive(Default)]
            pub struct StructDecoder<'a> {
                $(
                    $f: <$ft as crate::Decode<'a>>::Decoder,
                )+
            }
            impl<'a> crate::coder::View<'a> for StructDecoder<'a> {
                fn populate(&mut self, input: &mut &'a [u8], length: usize) -> crate::coder::Result<()> {
                    $(
                        self.$f.populate(input, length)?;
                    )+
                    Ok(())
                }
            }
            impl<'a> crate::coder::Decoder<'a, $t> for StructDecoder<'a> {
                // TODO use decode_in_place instead.
                #[inline(always)]
                fn decode(&mut self) -> $t {
                    $t::$new($(self.$f.decode()),+)
                }
            }
            impl<'a> crate::Decode<'a> for $t {
                type Decoder = StructDecoder<'a>;
            }
        };
    }
}
#[allow(unused)]
pub(crate) use impl_struct;