Skip to content

fix: flush Writer on drop - #199

Merged
martin-g merged 3 commits into
apache:mainfrom
Kriskras99:fix/writer_flush_on_drop
May 30, 2025
Merged

martin-g merged 3 commits into
apache:mainfrom
Kriskras99:fix/writer_flush_on_drop

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

This brings the writer in line with standard library types like BufWriter and LineWriter.

This is a breaking change when the inner writer is &mut W and used later in the scope without dropping the writer or calling into_inner (as seen in the fixed tests).

@martin-g

Copy link
Copy Markdown
Member

I will have to do some research before merging this PR.

@Kriskras99

Copy link
Copy Markdown
Contributor Author

Understandable, if you have any questions just ask. The implementation is based on std::io::BufWriter.

@martin-g
martin-g requested a review from Copilot May 30, 2025 11:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures the Writer flushes its buffer on drop (like BufWriter) and provides an into_inner method to extract the underlying writer without double-flushing.

  • Added a Drop impl to auto-flush (errors ignored) when Writer is dropped.
  • Implemented into_inner using ManuallyDrop to safely return the inner writer.
  • Updated tests to call into_inner after flush to avoid relying on drop timing.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
avro/tests/union_schema.rs Added let _ = writer.into_inner()?; to finalize the writer in the test.
avro/tests/to_from_avro_datum_schemata.rs Inserted let _ = writer.into_inner()?; after flush() to match new behavior.
avro/src/writer.rs Expanded into_inner to use ManuallyDrop, added a Drop impl, and updated docs.

Comment thread avro/src/writer.rs Resolved
Comment thread avro/src/writer.rs Resolved
@martin-g

Copy link
Copy Markdown
Member

This brings the writer in line with standard library types like BufWriter and LineWriter.

I do not see similar code in LineWriter

@Kriskras99

Kriskras99 commented May 30, 2025 •

Copy link
Copy Markdown
Contributor Author

I do not see similar code in LineWriter

That's because LineWriter is just a wrapper around BufWriter and thus doesn't have to implement this.

@Kriskras99

Copy link
Copy Markdown
Contributor Author

Not sure why the CI is angry, I don't see a double get_ref or get_mut implementation. And I can't reproduce it in codespaces

kriskras99 and others added 3 commits May 31, 2025 00:04
This brings the writer in line with standard library types like `BufWriter` and `LineWriter`.

This is a breaking change when the inner writer is `&mut W` and used later in the scope without dropping the writer or calling `into_inner` (as seen in the fixed tests).
@martin-g
martin-g force-pushed the fix/writer_flush_on_drop branch from 6e113ce to d58ce4d Compare May 30, 2025 21:05
@martin-g
martin-g merged commit 9f37816 into apache:main May 30, 2025
@martin-g

Copy link
Copy Markdown
Member

Thank you once again, @Kriskras99 !

@Kriskras99

Copy link
Copy Markdown
Contributor Author

Thanks for taking the time to do an in-depth review!

@Kriskras99
Kriskras99 deleted the fix/writer_flush_on_drop branch June 1, 2025 08:01
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.

3 participants