diff --git a/Cargo.toml b/Cargo.toml index 5ab0ba847..f8b4cfe41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ members = [ "eth2/utils/int_to_bytes", "eth2/utils/slot_clock", "eth2/utils/ssz", + "eth2/utils/ssz_derive", "eth2/utils/swap_or_not_shuffle", "eth2/utils/fisher_yates_shuffle", "beacon_node", diff --git a/eth2/types/Cargo.toml b/eth2/types/Cargo.toml index 4c5be65b5..f51e20236 100644 --- a/eth2/types/Cargo.toml +++ b/eth2/types/Cargo.toml @@ -18,6 +18,7 @@ serde_derive = "1.0" serde_json = "1.0" slog = "^2.2.3" ssz = { path = "../utils/ssz" } +ssz_derive = { path = "../utils/ssz_derive" } swap_or_not_shuffle = { path = "../utils/swap_or_not_shuffle" } [dev-dependencies] diff --git a/eth2/utils/ssz_derive/Cargo.toml b/eth2/utils/ssz_derive/Cargo.toml new file mode 100644 index 000000000..3e58d752b --- /dev/null +++ b/eth2/utils/ssz_derive/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ssz_derive" +version = "0.1.0" +authors = ["Paul Hauner "] +edition = "2018" +description = "Procedural derive macros for SSZ encoding and decoding." + +[lib] +proc-macro = true + +[dependencies] +syn = "0.15" +quote = "0.6" +ssz = { path = "../ssz" } diff --git a/eth2/utils/ssz_derive/src/lib.rs b/eth2/utils/ssz_derive/src/lib.rs new file mode 100644 index 000000000..fdee88dd9 --- /dev/null +++ b/eth2/utils/ssz_derive/src/lib.rs @@ -0,0 +1,80 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, DeriveInput}; + +fn get_named_field_idents<'a>(struct_data: &'a syn::DataStruct) -> Vec<&'a syn::Ident> { + struct_data.fields.iter().map(|f| { + match &f.ident { + Some(ref ident) => ident, + _ => panic!("ssz_derive only supports named struct fields.") + } + }).collect() +} + +#[proc_macro_derive(Encode)] +pub fn ssz_encode_derive(input: TokenStream) -> TokenStream { + let item = parse_macro_input!(input as DeriveInput); + + let name = &item.ident; + + let struct_data = match &item.data { + syn::Data::Struct(s) => s, + _ => panic!("ssz_derive only supports structs.") + }; + + let field_idents = get_named_field_idents(&struct_data); + + let output = quote! { + impl Encodable for #name { + fn ssz_append(&self, s: &mut SszStream) { + #( + s.append(&self.#field_idents); + )* + } + } + }; + output.into() +} + +#[proc_macro_derive(Decode)] +pub fn ssz_decode_derive(input: TokenStream) -> TokenStream { + let item = parse_macro_input!(input as DeriveInput); + + let name = &item.ident; + + let struct_data = match &item.data { + syn::Data::Struct(s) => s, + _ => panic!("ssz_derive only supports structs.") + }; + + let field_idents = get_named_field_idents(&struct_data); + + // Using a var in an iteration always consumes the var, therefore we must make a `fields_a` and + // a `fields_b` in order to perform two loops. + // + // https://github.com/dtolnay/quote/issues/8 + let field_idents_a = &field_idents; + let field_idents_b = &field_idents; + + let output = quote! { + impl Decodable for #name { + fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { + #( + let (#field_idents_a, i) = <_>::ssz_decode(bytes, i)?; + )* + + Ok(( + Self { + #( + #field_idents_b, + )* + }, + i + )) + } + } + }; + output.into() +}