Skip to content

Commit

Permalink
derive: fix array with a nested preadable type; add test for regressi…
Browse files Browse the repository at this point in the history
…on (#109)
  • Loading branch information
m4b authored Nov 18, 2024
1 parent f711272 commit a5680ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
14 changes: 1 addition & 13 deletions scroll_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ fn impl_field(
let default_ctx = syn::Ident::new("ctx", proc_macro2::Span::call_site()).into_token_stream();
let ctx = custom_ctx.unwrap_or(&default_ctx);
match *ty {
syn::Type::Array(ref array) => match array.len {
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Int(ref int),
..
}) => {
let size = int.base10_parse::<usize>().unwrap();
quote! {
#ident: { let mut __tmp: #ty = [0u8.into(); #size]; src.gread_inout_with(offset, &mut __tmp, #ctx)?; __tmp }
}
}
_ => panic!("Pread derive with bad array constexpr"),
},
syn::Type::Group(ref group) => impl_field(ident, &group.elem, custom_ctx),
_ => {
quote! {
Expand Down Expand Up @@ -114,7 +102,7 @@ fn impl_struct(
syn::GenericParam::Type(ref t) => {
let ident = &t.ident;
quote! {
#ident : ::scroll::ctx::TryFromCtx<'a, ::scroll::Endian> + ::std::convert::From<u8> + ::std::marker::Copy,
#ident : ::scroll::ctx::TryFromCtx<'a, ::scroll::Endian, Error = ::scroll::Error> + ::std::marker::Copy,
::scroll::Error : ::std::convert::From<< #ident as ::scroll::ctx::TryFromCtx<'a, ::scroll::Endian>>::Error>,
< #ident as ::scroll::ctx::TryFromCtx<'a, ::scroll::Endian>>::Error : ::std::convert::From<scroll::Error>
}
Expand Down
15 changes: 14 additions & 1 deletion scroll_derive/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ struct Data8<T, Y> {
ids: [T; 3],
xyz: Y,
}

#[test]
fn test_generics() {
let mut bytes = [0xde, 0xed, 0xef, 0x10, 0x10];
Expand Down Expand Up @@ -269,3 +268,17 @@ fn test_custom_ctx_derive() {
};
assert_eq!(data3, bytes.ioread_with(LE).unwrap());
}

#[derive(Debug, Pread, SizeWith)]
struct Data12 {
ids: [Data11; 1],
}

#[test]
fn test_array_with_nested_pread_data() {
let bytes = [0xde, 0xed, 0xef, 0x10, 0x10, 0x01];
let data: Data12 = bytes.pread_with(0, LE).unwrap();
assert_eq!(data.ids[0].a, 0xedde);
assert_eq!(data.ids[0].b, 0x10ef);
assert_eq!(data.ids[0].c, 0x1001);
}

0 comments on commit a5680ac

Please sign in to comment.