From c01cd7541b616b189466c9ec890bdacba512d627 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 6 Dec 2024 06:27:03 +1100 Subject: [PATCH] Simplify fixed decoding in derive macro --- ssz_derive/src/lib.rs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/ssz_derive/src/lib.rs b/ssz_derive/src/lib.rs index 5e01ea8..22e7543 100644 --- a/ssz_derive/src/lib.rs +++ b/ssz_derive/src/lib.rs @@ -821,20 +821,13 @@ fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Tok } fixed_decodes.push(quote! { - let #ident = { - start = end; - end = end - .checked_add(#ssz_fixed_len) - .ok_or_else(|| ssz::DecodeError::OutOfBoundsByte { - i: usize::max_value() - })?; - let slice = bytes.get(start..end) - .ok_or_else(|| ssz::DecodeError::InvalidByteLength { - len: bytes.len(), - expected: end - })?; - #from_ssz_bytes? - }; + let (slice, bytes) = bytes + .split_at_checked(#ssz_fixed_len) + .ok_or_else(|| ssz::DecodeError::InvalidByteLength { + len: bytes.len(), + expected: #ssz_fixed_len + })?; + let #ident = #from_ssz_bytes?; }); is_fixed_lens.push(is_ssz_fixed_len); fixed_lens.push(ssz_fixed_len); @@ -872,9 +865,6 @@ fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Tok }); } - let mut start: usize = 0; - let mut end = start; - #( #fixed_decodes )*