#[parser]
Expand description
Attribute macro used to generate
parse_with
functions.
Rust functions are transformed by this macro to match the binrw API.
Attribute options
#[parser(reader)]
or#[parser(reader: $ident)]
: Exposes the write stream to the function. If no variable name is given,reader
is used.#[parser(endian)]
or#[parser(endian: $ident)]
: Exposes the endianness to the function. If no variable name is given,endian
is used.
Options are comma-separated.
Function parameters
Parameters are transformed into either tuple-style arguments or raw arguments depending upon the function signature.
Tuple-style arguments
Use a normal function signature. The parameters in the signature will be converted to a tuple. For example:
#[binrw::parser(reader: r, endian)]
fn custom_parser(v0: u8, v1: i16) -> binrw::BinResult<()> {
Ok(())
}
The transformed output for this function is:
use binrw::{BinResult, Endian, io::{Read, Seek}};
fn custom_parser<R: Read + Seek>(
r: &mut R,
endian: Endian,
(v0, v1): (u8, i16)
) -> BinResult<()> {
Ok(())
}
Raw arguments
Use a variadic function signature with a single parameter. The name and type of the parameter will be used as the raw argument. For example:
#[binrw::parser]
fn custom_parser(args: ArgsType, ...) -> binrw::BinResult<()> {
Ok(())
}
The transformed output for this function is:
use binrw::{BinResult, Endian, io::{Read, Seek}};
fn custom_parser<R: Read + Seek>(
_: &mut R,
_: Endian,
args: ArgsType
) -> BinResult<()> {
Ok(())
}
Return value
The return value of a parser function must be BinResult<T>
,
where T
is the type of the object being parsed.