-
Notifications
You must be signed in to change notification settings - Fork 37
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
Conversation
There was a problem hiding this 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, ())? |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refer
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> { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
syn::GenericParam::Lifetime(_) => None, | ||
p => Some(quote! { #p }), | ||
}).collect(); | ||
if !gi.is_empty() { |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
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 |
i can investigate more and propose a PR later.
nope, it's okay. thanks though. |
Closes #83