-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] demo: Replace 'static with explicit lifetime 'a #97
Conversation
demo/src/reader.rs
Outdated
pub struct Reader { | ||
data: Box<dyn SeekableRead>, | ||
pub struct Reader<'a> { | ||
data: Box<dyn SeekableRead<'a>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data: Box<dyn SeekableRead<'a>>, | |
data: Box<dyn SeekableRead + 'a>, |
I believe changing the trait itself isn't necessary, the + 'a
here should suffice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. Could you elaborate how this solved the weird lifetime issue in tools/demo_read_write
?
(I unfortunately already squashed the commits. From memory: The writer was initialized with references from the reader. Then the borrow checker complained when the writer was used later)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple of ways you can recover squashed commits:
- In your
git reflog
. - In this pull request, ctrl-f "force-pushed".
- Using GitHub events if it's only on a remote branch: https://neodyme.io/en/blog/github_secrets/.
I couldn't quickly see why the code wasn't accepted before. SeekableRead<'a>
is different from SeekableRead + 'a
because the compiler cannot assume that a type also implements SeekableRead<'b>
if it implements SeekableRead<'a>
and 'b
is shorter than 'a
. But of course every type that is SeekableRead + 'a
is also SeekableRead + 'b
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat tool :)
The lifetime issue is in 1703cab
Compiler error:
error[E0597]: `reader` does not live long enough
--> tools/src/bin/demo_read_write.rs:73:9
|
70 | let mut reader = ddnet::DemoReader::<DDNet>::new(input_file, &mut warn::Log)?;
| ---------- binding `reader` declared here
...
73 | reader.inner().net_version(),
| ^^^^^^ borrowed value does not live long enough
...
95 | }
| -
| |
| `reader` dropped here while still borrowed
| borrow might be used here, when `reader` is dropped and runs the destructor for type `DemoReader<'_, Protocol>`
error[E0502]: cannot borrow `reader` as mutable because it is also borrowed as immutable
--> tools/src/bin/demo_read_write.rs:83:29
|
73 | reader.inner().net_version(),
| ------ immutable borrow occurs here
...
83 | while let Some(chunk) = reader.next_chunk(&mut warn::Log)? {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
...
95 | }
| - immutable borrow might be used here, when `reader` is dropped and runs the destructor for type `DemoReader<'_, Protocol>`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only change here is that the lifetime is also in the trait.
The lifetime of the trait is the same as the file
parameter. The other parameters, which cause the issue here, shouldn't be taken into account afaik
The suggested changes solved all the issues 👍 |
Previously only owned readers / writers could be used. Now, `&mut Vec<u8>` can be used. Also minor formatting changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Problem I want to solve
Currently, you can only pass
'static
readers/writers into the demo readers/writers.This works for
File
s, but not for writing intoVec<u8>
.As we require
io::Seek
for both reader and writer, we would not write intoVec<u8>
directly, but aCursor<&mut Vec<u8>>
should work.Why RFC
I'm not sure about the implications of this change.
Introducing a lifetime typically makes using it more difficult.
This is apparent with the lifetime issues that arose.
Issues
New lifetime issues which I wasn't able to solve, I guess I disagree with the compiler on some level.
tools/demo_read_write
.tools/teehistorian2demo
, where I tried to actually write into aCursor<Vec<u8>>
. (I also triedCursor<&mut Vec<u8>>
and that didn't work either.