Hello,
I want to make a derive macro that creates an struct of array version of a struct. I'm just having trouble figuring out how to make it nicely work especially with the methods. For instance a get(idx: usize) method would return an instance of the base struct from the values in its SoA version's Vec's at the specified index. I don't know how to go about doing so. Here is my macro:
use proc_macro::TokenStream; use quote::{format_ident, quote}; use syn::{parse_macro_input, DeriveInput, Fields}; #[proc_macro_derive(Soa)] pub fn derive_macro_soa(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); let soa_name = format_ident!("{}Soa", &ast.ident); if let syn::Data::Struct(ref data) = ast.data { if let Fields::Named(ref fields) = data.fields { let built_fields = fields.named.iter().map(|f| { let name = &f.ident; let ty = &f.ty; quote!{ #name: Vec<#ty> } }); return quote!{ pub struct #soa_name { #(#built_fields,)* } }.into() } } TokenStream::from( syn::Error::new( ast.ident.span(), "The Soa macro only works on structs with named fields", ).to_compile_error() ) } Any help is much appreciated
3 posts - 2 participants