|  | 
|  | 1 | +#![recursion_limit = "128"] | 
|  | 2 | + | 
|  | 3 | +extern crate proc_macro; | 
|  | 4 | +extern crate syn; | 
|  | 5 | +#[macro_use] | 
|  | 6 | +extern crate quote; | 
|  | 7 | + | 
|  | 8 | +#[proc_macro_derive(VertexAttribPointers, attributes(location, divisor))] | 
|  | 9 | +pub fn vertex_attrib_pointers_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | 
|  | 10 | +    let s = input.to_string(); | 
|  | 11 | +    let ast = syn::parse_derive_input(&s).unwrap(); | 
|  | 12 | +    let gen = generate_impl(&ast); | 
|  | 13 | +    gen.parse().unwrap() | 
|  | 14 | +} | 
|  | 15 | + | 
|  | 16 | +fn generate_impl(ast: &syn::DeriveInput) -> quote::Tokens { | 
|  | 17 | +    let ident = &ast.ident; | 
|  | 18 | +    let generics = &ast.generics; | 
|  | 19 | +    let where_clause = &ast.generics.where_clause; | 
|  | 20 | +    let fields_vertex_attrib_pointer = generate_vertex_attrib_pointer_calls(&ast.body); | 
|  | 21 | + | 
|  | 22 | +    quote!{ | 
|  | 23 | +        impl #ident #generics #where_clause { | 
|  | 24 | +            #[allow(unused_variables)] | 
|  | 25 | +            pub fn vertex_attrib_pointers(gl: &::gl::Gl) { | 
|  | 26 | +                let stride = ::std::mem::size_of::<Self>(); | 
|  | 27 | +                let offset = 0; | 
|  | 28 | + | 
|  | 29 | +                #(#fields_vertex_attrib_pointer)* | 
|  | 30 | +            } | 
|  | 31 | +        } | 
|  | 32 | +    } | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +fn generate_vertex_attrib_pointer_calls(body: &syn::Body) -> Vec<quote::Tokens> { | 
|  | 36 | +    match body { | 
|  | 37 | +        &syn::Body::Enum(_) => panic!("VertexAttribPointers can not be implemented for enums"), | 
|  | 38 | +        &syn::Body::Struct(syn::VariantData::Unit) => { | 
|  | 39 | +            panic!("VertexAttribPointers can not be implemented for Unit structs") | 
|  | 40 | +        } | 
|  | 41 | +        &syn::Body::Struct(syn::VariantData::Tuple(_)) => { | 
|  | 42 | +            panic!("VertexAttribPointers can not be implemented for Tuple structs") | 
|  | 43 | +        } | 
|  | 44 | +        &syn::Body::Struct(syn::VariantData::Struct(ref s)) => s | 
|  | 45 | +            .iter() | 
|  | 46 | +            .map(generate_struct_field_vertex_attrib_pointer_call) | 
|  | 47 | +            .collect(), | 
|  | 48 | +    } | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +fn generate_struct_field_vertex_attrib_pointer_call(field: &syn::Field) -> quote::Tokens { | 
|  | 52 | +    let field_name = match field.ident { | 
|  | 53 | +        Some(ref i) => format!("{}", i), | 
|  | 54 | +        None => String::from(""), | 
|  | 55 | +    }; | 
|  | 56 | +    let field_ty = &field.ty; | 
|  | 57 | + | 
|  | 58 | +    if let Some(location_attr) = field | 
|  | 59 | +        .attrs | 
|  | 60 | +        .iter() | 
|  | 61 | +        .filter(|a| a.value.name() == "location") | 
|  | 62 | +        .next() | 
|  | 63 | +    { | 
|  | 64 | +        let location_value: usize = match location_attr.value { | 
|  | 65 | +            syn::MetaItem::NameValue(_, syn::Lit::Str(ref s, _)) => { | 
|  | 66 | +                s.parse().unwrap_or_else(|_| { | 
|  | 67 | +                    panic!( | 
|  | 68 | +                        "Field {} location attribute value must contain an integer", | 
|  | 69 | +                        field_name | 
|  | 70 | +                    ) | 
|  | 71 | +                }) | 
|  | 72 | +            } | 
|  | 73 | +            _ => panic!( | 
|  | 74 | +                "Field {} location attribute value must be a string literal", | 
|  | 75 | +                field_name | 
|  | 76 | +            ), | 
|  | 77 | +        }; | 
|  | 78 | + | 
|  | 79 | +        let divisor_call = match field | 
|  | 80 | +            .attrs | 
|  | 81 | +            .iter() | 
|  | 82 | +            .filter(|a| a.value.name() == "divisor") | 
|  | 83 | +            .next() | 
|  | 84 | +        { | 
|  | 85 | +            Some(attr) => { | 
|  | 86 | +                let divisor_value: u32 = match attr.value { | 
|  | 87 | +                    syn::MetaItem::NameValue(_, syn::Lit::Str(ref s, _)) => { | 
|  | 88 | +                        s.parse().unwrap_or_else(|_| { | 
|  | 89 | +                            panic!( | 
|  | 90 | +                                "Field {} divisor attribute value must contain an integer", | 
|  | 91 | +                                field_name | 
|  | 92 | +                            ) | 
|  | 93 | +                        }) | 
|  | 94 | +                    } | 
|  | 95 | +                    _ => panic!( | 
|  | 96 | +                        "Field {} divisor attribute value must be a string literal", | 
|  | 97 | +                        field_name | 
|  | 98 | +                    ), | 
|  | 99 | +                }; | 
|  | 100 | + | 
|  | 101 | +                quote! { | 
|  | 102 | +                    gl.VertexAttribDivisor(#location_value as u32, #divisor_value); | 
|  | 103 | +                } | 
|  | 104 | +            } | 
|  | 105 | +            None => quote!{}, | 
|  | 106 | +        }; | 
|  | 107 | + | 
|  | 108 | +        quote! { | 
|  | 109 | +            let location = #location_value; | 
|  | 110 | +            unsafe { | 
|  | 111 | +                #field_ty::vertex_attrib_pointer(gl, stride, location, offset); | 
|  | 112 | +                #divisor_call | 
|  | 113 | +            } | 
|  | 114 | +            let offset = offset + ::std::mem::size_of::<#field_ty>(); | 
|  | 115 | +        } | 
|  | 116 | +    } else { | 
|  | 117 | +        quote! { | 
|  | 118 | +            let offset = offset + ::std::mem::size_of::<#field_ty>(); | 
|  | 119 | +        } | 
|  | 120 | +    } | 
|  | 121 | +} | 
0 commit comments