-
Notifications
You must be signed in to change notification settings - Fork 63
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
folkertdev
merged 5 commits into
trifectatechfoundation:master
from
jonasbb:add-drop-bzencoder
Jan 13, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c3be758
Add finisher drop implementation to BzEncoder
jonasbb 1539a77
Add test that ensures the Drop implementation for BzEncoder will not …
jonasbb 5859239
link PR in the test
folkertdev c2e93b1
add `panicked` flag to `BzEncoder`
folkertdev 2353eed
add `panicked` flag to `BzDecoder`
folkertdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{BzDecoder, BzEncoder}; | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the
|
||
}; | ||
assert_eq!(&uncompressed[0..5], b"12834"); | ||
assert_eq!(uncompressed.len(), 500005); | ||
assert!(format!("12834{}", s).as_bytes() == &*uncompressed); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
std::io::BufWriter
skips the flush on Drop if a panic happened while writing to the inner writer. It does so by having apanicked
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.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.
Are you implying that we should be doing something similar here?
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.
It would be nice to do so yeah.
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.
I do not understand what you want me to tell here or how to apply the
panicked
field in this implementation.