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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#![recursion_limit = "128"]

/*!
Derive `trait NpyRecord` for a structure.

Using this crate, it is enough to `#[derive(NpyRecord)]` on a struct to be able to serialize and
deserialize it. All the fields must implement [`Serializable`](../npy/trait.Serializable.html).

*/

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use syn::Body;
use quote::{Tokens, ToTokens};

/// Macros 1.1-based custom derive function
#[proc_macro_derive(NpyRecord)]
pub fn npy_data(input: TokenStream) -> TokenStream {
    // Construct a string representation of the type definition
    let s = input.to_string();

    // Parse the string representation
    let ast = syn::parse_macro_input(&s).unwrap();

    // Build the impl
    let expanded = impl_npy_data(&ast);

    // Return the generated impl
    expanded.parse().unwrap()
}

fn impl_npy_data(ast: &syn::DeriveInput) -> quote::Tokens {
    let name = &ast.ident;
    let fields = match ast.body {
        Body::Enum(_) => panic!("#[derive(NpyRecord)] can only be used with structs"),
        Body::Struct(ref data) => data.fields(),
    };
    // Helper is provided for handling complex generic types correctly and effortlessly
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();

    let idents = fields.iter().map(|f| {
        let mut t = Tokens::new();
        f.ident.clone().expect("Tuple structs not supported").to_tokens(&mut t);
        t
    }).collect::<Vec<_>>();
    let types = fields.iter().map(|f|  {
        let mut t = Tokens::new();
        f.ty.to_tokens(&mut t);
        t
    }).collect::<Vec<_>>();

    let idents_c = idents.clone();
    let idents_str = idents.clone().into_iter().map(|t| t.to_string()).collect::<Vec<_>>();
    let idents_str_c1 = idents_str.clone();
    let types_c1 = types.clone();
    let types_c2 = types.clone();
    let types_c3 = types.clone();

    quote! {
        impl #impl_generics ::npy::NpyRecord for #name #ty_generics #where_clause {
            fn get_dtype() -> Vec<(&'static str, ::npy::DType)> {
                vec![#( {
                    (#idents_str_c1, <#types_c1 as ::npy::Serializable>::dtype())
                } ),*]
            }

            fn n_bytes() -> usize {
                #( <#types_c2 as ::npy::Serializable>::n_bytes() )+*
            }

            #[allow(unused_assignments)]
            fn read(buf: &[u8]) -> Self {
                let mut offset = 0;
                #name { #(
                    #idents: {
                        let x = ::npy::Serializable::read(&buf[offset..]);
                        offset += <#types_c3 as ::npy::Serializable>::n_bytes();
                        x
                    }
                ),* }
            }

            fn write<W: ::std::io::Write>(&self, writer: &mut W) -> ::std::io::Result<()> {
                #( ::npy::Serializable::write(&self.#idents_c, writer)?; )*
                Ok(())
            }
        }
    }
}