Skip to content

Commit 27a5a36

Browse files
committed
Use tuple index rather than assuming 0
1 parent 5e40ad1 commit 27a5a36

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

consensus/ssz_derive/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ use darling::{FromDeriveInput, FromMeta};
147147
use proc_macro::TokenStream;
148148
use quote::quote;
149149
use std::convert::TryInto;
150-
use syn::{parse_macro_input, DataEnum, DataStruct, DeriveInput, Ident};
150+
use syn::{parse_macro_input, DataEnum, DataStruct, DeriveInput, Ident, Index};
151151

152152
/// The highest possible union selector value (higher values are reserved for backwards compatible
153153
/// extensions).
@@ -442,11 +442,15 @@ fn ssz_encode_derive_struct_transparent(
442442
);
443443
}
444444

445-
let (ty, ident, _field_opts) = ssz_fields
445+
let (index, (ty, ident, _field_opts)) = ssz_fields
446446
.iter()
447-
.find(|(_, _, field_opts)| !field_opts.skip_deserializing)
447+
.enumerate()
448+
.find(|(_, (_, _, field_opts))| !field_opts.skip_deserializing)
448449
.expect("\"transparent\" struct must have at least one non-skipped field");
449450

451+
// Remove the `_usize` suffix from the value to avoid a compiler warning.
452+
let index = Index::from(index);
453+
450454
let output = if let Some(field_name) = ident {
451455
quote! {
452456
impl #impl_generics ssz::Encode for #name #ty_generics #where_clause {
@@ -479,11 +483,11 @@ fn ssz_encode_derive_struct_transparent(
479483
}
480484

481485
fn ssz_bytes_len(&self) -> usize {
482-
self.0.ssz_bytes_len()
486+
self.#index.ssz_bytes_len()
483487
}
484488

485489
fn ssz_append(&self, buf: &mut Vec<u8>) {
486-
self.0.ssz_append(buf)
490+
self.#index.ssz_append(buf)
487491
}
488492
}
489493
}

consensus/ssz_derive/tests/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,24 @@ fn transparent_struct_newtype_skipped_field() {
213213
&vec![42_u8].as_ssz_bytes(),
214214
);
215215
}
216+
217+
#[derive(PartialEq, Debug, Encode, Decode)]
218+
#[ssz(struct_behaviour = "transparent")]
219+
struct TransparentStructNewTypeSkippedFieldReverse(
220+
#[ssz(skip_serializing, skip_deserializing)] PhantomData<u64>,
221+
Vec<u8>,
222+
);
223+
224+
impl TransparentStructNewTypeSkippedFieldReverse {
225+
fn new(inner: Vec<u8>) -> Self {
226+
Self(PhantomData, inner)
227+
}
228+
}
229+
230+
#[test]
231+
fn transparent_struct_newtype_skipped_field_reverse() {
232+
assert_encode_decode(
233+
&TransparentStructNewTypeSkippedFieldReverse::new(vec![42_u8]),
234+
&vec![42_u8].as_ssz_bytes(),
235+
);
236+
}

0 commit comments

Comments
 (0)