Problem Description and Proof of Concept
In my attempts of learning new concepts in programming, I usually like to re-implement something which has already been done. In this case I’m trying to learn Rust procedual macros by implementing a csv-like parser. However I ran into some problems.
Assuming a struct
struct MyStruct {
_first_field: String,
_second_field: String,
_third_field: String,
}
I would like to use a derive macro
#[derive(PopulateFields)]
struct MyStruct {
...
}
whose goal is to add an impl
like
impl PopulateFields for MyStruct {
fn populate_fields(vec: Vec<String>) -> Self {
Self {
_first_field: vec[0],
_second_field: vec[1],
_third_field: vec[2]
}
}
}
i.e. taking a vector of values and returning a Self
with the values mapped to the fields. I know that this is approximately what e.g. serde
-crate can do, and I know that there is a specific csv
-crate (using serde
). But as part of the learning process, I thought this was a good example to re-implement this feature…
The code below is a proof of concept macro which works, but is not generic at all.
// Proof of concept, with hardcoded indices.
use proc_macro::{TokenStream, Ident, Span};
use quote::quote;
use syn::{parse_macro_input, ItemStruct};
#[proc_macro_derive(PopulateFields)]
pub fn derive_populate_fields(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemStruct);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let output = quote! {
impl #impl_generics PopulateFields for #name #ty_generics #where_clause {
fn populate_fields(vec: Vec<String>) -> Self {
Self {
_first_field: vec[0],
_second_field: vec[1],
_third_field: vec[2]
}
}
}
};
TokenStream::from(output)
}
My problem which the below ideas tried to address is to find a way of making the content of the quote!
more general to e.g. loop over the fields rather than hard-coding the indices for every new struct/csv format.
General Disclaimer
The snippets below are perhaps naively written and without any checks what so ever (e.g. bound checks on accessing the vec
elements). However I prefered to keep the code as simple as I could but still showing the thought process. A more realistic implementation would likely return a Result(Self)
object rather than Self
on its own.
I have also looked at the general proc_macro documentation, and several SO answers but cannot a way of solving my problem.
Idea 1
Inspired by this anwser I managed to get a count of the number of fields of the current struct.
My idea is to just get this field number and loop over the entries of the vec
if the length of vec
is the same as the number of fields. However I cannot find a way of looping within the quote!
part of the proc_macro.
// Idea 1
use proc_macro::{TokenStream, Ident, Span};
use quote::quote;
use syn::{parse_macro_input, ItemStruct};
#[proc_macro_derive(PopulateFields)]
pub fn derive_populate_fields(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemStruct);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let field_count = input.fields.iter().count(); // as by the linked answer
let output = quote! {
impl #impl_generics PopulateFields for #name #ty_generics #where_clause {
fn populate_fields(vec: Vec<String>) -> Self {
Self {
// pseudo-code since I could not get any attempt to work...
for i, field in enumerate(#input.fields) { // coming from a python background...
#field: vec[#i],
}
}
}
}
};
TokenStream::from(output)
}
Idea 2
Another idea was to build the _i_field: vec[i]
-part of the code outside of the quote!
-context and store them in a new vec
, as inspired by this answer. Then inside the quote!
the full Self {"_i_field: vec[i]"-part}
would be built by join
ing the new vec
.
// Idea 2
use proc_macro::{TokenStream, Ident, Span};
use quote::quote;
use syn::{parse_macro_input, ItemStruct};
#[proc_macro_derive(PopulateFields)]
pub fn derive_populate_fields(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemStruct);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let new_fields: Vec<_> = input.fields
.iter()
.map(|field| {
quote! {
#field: vec[#i] // somehow to get the index as well, I just used a hard-coded index in my testing
}
})
.collect();
let output = quote! {
impl #impl_generics PopulateFields for #name #ty_generics #where_clause {
fn populate_fields(vec: Vec<String>) -> Self {
Self {
#(#new_fields)*
}
}
}
};
TokenStream::from(output)
}
Idea 3
The final idea I have so far is to simply build a String
with the correct return-shape and then somehow turn that into code inside the quote!
. This approach works in the sense that the produced string is correctly formatted, but I cannot transfer it “as code” into the quote!
-area (it gets pasted as a String
).
// Idea 3
#[proc_macro_derive(PopulateFields)]
pub fn derive_populate_fields(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ItemStruct);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
// Extract the fields, create a vec of the field names, format them nicely and push to vec.
let fields = input.fields.iter().collect::<Vec<_>>();
let mut field_assigns: Vec<String> = vec![];
let mut idx: usize = 0;
for f in &fields {
let fname: String = f.ident.clone().unwrap().to_string();
field_assigns.push(format!(" {fname}: vec[{idx}]"));
idx += 1;
}
// Join the string and put it into the final string.
let joined: String = field_assigns.join(",n");
let inside: String = "Self {n".to_string() + format!("{}", joined).as_str() + "n}";
println!("{}", inside); // this prints a correctly formatted string
let output = quote! {
impl #impl_generics PopulateFields for #name #ty_generics #where_clause {
fn populate_fields(vec: Vec<String>) -> Self {
// paste code from `inside` here...
}
}
};
TokenStream::from(output)
}