it is defined in the BinRead::read/BinWrite::write function, so it is not visible from outside
position_of!(x) gives the stream position at which the field x has been read
Additional ideas
to make look less like it's popping out of nowhere, add a struct-level position_of attribute such as:
if it is absent, the macro is not added in the generated code
#[br(position_of)] means we add it with it's default name
#[br(position_of = my_name)] means we add it and name it my_name
Concerns
what to return when the field is not actually read/written:
when it's conditionally read/written (with and if attribute for example), should it be:
an Option<u64> which is None when the field is not read and Some when it is (preferred)
a regular u64 that gives the position of the stream where the field could've been read (introduce concerns with conditionally applied seek/align directives)
when it's calculated with calc, should it be:
not defined at all (preferred)
a regular u64 that gives the position of the stream at the time the field is calculated
any other edge case?
Implementation proposals
The main difficulty is that we need to define a macro that allows us to reference earlier fields only:
we can redefine the macro each time a new field position is known, so for the given example you'd have:
This issue is a feature proposal for adding a
position_of!
macro in the generated code for reading/writing.Main features
BinRead::read
/BinWrite::write
function, so it is not visible from outsideposition_of!(x)
gives the stream position at which the fieldx
has been readAdditional ideas
position_of
attribute such as:#[br(position_of)]
means we add it with it's default name#[br(position_of = my_name)]
means we add it and name itmy_name
Concerns
if
attribute for example), should it be:Option<u64>
which isNone
when the field is not read andSome
when it is (preferred)u64
that gives the position of the stream where the field could've been read (introduce concerns with conditionally appliedseek
/align
directives)calc
, should it be:u64
that gives the position of the stream at the time the field is calculatedImplementation proposals
The main difficulty is that we need to define a macro that allows us to reference earlier fields only:
once the position of
a
is known, and as soon as the position ofb
is known, redefine it this way: