Skip to content

Commit

Permalink
Do not move read function when validating args type
Browse files Browse the repository at this point in the history
Fixes #185.
  • Loading branch information
csnover committed Oct 25, 2023
1 parent 6b2173b commit acaf7cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions binrw/tests/derive/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,27 @@ fn parse_with_default_args() {
);
}

#[test]
fn args_type_hint_borrowck() {
#[derive(BinRead, Debug, PartialEq)]
#[br(import(a: u8))]
struct NeedsArgs(#[br(map = |x: u8| x + a)] u8);

#[derive(BinRead, Debug, PartialEq)]
struct Test {
#[br(args(4), parse_with = binrw::helpers::until(|x| x == &NeedsArgs(4)))]
a: Vec<NeedsArgs>,
}

let result = Test::read_le(&mut Cursor::new(b"\x01\x00\x02")).unwrap();
assert_eq!(
result,
Test {
a: vec![NeedsArgs(5), NeedsArgs(4)]
}
);
}

#[test]
fn args_same_name() {
#[allow(dead_code)]
Expand Down
2 changes: 1 addition & 1 deletion binrw_derive/src/binrw/codegen/read_options/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ impl<'field> FieldGenerator<'field> {

if let FieldMode::Function(_) = &self.field.field_mode {
quote_spanned! {ty.span()=>
let #args_var = #ARGS_TYPE_HINT::<R, #ty, _, _>(#READ_FUNCTION, #args);
let #args_var = #ARGS_TYPE_HINT::<R, #ty, _, _>(&#READ_FUNCTION, #args);
}
} else {
match &self.field.map {
Expand Down

0 comments on commit acaf7cb

Please sign in to comment.