#[binrw]
#[br(import(item_size: u32))]
struct Block {
... // some fields
example: u8,
#[br(parse_with=count(item_size as usize))]
content: Vec<u8>,
}
#[binrw]
struct Something {
item_count: u32,
#[br(parse_with=count(item_count as usize))]
sizes: Vec<u32>,
// problem is here
#[br(parse_with=count(item_count as usize))]
blocks: Vec<Block>,
}
Each Block is of variable size, their sizes determined by their fields + a Vec<u8> block whose size is determined by the item in the previous sizes Vec. (that is, blocks[3] has a content of size sizes[3]).
While I can get args to Vec, or get args to be passed to all elements (using VecArgs::inner), I specifically need Block to be able to either:
receive a different arg per item (such as being able to directly have them receive the value of sizes[index], maybe something like #[br(zip=sizes)]?)
receive what index it is in the Vec (such that I can pass sizes itself as an arg, and index it like count(sizes[index] as usize)
I can't really use seek tricks because the blocks are end-to-end and thus their offsets become cumulative (the offset of blocks[5] would require me to sum the previous sizes), and because some of the fields of Block may be optionally not there (so the size of the other fields may also vary if a field is omitted).
At the moment the only real thing I can think of is writing a custom parser, but it feels like there should be an easier way to deal with transposed tables like this.
I have a format that looks basically like this:
Each
Block
is of variable size, their sizes determined by their fields + aVec<u8>
block whose size is determined by the item in the previoussizes
Vec. (that is, blocks[3] has acontent
of size sizes[3]).While I can get args to Vec, or get args to be passed to all elements (using
VecArgs::inner
), I specifically need Block to be able to either:sizes[index]
, maybe something like#[br(zip=sizes)]
?)sizes
itself as an arg, and index it likecount(sizes[index] as usize)
I can't really use
seek
tricks because the blocks are end-to-end and thus their offsets become cumulative (the offset of blocks[5] would require me to sum the previous sizes), and because some of the fields of Block may be optionally not there (so the size of the other fields may also vary if a field is omitted).At the moment the only real thing I can think of is writing a custom parser, but it feels like there should be an easier way to deal with transposed tables like this.