Skip to content

Parse vector where each entry is parametered by previous vector #301

Answered by csnover
vizigr0u asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, thanks for your question!

You are looking for the args_iter helper, which will be used something like this:

#[binread]
struct Data {
    #[br(temp)]
    _headers_count: u32,
    #[br(count = _headers_count)]
    pub headers: Vec<MyObjectHeader>,
    #[br(temp, assert(_object_count == _headers_count))]
    _object_count: u32,
    #[br(parse_with = binrw::helpers::args_iter(&headers))]
    pub objects: Vec<MyObject>,
}

// …

#[derive(BinRead)]
#[br(import_raw(header: &MyObjectHeader))]
struct MyObject {
    // …
}

Due to a bug in the current version of binrw, you will have to do one of two things instead temporarily:

  1. Use args_iter_with(&headers, |r, e, a| MyObject::read_options(r, e, …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vizigr0u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants