now using the Hashtree macro for most struct types

This commit is contained in:
mjkeating
2019-02-22 13:07:04 -08:00
parent e9f4cc134e
commit f95a0134e6
26 changed files with 110 additions and 363 deletions

View File

@@ -2,6 +2,7 @@
//!
//! - `#[derive(Encode)]`
//! - `#[derive(Decode)]`
//! - `#[derive(Hashtree)]`
//!
//! These macros provide SSZ encoding/decoding for a `struct`. Fields are encoded/decoded in the
//! order they are defined.
@@ -126,3 +127,34 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
};
output.into()
}
/// Implements `ssz::TreeHash` for some `struct`.
///
/// Fields are processed in the order they are defined.
#[proc_macro_derive(Hashtree)]
pub fn ssz_hashtree_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 ssz::TreeHash for #name {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
#(
result.append(&mut self.#field_idents.hash_tree_root_internal());
)*
ssz::hash(&result)
}
}
};
output.into()
}