#[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:
- When manually implementing
BinRead
orBinWrite
on a type where named arguments are desired. - When creating a custom parser or writer where named arguments are desired.
- 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 toBinRead::Args
orBinWrite::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>,
}