sigp / superstruct

Rust library for versioned data types
https://sigp.github.io/superstruct/
Apache License 2.0
65 stars 3 forks source link

Add lifetime bounds to generic params in the generated ref type #12

Closed realbigsean closed 2 years ago

realbigsean commented 2 years ago

Ran into this when working on making blocks generic across transactions. Seems like it wasn't an issue with the EthSpec generic because it has a static lifetime.

realbigsean commented 2 years ago

I spent a bunch of time trying to figure out what the issue was in order to make a test, and realized it was only happening when deriving TreeHash on superstruct's *Ref enums. Using cargo expand I found it was specifically here where we are explicit about the '__superstruct lifetime when I don't think we need to be:

        fn tree_hash_type() -> tree_hash::TreeHashType {
            {
                match (
                    &<&'__superstruct MessageA<T> as tree_hash::TreeHash>::tree_hash_type(),
                    &tree_hash::TreeHashType::Container,
                ) {
                     ...
                }
            };
            tree_hash::TreeHashType::Container
        }

So another solution would be to just update tree_hash_derive here, to something like this:

            let type_expr =  {
                match ty {
                    Type::Reference(ty_ref) => {
                        let ty_ref_elem = ty_ref.elem.as_ref();
                        quote! {
                            <#ty_ref_elem as tree_hash::TreeHash>::tree_hash_type()
                        }
                    },
                    _ => quote! {
                        <#ty as tree_hash::TreeHash>::tree_hash_type()
                    }
                }
            };