From dffc26a466c10c9f1397f6ab6e226f519a1eea5e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 15 Mar 2019 18:33:32 +1100 Subject: [PATCH] Add field idents support to ssz_derive. - Adds idents to skip ser, deser and tree hashing --- eth2/utils/ssz_derive/src/lib.rs | 123 +++++++++++++++++++++++++++---- 1 file changed, 110 insertions(+), 13 deletions(-) diff --git a/eth2/utils/ssz_derive/src/lib.rs b/eth2/utils/ssz_derive/src/lib.rs index a7802a274f..9ba1de4160 100644 --- a/eth2/utils/ssz_derive/src/lib.rs +++ b/eth2/utils/ssz_derive/src/lib.rs @@ -56,10 +56,46 @@ fn get_named_field_idents<'a>(struct_data: &'a syn::DataStruct) -> Vec<&'a syn:: .collect() } +/// Returns a Vec of `syn::Ident` for each named field in the struct, whilst filtering out fields +/// that should not be serialized. +/// +/// # Panics +/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time. +fn get_serializable_named_field_idents<'a>( + struct_data: &'a syn::DataStruct, +) -> Vec<&'a syn::Ident> { + struct_data + .fields + .iter() + .filter_map(|f| { + if should_skip_serializing(&f) { + None + } else { + Some(match &f.ident { + Some(ref ident) => ident, + _ => panic!("ssz_derive only supports named struct fields."), + }) + } + }) + .collect() +} + +/// Returns true if some field has an attribute declaring it should not be serialized. +/// +/// The field attribute is: `#[ssz(skip_serializing)]` +fn should_skip_serializing(field: &syn::Field) -> bool { + for attr in &field.attrs { + if attr.tts.to_string() == "( skip_serializing )" { + return true; + } + } + false +} + /// Implements `ssz::Encodable` for some `struct`. /// /// Fields are encoded in the order they are defined. -#[proc_macro_derive(Encode)] +#[proc_macro_derive(Encode, attributes(ssz))] pub fn ssz_encode_derive(input: TokenStream) -> TokenStream { let item = parse_macro_input!(input as DeriveInput); @@ -70,7 +106,7 @@ pub fn ssz_encode_derive(input: TokenStream) -> TokenStream { _ => panic!("ssz_derive only supports structs."), }; - let field_idents = get_named_field_idents(&struct_data); + let field_idents = get_serializable_named_field_idents(&struct_data); let output = quote! { impl ssz::Encodable for #name { @@ -84,6 +120,18 @@ pub fn ssz_encode_derive(input: TokenStream) -> TokenStream { output.into() } +/// Returns true if some field has an attribute declaring it should not be deserialized. +/// +/// The field attribute is: `#[ssz(skip_deserializing)]` +fn should_skip_deserializing(field: &syn::Field) -> bool { + for attr in &field.attrs { + if attr.tts.to_string() == "( skip_deserializing )" { + return true; + } + } + false +} + /// Implements `ssz::Decodable` for some `struct`. /// /// Fields are decoded in the order they are defined. @@ -98,26 +146,39 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream { _ => panic!("ssz_derive only supports structs."), }; - let field_idents = get_named_field_idents(&struct_data); + let all_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; + // Build quotes for fields that should be deserialized and those that should be built from + // `Default`. + let mut quotes = vec![]; + for field in &struct_data.fields { + match &field.ident { + Some(ref ident) => { + if should_skip_deserializing(field) { + quotes.push(quote! { + let #ident = <_>::default(); + }); + } else { + quotes.push(quote! { + let (#ident, i) = <_>::ssz_decode(bytes, i)?; + }); + } + } + _ => panic!("ssz_derive only supports named struct fields."), + }; + } let output = quote! { impl ssz::Decodable for #name { fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), ssz::DecodeError> { #( - let (#field_idents_a, i) = <_>::ssz_decode(bytes, i)?; + #quotes )* Ok(( Self { #( - #field_idents_b, + #all_idents, )* }, i @@ -128,10 +189,46 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream { output.into() } +/// Returns a Vec of `syn::Ident` for each named field in the struct, whilst filtering out fields +/// that should not be tree hashed. +/// +/// # Panics +/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time. +fn get_tree_hashable_named_field_idents<'a>( + struct_data: &'a syn::DataStruct, +) -> Vec<&'a syn::Ident> { + struct_data + .fields + .iter() + .filter_map(|f| { + if should_skip_tree_hash(&f) { + None + } else { + Some(match &f.ident { + Some(ref ident) => ident, + _ => panic!("ssz_derive only supports named struct fields."), + }) + } + }) + .collect() +} + +/// Returns true if some field has an attribute declaring it should not be tree-hashed. +/// +/// The field attribute is: `#[tree_hash(skip_hashing)]` +fn should_skip_tree_hash(field: &syn::Field) -> bool { + for attr in &field.attrs { + if attr.tts.to_string() == "( skip_hashing )" { + return true; + } + } + false +} + /// Implements `ssz::TreeHash` for some `struct`. /// /// Fields are processed in the order they are defined. -#[proc_macro_derive(TreeHash)] +#[proc_macro_derive(TreeHash, attributes(tree_hash))] pub fn ssz_tree_hash_derive(input: TokenStream) -> TokenStream { let item = parse_macro_input!(input as DeriveInput); @@ -142,7 +239,7 @@ pub fn ssz_tree_hash_derive(input: TokenStream) -> TokenStream { _ => panic!("ssz_derive only supports structs."), }; - let field_idents = get_named_field_idents(&struct_data); + let field_idents = get_tree_hashable_named_field_idents(&struct_data); let output = quote! { impl ssz::TreeHash for #name {