use sszb::{Decode, DecodeError, Encode, SszDecoderBuilder, SszEncoder}; #[derive(Debug, PartialEq)] pub struct Foo { a: u16, b: Vec, c: u16, } impl Encode for Foo { fn is_sszb_fixed_len() -> bool { ::is_sszb_fixed_len() && as Encode>::is_sszb_fixed_len() } fn sszb_bytes_len(&self) -> usize { ::sszb_fixed_len() + sszb::BYTES_PER_LENGTH_OFFSET + ::sszb_fixed_len() + self.b.sszb_bytes_len() } fn sszb_append(&self, buf: &mut Vec) { let offset = ::sszb_fixed_len() + as Encode>::sszb_fixed_len() + ::sszb_fixed_len(); let mut encoder = SszEncoder::container(buf, offset); encoder.append(&self.a); encoder.append(&self.b); encoder.append(&self.c); encoder.finalize(); } } impl Decode for Foo { fn is_sszb_fixed_len() -> bool { ::is_sszb_fixed_len() && as Decode>::is_sszb_fixed_len() } fn from_sszb_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 my_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!(my_foo.as_sszb_bytes(), bytes); let decoded_foo = Foo::from_sszb_bytes(&bytes).unwrap(); assert_eq!(my_foo, decoded_foo); }