Function binrw::helpers::args_iter_with

source ·
pub fn args_iter_with<Reader, T, Arg, Ret, It, ReadFn>(
    it: It,
    read: ReadFn,
) -> impl FnOnce(&mut Reader, Endian, ()) -> BinResult<Ret>
where Reader: Read + Seek, Arg: Clone, Ret: FromIterator<T>, It: IntoIterator<Item = Arg>, ReadFn: Fn(&mut Reader, Endian, Arg) -> BinResult<T>,
Expand description

Creates a parser that uses a given function to build a collection, using items from the given iterable object as arguments for the function.

The given read function should return one item each time it is called.

This helper can be used to read into any collection type that implements FromIterator.

§Examples

Reading an object containing header data followed by body data:

#[derive(BinRead)]
#[br(big)]
struct Header {
    count: u16,

    #[br(args { count: count.into() })]
    sizes: Vec<u16>,
}

#[derive(BinRead)]
#[br(big)]
struct Object {
    header: Header,
    #[br(parse_with = args_iter_with(&header.sizes, |reader, options, &size| {
        Vec::<u8>::read_options(reader, options, args! { count: size.into() })
    }))]
    segments: Vec<Vec<u8>>,
}