#[writer]
Expand description
Attribute macro used to generate
write_with
functions.
Rust functions are transformed by this macro to match the binrw API.
§Attribute options
#[writer(writer)]
or#[writer(writer: $ident)]
: Exposes the write stream to the function. If no variable name is given,writer
is used.#[writer(endian)]
or#[writer(endian: $ident)]
: Exposes the endianness to the function. If no variable name is given,endian
is used.
Options are comma-separated.
§Function parameters
The first parameter is required and receives a reference to the object being written.
Subsequent 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 remaining parameters in the signature will be converted to a tuple. For example:
#[binrw::writer(writer: w, endian)]
fn custom_writer(obj: &Object, v0: u8, v1: i16) -> binrw::BinResult<()> {
Ok(())
}
The transformed output for this function is:
use binrw::{BinResult, Endian, io::{Seek, Write}};
fn custom_writer<W: Write + Seek>(
obj: &Object,
w: &mut W,
endian: Endian,
(v0, v1): (u8, i16)
) -> BinResult<()> {
Ok(())
}
§Raw arguments
Use a variadic function signature with a second parameter. The name and type of the second parameter will be used as the raw argument. For example:
#[binrw::writer]
fn custom_writer(obj: &Object, args: ArgsType, ...) -> binrw::BinResult<()> {
Ok(())
}
The transformed output for this function is:
use binrw::{BinResult, Endian, io::{Seek, Write}};
fn custom_writer<W: Write + Seek>(
obj: &Object,
_: &mut W,
_: Endian,
args: ArgsType
) -> BinResult<()> {
Ok(())
}
§Return value
The return value of a writer function must be BinResult<()>
.