Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scroll_derive: support reference #101

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scroll_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ quote = "1"
syn = "2"

[dev-dependencies.scroll]
version = "0.11"
version = "0.12"
Grvzard marked this conversation as resolved.
Show resolved Hide resolved
path = ".."
36 changes: 25 additions & 11 deletions scroll_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ fn impl_pwrite_field(ident: &proc_macro2::TokenStream, ty: &syn::Type) -> proc_m
_ => panic!("Pwrite derive with bad array constexpr"),
},
syn::Type::Group(group) => impl_pwrite_field(ident, &group.elem),
syn::Type::Reference(reference) => match *reference.elem {
syn::Type::Slice(_) => quote! {
dst.gwrite_with(self.#ident, offset, ())?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is a () ctx passed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refer

scroll/src/ctx.rs

Lines 713 to 716 in 3cffc5a

impl<'a> TryIntoCtx for &'a [u8] {
type Error = error::Error;
#[inline]
fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result<usize> {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅 haha yes i forgot pwrite for &[u8] has no ctx, though that is a bit quirky/assymetric, as it's pread has a usize ctx (e.g., how many things to read). r

},
_ => quote! {
dst.gwrite_with(self.#ident, offset, ctx)?
},
},
_ => {
quote! {
dst.gwrite_with(&self.#ident, offset, ctx)?
Expand Down Expand Up @@ -166,32 +174,38 @@ fn impl_try_into_ctx(
});
let gn = quote! { #gl #( #gn ),* #gg };
let gwref = if !gp.is_empty() {
let gi = gp.iter().map(|param: &syn::GenericParam| match param {
let gi: Vec<_> = gp.iter().filter_map(|param: &syn::GenericParam| match param {
syn::GenericParam::Type(ref t) => {
let ident = &t.ident;
quote! {
Some(quote! {
&'a #ident : ::scroll::ctx::TryIntoCtx<::scroll::Endian>,
::scroll::Error: ::std::convert::From<<&'a #ident as ::scroll::ctx::TryIntoCtx<::scroll::Endian>>::Error>,
<&'a #ident as ::scroll::ctx::TryIntoCtx<::scroll::Endian>>::Error: ::std::convert::From<scroll::Error>
}
})
},
p => quote! { #p }
});
quote! { where #( #gi ),* }
syn::GenericParam::Lifetime(_) => None,
p => Some(quote! { #p }),
}).collect();
if !gi.is_empty() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is the heart of the fix, thank you for looking into this and doing it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this one doesn't matter here. a dangling where clause will be ignored by rust. I did more processing here just to make the generated token clearer.

quote! { where #( #gi ),* }
} else {
quote! {}
}
} else {
quote! {}
};
let gw = if !gp.is_empty() {
let gi = gp.iter().map(|param: &syn::GenericParam| match param {
let gi = gp.iter().filter_map(|param: &syn::GenericParam| match param {
syn::GenericParam::Type(ref t) => {
let ident = &t.ident;
quote! {
Some(quote! {
#ident : ::scroll::ctx::TryIntoCtx<::scroll::Endian>,
::scroll::Error: ::std::convert::From<<#ident as ::scroll::ctx::TryIntoCtx<::scroll::Endian>>::Error>,
<#ident as ::scroll::ctx::TryIntoCtx<::scroll::Endian>>::Error: ::std::convert::From<scroll::Error>
}
})
},
p => quote! { #p }
syn::GenericParam::Lifetime(_) => None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not clear to me why this branch is None here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually 'the heart of the fix' is here.
the prevoius code doesn't process lifetime properly, which will generates tokens like

impl<'b> ... for ... 
where
    ... : ... ,
    'b
    ^^ invalid syntax

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see, thank you for clarifying!

p => Some(quote! { #p }),
});
quote! { where Self: ::std::marker::Copy, #( #gi ),* }
} else {
Expand All @@ -205,7 +219,7 @@ fn impl_try_into_ctx(
fn try_into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) -> ::scroll::export::result::Result<usize, Self::Error> {
use ::scroll::Pwrite;
let offset = &mut 0;
#(#items;)*;
#(#items;)*
Ok(*offset)
}
}
Expand Down
21 changes: 21 additions & 0 deletions scroll_derive/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,24 @@ fn test_newtype() {
let written = bytes.pwrite_with(&data, 0, LE).unwrap();
assert_eq!(written, size);
}

#[derive(Debug, PartialEq, Eq, Pread, Pwrite, IOread, IOwrite, SizeWith)]
struct Data10I(u8, u16);

#[derive(Debug, Pwrite)]
struct Data10<'b> {
pub inner: &'b Data10I,
pub name: &'b [u8],
}

#[test]
fn test_reference() {
let inner = Data10I(255, 1);
let data = Data10 {
inner: &inner,
name: b"name",
};
let mut bytes = vec![0; 32];
assert_eq!(bytes.pwrite_with(&data, 0, LE).unwrap(), 7);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm surprised you can pass a &data reference to pwrite_with here, instead of just data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the derivation of Pwrite will also generates a Pwrite impl for &Data. that's not my credit :)

assert_eq!(bytes[..7], *b"\xff\x01\x00name");
}
Loading