We have added compatible union support to ssz and tree_hash in these PRs:
However these is no verification or validation that variants in a compatible union are actually compatible. The rules for this are clearly defined in the spec, but implementing them will require a bit of cleverness.
Some complications:
- We would ideally like to check these rules at compile-time, but derive macros are purely syntactic and lack proper type information or the ability to evaluate type predicates or trait-methods at compile-time.
- We need something that works for both SSZ and tree_hash, preferably without duplication of logic/effort.
One approach might be to generate const_assert statements from the derive macro(s) which will fail if incompatible types are used. These assertions could use traits if we are able to encode the rules in the trait system, e.g. something like:
trait CompatibleWith<Other> {}
// Primitive types.
impl CompatibleWith<u8> for u8 {}
// etc
// Implemented per crate where relevant
impl<T, U, N> CompatibleWith<List<U, N>> for List<T, N> where T: CompatibleWith<U> {}
One complication is that then there probably needs to be a derive macro for CompatibleWith itself, so that it can be implemented for structs.
We have added compatible union support to
sszandtree_hashin these PRs:However these is no verification or validation that variants in a compatible union are actually compatible. The rules for this are clearly defined in the spec, but implementing them will require a bit of cleverness.
Some complications:
One approach might be to generate
const_assertstatements from the derive macro(s) which will fail if incompatible types are used. These assertions could use traits if we are able to encode the rules in the trait system, e.g. something like:One complication is that then there probably needs to be a derive macro for
CompatibleWithitself, so that it can be implemented for structs.