jam1garner / binrw

A Rust crate for helping parse and rebuild binary data using ✨macro magic✨.
https://binrw.rs
MIT License
545 stars 35 forks source link

Borrow of moved value error when derriving binread #236

Closed daniel-brenot closed 8 months ago

daniel-brenot commented 8 months ago

I followed the example documented by copying the code for the checksum struct to verify the behavior. Everything worked fine until i attempted to use a Vec with the count attribute, after which i got the following error

borrow this binding in the pattern to avoid moving the value: `ref `rustc[E0382](https://doc.rust-lang.org/error-index.html#E0382)
section_3.rs(6, 10): original diagnostic
borrow of moved value: `__binrw_generated_var_reader`
value borrowed here after moverustc[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20%5B0%5D?0#file%3A%2F%2F%2Fhome%2Fdbrenot%2FDocuments%2Fprojects%2FEXTERNAL%2Fgrib2%2Fsrc%2Fsection_3.rs)
section_3.rs(20, 18): borrow occurs due to use in closure
section_3.rs(6, 10): borrow this binding in the pattern to avoid moving the value: `ref `
borrow of moved value: `__binrw_generated_var_reader`
move occurs because `__binrw_generated_var_reader` has type `&mut R`, which does not implement the `Copy` trait

For completeness sake, here is the full code for the struct as well:

#[derive(BinRead, BinWrite)]
#[br(
    stream = r, map_stream = |reader| Checksum::new(reader),
    assert(section_number == 3)
)]
pub struct GridDefinitionSection {
    pub length: u32,
    section_number: u8,
    pub grid_definition_source: u8,
    pub data_points_length: u32,
    pub points_list_length: u8,
    pub number_of_points_interpretation: u8,
    pub grid_definition_template: GridDefinitionTemplate,
    // TODO figure out what this is supposed to be
    #[br(count = 15-length)]
    pub number_of_points: Vec<u8>
}

What can i do to be able to use the map_stream together with the count attribute?

csnover commented 8 months ago

Thank you for the bug report.

For the moment you can work around this by removing stream = r since you are not using r, or by using args { count: (15-length) as usize } instead of count = 15-length.

csnover commented 8 months ago

This issue has been fixed in version 0.13.1. Thanks again!

daniel-brenot commented 8 months ago

Just tried the new version and this seems to work now. Thanks!