From c01cd7541b616b189466c9ec890bdacba512d627 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 6 Dec 2024 06:27:03 +1100 Subject: [PATCH 1/3] 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 )* From cda11f3aab4dc42e3585912dcb7b02cda32c3d82 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 6 Dec 2024 06:38:30 +1100 Subject: [PATCH 2/3] Use `ok_or` --- ssz_derive/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssz_derive/src/lib.rs b/ssz_derive/src/lib.rs index 22e7543..daf35bf 100644 --- a/ssz_derive/src/lib.rs +++ b/ssz_derive/src/lib.rs @@ -823,7 +823,7 @@ fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Tok fixed_decodes.push(quote! { let (slice, bytes) = bytes .split_at_checked(#ssz_fixed_len) - .ok_or_else(|| ssz::DecodeError::InvalidByteLength { + .ok_or(ssz::DecodeError::InvalidByteLength { len: bytes.len(), expected: #ssz_fixed_len })?; From 730dc5834f2a2358ef2a8afb13f0d2fed062daaa Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 6 Dec 2024 06:40:32 +1100 Subject: [PATCH 3/3] Use `split_at` --- ssz_derive/src/lib.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ssz_derive/src/lib.rs b/ssz_derive/src/lib.rs index daf35bf..2e8d934 100644 --- a/ssz_derive/src/lib.rs +++ b/ssz_derive/src/lib.rs @@ -821,12 +821,7 @@ fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Tok } fixed_decodes.push(quote! { - let (slice, bytes) = bytes - .split_at_checked(#ssz_fixed_len) - .ok_or(ssz::DecodeError::InvalidByteLength { - len: bytes.len(), - expected: #ssz_fixed_len - })?; + let (slice, bytes) = bytes.split_at(#ssz_fixed_len); let #ident = #from_ssz_bytes?; }); is_fixed_lens.push(is_ssz_fixed_len);