Skip to content
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

Merged
merged 1 commit into from
May 28, 2024

Conversation

Patiga
Copy link
Collaborator

@Patiga Patiga commented May 16, 2024

Problem I want to solve

Currently, you can only pass 'static readers/writers into the demo readers/writers.
This works for Files, but not for writing into Vec<u8>.
As we require io::Seek for both reader and writer, we would not write into Vec<u8> directly, but a Cursor<&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.

  1. Weird lifetime issue in tools/demo_read_write.
  2. Lifetime issue in tools/teehistorian2demo, where I tried to actually write into a Cursor<Vec<u8>>. (I also tried Cursor<&mut Vec<u8>> and that didn't work either.

pub struct Reader {
data: Box<dyn SeekableRead>,
pub struct Reader<'a> {
data: Box<dyn SeekableRead<'a>>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data: Box<dyn SeekableRead<'a>>,
data: Box<dyn SeekableRead + 'a>,

I believe changing the trait itself isn't necessary, the + 'a here should suffice.

Copy link
Collaborator Author

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)

Copy link
Owner

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:

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.

Copy link
Collaborator Author

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>`

Copy link
Collaborator Author

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

@Patiga
Copy link
Collaborator Author

Patiga commented May 17, 2024

The suggested changes solved all the issues 👍
This change did not require any changes in tools.
It should only lessen the restriction on the parameter (and make the code a little more complicated).

demo/src/reader.rs Outdated Show resolved Hide resolved
Previously only owned readers / writers could be used.
Now, `&mut Vec<u8>` can be used.

Also minor formatting changes.
Copy link
Owner

@heinrich5991 heinrich5991 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@heinrich5991 heinrich5991 merged commit 07c59ca into heinrich5991:master May 28, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants