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
extern crate syn;
#[macro_use]
extern crate synstructure;
#[macro_use]
extern crate quote;

decl_derive!([Abomonation] => derive_abomonation);

fn derive_abomonation(mut s: synstructure::Structure) -> quote::Tokens {
    let entomb = s.each(|bi| quote! {
        ::abomonation::Abomonation::entomb(#bi, _writer);
    });

    s.bind_with(|_| synstructure::BindStyle::RefMut);
    let embalm = s.each(|bi| quote! {
        ::abomonation::Abomonation::embalm(#bi);
    });

    let exhume = s.each(|bi| quote! {
        let temp = bytes;
        let exhume_result = ::abomonation::Abomonation::exhume(#bi, temp);
        bytes = if let Some(bytes) = exhume_result {
            bytes
        } else {
            return None
        };
    });

    s.bound_impl("::abomonation::Abomonation", quote! {
        #[inline] unsafe fn entomb(&self, _writer: &mut Vec<u8>) {
            match *self { #entomb }
        }
        #[inline] unsafe fn embalm(&mut self) {
            match *self { #embalm }
        }
        #[allow(unused_mut)]
        #[inline] unsafe fn exhume<'a,'b>(
            &'a mut self,
            mut bytes: &'b mut [u8]
        ) -> Option<&'b mut [u8]> {
            match *self { #exhume }
            Some(bytes)
        }
    })
}