use ssz::{Decodable, DecodeError, Encodable, SszDecoderBuilder, SszEncoder}; #[derive(Debug, PartialEq)] pub struct Foo { a: u16, b: Vec, c: u16, } impl Encodable for Foo { fn is_ssz_fixed_len() -> bool { ::is_ssz_fixed_len() && as Encodable>::is_ssz_fixed_len() } fn ssz_append(&self, buf: &mut Vec) { let offset = ::ssz_fixed_len() + as Encodable>::ssz_fixed_len() + ::ssz_fixed_len(); let mut encoder = SszEncoder::container(offset); encoder.append(&self.a); encoder.append(&self.b); encoder.append(&self.c); encoder.drain_onto(buf); } } impl Decodable for Foo { fn is_ssz_fixed_len() -> bool { ::is_ssz_fixed_len() && as Decodable>::is_ssz_fixed_len() } fn from_ssz_bytes(bytes: &[u8]) -> Result { let mut builder = SszDecoderBuilder::new(bytes); builder.register_type::()?; builder.register_type::>()?; builder.register_type::()?; let mut decoder = builder.build()?; Ok(Self { a: decoder.decode_next()?, b: decoder.decode_next()?, c: decoder.decode_next()?, }) } } fn main() { let foo = Foo { a: 42, b: vec![0, 1, 2, 3], c: 11, }; let bytes = vec![42, 0, 8, 0, 0, 0, 11, 0, 0, 1, 2, 3]; assert_eq!(foo.as_ssz_bytes(), bytes); let decoded_foo = Foo::from_ssz_bytes(&bytes).unwrap(); assert_eq!(foo, decoded_foo); }