binrw icon indicating copy to clipboard operation
binrw copied to clipboard

Add support for reference arguments

Open jam1garner opened this issue 4 years ago • 1 comments

Todo:

  • [x] Document end-user ability to use reference types
    • [x] Document limitations regarding manual lifetime specification (eg to use '_)
  • [x] Better document how manual BinRead implementations now require slight adjustments
    • [x] Most notably special care should be put towards how this interacts with generics
  • [ ] Fix issues regarding Punctuated's use of VecArgs breaking doctests
  • [ ] Migration guide
  • [x] Testing regarding how named arguments interact with reference types
  • [ ] Add support for reference types for BinWrite too

Example

#[derive(BinRead, Debug, PartialEq)]
#[br(import(x: &'_ u32, y: &'_ str))]
struct ImportRefs {
    #[br(calc(*x))]
    a: u32,

    #[br(calc(y.to_owned()))]
    b: String,
}

Also Adds

  • arg_type!() - a macro which takes a type and turns it into a type that can be suitable for use as BinRead::Args. For example code that previously did type Args = u32; would now do type Args = arg_type!(u32);. It also allows for the default lifetime ('_) to be used, so now type Args = arg_type!(&'_ u32); works.
  • args_of!() - a macro that when given a type expands into the associated type representing the arguments of that type. So code that previously used T::Args would now use args_of!(T) and code that previously used <T as BinRead>::Args would now use args_of!(T as BinRead)

Closes #97

jam1garner avatar Feb 13 '22 20:02 jam1garner

Codecov Report

Merging #95 (e5c1080) into master (446f8ff) will increase coverage by 0.13%. The diff coverage is 81.08%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #95      +/-   ##
==========================================
+ Coverage   67.59%   67.72%   +0.13%     
==========================================
  Files          63       64       +1     
  Lines        3148     3170      +22     
==========================================
+ Hits         2128     2147      +19     
- Misses       1020     1023       +3     
Impacted Files Coverage Δ
binrw/src/helpers.rs 0.00% <ø> (ø)
binrw/src/pos_value.rs 94.44% <ø> (ø)
binrw/src/private.rs 60.00% <ø> (ø)
binrw/src/punctuated.rs 82.60% <ø> (ø)
binrw_derive/src/codegen/mod.rs 80.00% <ø> (ø)
binrw_derive/src/codegen/read_options/struct.rs 71.16% <0.00%> (ø)
binrw_derive/src/lib.rs 86.88% <ø> (ø)
binrw/src/binread/mod.rs 76.92% <66.66%> (-2.25%) :arrow_down:
binrw_derive/src/args_type.rs 72.72% <72.72%> (ø)
binrw/src/binread/impls.rs 81.63% <100.00%> (ø)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 446f8ff...e5c1080. Read the comment docs.

codecov-commenter avatar Feb 13 '22 20:02 codecov-commenter