Derive Macro binrw::NamedArgs

source ·
#[derive(NamedArgs)]
{
    // Attributes available to this derive:
    #[named_args]
}
Expand description

Derive macro generating an impl of the trait NamedArgs.

The use cases for this macro are:

  1. When manually implementing BinRead or BinWrite on a type where named arguments are desired.
  2. When creating a custom parser or writer where named arguments are desired.
  3. When a named arguments type should be shared by several different types (e.g. by using import_raw on derived types, and by assigning the type to BinRead::Args or BinWrite::Args in manual implementations).

Field options

  • #[named_args(default = $expr)]: Sets the default value for a field.

Examples

use binrw::{args, binread, BinRead, NamedArgs};
#[derive(Clone, NamedArgs)]
struct GlobalArgs<Inner> {
    #[named_args(default = 1)]
    version: i16,
    inner: Inner,
}

#[binread]
#[br(import_raw(args: GlobalArgs<T::Args<'_>>))]
struct Container<T>
where
    T: BinRead + 'static,
    for<'a> T::Args<'a>: Clone,
{
    #[br(temp, if(args.version > 1, 16))]
    count: u16,
    #[br(args {
        count: count.into(),
        inner: args.inner
    })]
    items: Vec<T>,
}