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

RFC/feat: Add ctx_nested attribute to top level #229

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
20 changes: 14 additions & 6 deletions deku-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn cerror(span: proc_macro2::Span, msg: &str) -> TokenStream {
syn::Error::new(span, msg).to_compile_error()
}

/// A post-processed version of `DekuReceiver`
/// A post-processed version of [DekuReceiver](self::DekuReceiver)
#[derive(Debug)]
struct DekuData {
vis: syn::Visibility,
Expand All @@ -117,6 +117,9 @@ struct DekuData {
/// default context passed to the field
ctx_default: Option<Punctuated<syn::Expr, syn::token::Comma>>,

/// additional context passed down to the fields
ctx_nested: Option<Punctuated<syn::Expr, syn::token::Comma>>,

/// A magic value that must appear at the start of this struct/enum's data
magic: Option<syn::LitByteStr>,

Expand All @@ -134,7 +137,7 @@ struct DekuData {
}

impl DekuData {
/// Map a `DekuReceiver` to `DekuData`
/// Map a [DekuReceiver](self::DekuReceiver) to [DekuData](self::DekuData)
fn from_receiver(receiver: DekuReceiver) -> Result<Self, TokenStream> {
let data = match receiver.data {
ast::Data::Struct(fields) => ast::Data::Struct(ast::Fields::new(
Expand All @@ -161,6 +164,7 @@ impl DekuData {
endian: receiver.endian,
ctx: receiver.ctx,
ctx_default: receiver.ctx_default,
ctx_nested: receiver.ctx_nested,
magic: receiver.magic,
id: receiver.id,
id_type: receiver.id_type?,
Expand Down Expand Up @@ -265,7 +269,7 @@ impl DekuData {
}
}

/// Common variables from `DekuData` for `emit_enum` read/write functions
/// Common variables from [DekuData](self::DekuData) for `emit_enum` read/write functions
#[derive(Debug)]
struct DekuDataEnum<'a> {
imp: syn::ImplGenerics<'a>,
Expand Down Expand Up @@ -311,7 +315,7 @@ impl<'a> TryFrom<&'a DekuData> for DekuDataEnum<'a> {
}
}

/// Common variables from `DekuData` for `emit_struct` read/write functions
/// Common variables from [DekuData](self::DekuData) for `emit_struct` read/write functions
#[derive(Debug)]
struct DekuDataStruct<'a> {
imp: syn::ImplGenerics<'a>,
Expand Down Expand Up @@ -342,7 +346,7 @@ impl<'a> TryFrom<&'a DekuData> for DekuDataStruct<'a> {
}
}

/// A post-processed version of `FieldReceiver`
/// A post-processed version of [FieldReceiver](self::FieldReceiver)
#[derive(Debug)]
struct FieldData {
ident: Option<syn::Ident>,
Expand Down Expand Up @@ -511,7 +515,7 @@ impl FieldData {
}
}

/// A post-processed version of `VariantReceiver`
/// A post-processed version of [VariantReceiver](self::VariantReceiver)
#[derive(Debug)]
struct VariantData {
ident: syn::Ident,
Expand Down Expand Up @@ -601,6 +605,10 @@ struct DekuReceiver {
#[darling(default)]
ctx_default: Option<syn::punctuated::Punctuated<syn::Expr, syn::token::Comma>>,

/// additional context passed down to the fields
#[darling(default)]
ctx_nested: Option<syn::punctuated::Punctuated<syn::Expr, syn::token::Comma>>,

/// A magic value that must appear at the start of this struct/enum's data
#[darling(default)]
magic: Option<syn::LitByteStr>,
Expand Down
1 change: 1 addition & 0 deletions deku-derive/src/macros/deku_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ fn emit_field_read(
f.bits.as_ref(),
f.bytes.as_ref(),
f.ctx.as_ref(),
input.ctx_nested.as_ref(),
)?;

// The container limiting options are special, we need to generate `(limit, (other, ..))` for them.
Expand Down
1 change: 1 addition & 0 deletions deku-derive/src/macros/deku_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ fn emit_field_write(
f.bits.as_ref(),
f.bytes.as_ref(),
f.ctx.as_ref(),
input.ctx_nested.as_ref(),
)?;

quote! { #object_prefix #field_ident.write(__deku_output, (#write_args)) }
Expand Down
16 changes: 12 additions & 4 deletions deku-derive/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,26 @@ fn gen_field_args(
bits: Option<&Num>,
bytes: Option<&Num>,
ctx: Option<&Punctuated<syn::Expr, syn::token::Comma>>,
ctx_nested: Option<&Punctuated<syn::Expr, syn::token::Comma>>,
) -> syn::Result<TokenStream> {
let crate_ = get_crate_name();
let endian = endian.map(gen_endian_from_str).transpose()?;
let bits = bits.map(|n| quote! {::#crate_::ctx::Size::Bits(#n)});
let bytes = bytes.map(|n| quote! {::#crate_::ctx::Size::Bytes(#n)});
let ctx = ctx.map(|c| quote! {#c});
let ctx_nested = ctx_nested.map(|c| quote! {#c});

// FIXME: Should be `into_iter` here, see https://github.com/rust-lang/rust/issues/66145.
let field_args = [endian.as_ref(), bits.as_ref(), bytes.as_ref(), ctx.as_ref()]
.iter()
.filter_map(|i| *i)
.collect::<Vec<_>>();
let field_args = [
endian.as_ref(),
bits.as_ref(),
bytes.as_ref(),
ctx.as_ref(),
ctx_nested.as_ref(),
Copy link
Owner Author

@sharksforarms sharksforarms Jun 22, 2021

Choose a reason for hiding this comment

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

should be first?

impl DekuWrite<Endian, Size, MyCtx> for u32 {
vs
impl DekuWrite<MyCtx, Endian, Size> for u32 {

Copy link
Collaborator

Choose a reason for hiding this comment

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

I like MyCtx being first, since it's "non-default" deku usage and something you want to see first in impl error.

]
.iter()
.filter_map(|i| *i)
.collect::<Vec<_>>();

// Because `impl DekuRead<'_, (T1, T2)>` but `impl DekuRead<'_, T1>`(not tuple)
match &field_args[..] {
Expand Down