jam1garner / jam1garner/binrw

Stream borrowed after a move

Open
#370 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
853
Forks
56
PR merge metrics
No merged PRs in 30d

Description

Try building a library with the following code:
```rust
use binrw::binread;

#[binread]
#[br(stream = reader)]
enum Foo {
Bar {
#[br(count = 1)]
vec: Vec,
},
}
```

I would expect this to compile, but it gives the following error:
```
error[E0382]: borrow of moved value
--> src\lib.rs:3:1
|
3 | #[binread]
| ^^^^^^^^^^
| |
| value moved due to this method call
| borrow occurs due to use in closure
| value borrowed here after move
| move occurs because value has type `&mut R`,which does not implement the `Copy` trait
|
note: `stream_position` takes ownership of the receiver `self`, which moves value
--> *omitted*/rust\library\std\src\io\mod.rs:2154:29
|
2154 | fn stream_position(&mut self) -> Resu...
| ^^^^
= note: this error originates in the attributemacro `binread` (in Nightly builds, run with -Z macro-backtrace for more info)
help: borrow this binding in the pattern to avoid moving the value
|
3 | ref #[binread]
| +++

For more information about this error, try `rustc --explain E0382`.
error: could not compile `binrw_test` (lib) due to 1 previous error
```

Contributor guide

Open the contributing guide

Research direction

Start with the #[binread] attribute macro and reproduce the reported example to inspect how the stream is handled while parsing the enum. Trace the generated code around stream_position and borrowing, then add a regression test for the example that compiles without E0382.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.