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

Add finisher drop implementation to BzEncoder #121

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ impl<W: Write> Drop for BzDecoder<W> {
}
}

impl<W: Write> Drop for BzEncoder<W> {
fn drop(&mut self) {
if self.obj.is_some() {
let _ = self.try_finish();
Copy link
Collaborator

Choose a reason for hiding this comment

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

std::io::BufWriter skips the flush on Drop if a panic happened while writing to the inner writer. It does so by having a panicked field which is set to true before writing to the inner writer and set to false immediately after writing. This avoids a double panic if the inner writer would panic on the second write too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you implying that we should be doing something similar here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice to do so yeah.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not understand what you want me to tell here or how to apply the panicked field in this implementation.

}
}
}

#[cfg(test)]
mod tests {
use super::{BzDecoder, BzEncoder};
Expand Down Expand Up @@ -383,4 +391,29 @@ mod tests {
.into_inner()
}
}

#[test]
fn terminate_on_drop() {
// Test that dropping the BzEncoder will result in a valid, decompressable datastream
let s = "12345".repeat(100000);

let mut compressed = Vec::new();
{
let mut c: Box<dyn std::io::Write> =
Box::new(BzEncoder::new(&mut compressed, Compression::default()));
c.write_all(b"12834").unwrap();
c.write_all(s.as_bytes()).unwrap();
c.flush().unwrap();
}
assert!(!compressed.is_empty());

let uncompressed = {
let mut d = BzDecoder::new(Vec::new());
d.write_all(&compressed).unwrap();
d.finish().unwrap()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the Drop implementation, this line fails with:

called `Result::unwrap()` on an `Err` value: Custom { kind: UnexpectedEof, error: "Input EOF reached before logical stream end" }

};
assert_eq!(&uncompressed[0..5], b"12834");
assert_eq!(uncompressed.len(), 500005);
assert!(format!("12834{}", s).as_bytes() == &*uncompressed);
}
}
Loading