-
Hello, #[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(count = _object_count)]
pub objects: Vec<MyObject>,
}
#[derive(BinRead)]
struct MyObjectHeader {
pub object_type: u32,
// other header data
}
#[derive(BinRead)]
#[br(import(is_a: bool))]
struct MyObject {
#[br(if(is_a))]
pub a: Option<A>,
#[br(if(!is_a))]
pub b: Option<B>,
}
#[derive(BinRead)]
struct A {
pub blah: u32,
}
#[derive(BinRead)]
struct B {
pub foo: u32,
pub bar: u32,
} I can't figure out how to pass the data for each element to the parser that parses |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, thanks for your question! You are looking for the #[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:
This will be fixed in the next release, which should come out fairly soon, at which point you will be able to do as shown in the original suggestion. |
Beta Was this translation helpful? Give feedback.
Hi, thanks for your question!
You are looking for the
args_iter
helper, which will be used something like this:Due to a bug in the current version of binrw, you will have to do one of two things instead temporarily:
args_iter_with(&headers, |r, e, a| MyObject::read_options(r, e, …