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

scroll_derive: support reference #101

merged 1 commit into from
Aug 26, 2024

Conversation

Grvzard
Copy link
Contributor

@Grvzard Grvzard commented Aug 13, 2024

Closes #83

Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

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

My only question is about the () ctx; otherwise this looks great, thank you for looking into this and providing what appears to be a fix for this bug!

and thank you for adding the test!

@@ -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

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 :)

},
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!

scroll_derive/Cargo.toml Show resolved Hide resolved
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.

Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

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

thank you so much for fixing this!

@m4b m4b merged commit a7db2f2 into m4b:master Aug 26, 2024
6 checks passed
@Grvzard Grvzard deleted the feat branch August 26, 2024 07:37
@m4b
Copy link
Owner

m4b commented Aug 26, 2024

Somewhat related, and wondering out loud, I hope there aren't similar bugs in the Cread derive code, as I think it is probably large copy pasted from the Pread derive code? anyway, great work and thanks for this fix, do you want a minor patch release for scroll_derive? if it's urgent i can release it now

@Grvzard
Copy link
Contributor Author

Grvzard commented Aug 26, 2024

in the Cread derive code

i can investigate more and propose a PR later.

do you want a minor patch release for scroll_derive

nope, it's okay. thanks though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pwrite deriving fails with "unparseable tokens" with struct containing lifetimes?
2 participants