jam1garner / jam1garner/binwrite

derive macro silently fails when ran on a tuple struct

Open
#3 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
18
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Apologies, this isn't exactly a minimal test, just how I discovered it.

```rust
use binread::{BinRead, NullString};
use binwrite::BinWrite;

// this *compiles* but does not behave as exepected.
#[derive(BinRead, BinWrite, Clone, PartialEq, Default)]
pub struct BadNullString(
#[binwrite(cstr, preprocessor(NullString::to_string))]
NullString
);

#[test]
fn test_bad() {
let test = BadNullString(NullString(Vec::from(&b"Test!"[..])));

let expected = Vec::from(&b"Test!\0"[..]);

let mut writer = std::io::Cursor::new(Vec::new());
test.write(&mut writer).unwrap();

let res = writer.into_inner();
assert_eq!(res, expected); // fails, res has no bytes in it
}

// this works correctly
#[derive(BinRead, BinWrite, Clone, PartialEq, Default)]
pub struct GoodNullString {
#[binwrite(cstr, preprocessor(NullString::to_string))]
inner: NullString
}

#[test]
fn test_good() {
let test = GoodNullString { inner: NullString(Vec::from(&b"Test!"[..])) };

let expected = Vec::from(&b"Test!\0"[..]);

let mut writer = std::io::Cursor::new(Vec::new());
test.write(&mut writer).unwrap();

let res = writer.into_inner();
assert_eq!(res, expected); // passes
}
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided tuple-struct and named-struct tests, then locate the BinWrite derive macro entry point and compare how each struct shape is expanded. Done means the tuple-struct case writes the expected Test!\0 bytes while the named-struct case continues to pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.