From a5680ac5f04a34a8f8abe23c738d200b73e32035 Mon Sep 17 00:00:00 2001 From: m4b Date: Sun, 17 Nov 2024 22:12:40 -0800 Subject: [PATCH] derive: fix array with a nested preadable type; add test for regression (#109) --- scroll_derive/src/lib.rs | 14 +------------- scroll_derive/tests/tests.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/scroll_derive/src/lib.rs b/scroll_derive/src/lib.rs index a5714dd..11bc081 100644 --- a/scroll_derive/src/lib.rs +++ b/scroll_derive/src/lib.rs @@ -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::().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! { @@ -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 + ::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 } diff --git a/scroll_derive/tests/tests.rs b/scroll_derive/tests/tests.rs index 14dec4e..d9a781e 100644 --- a/scroll_derive/tests/tests.rs +++ b/scroll_derive/tests/tests.rs @@ -180,7 +180,6 @@ struct Data8 { ids: [T; 3], xyz: Y, } - #[test] fn test_generics() { let mut bytes = [0xde, 0xed, 0xef, 0x10, 0x10]; @@ -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); +}