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

Don't panic on drop #100

Merged
merged 4 commits into from Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/frame/compress.rs
Expand Up @@ -30,7 +30,11 @@
/// writer in a `std::io::BufWriter`.
///
/// To ensure a well formed stream the encoder must be finalized by calling
/// either `finish` or `try_finish()` methods.
/// either the [`finish()`], [`try_finish()`], or [`auto_finish()`] methods.
///
/// [`finish()`]: Self::finish
/// [`try_finish()`]: Self::try_finish
/// [`auto_finish()`]: Self::auto_finish
///
/// # Example 1
/// Serializing json values into a compressed file.
Expand Down Expand Up @@ -113,9 +117,12 @@

/// Returns a wrapper around `self` that will finish the stream on drop.
///
/// # Panic
/// # Note
/// Errors on drop get silently ignored. If you want to handle errors then use [`finish()`] or
/// [`try_finish()`] instead.
///
/// Panics on drop if an error happens when finishing the stream.
/// [`finish()`]: Self::finish
/// [`try_finish()`]: Self::try_finish
pub fn auto_finish(self) -> AutoFinishEncoder<W> {
AutoFinishEncoder {
encoder: Some(self),
Expand Down Expand Up @@ -387,12 +394,18 @@
}
}

/// A wrapper around an `FrameEncoder<W>` that finishes the stream on drop.
/// A wrapper around an [`FrameEncoder<W>`] that finishes the stream on drop.
///
/// This can be created by the [`auto_finish()`] method on the [`FrameEncoder<W>`].
///
/// This can be created by the [`auto_finish()`] method on the [`FrameEncoder`].
/// # Note
/// Errors on drop get silently ignored. If you want to handle errors then use [`finish()`] or
/// [`try_finish()`] instead.
///
/// [`auto_finish()`]: Encoder::auto_finish
/// [`Encoder`]: Encoder
/// [`finish()`]: FrameEncoder::finish
/// [`try_finish()`]: FrameEncoder::try_finish
/// [`auto_finish()`]: FrameEncoder::auto_finish
/// [`FrameEncoder<W>`]: FrameEncoder
PSeitz marked this conversation as resolved.
Show resolved Hide resolved
pub struct AutoFinishEncoder<W: Write> {
// We wrap this in an option to take it during drop.
encoder: Option<FrameEncoder<W>>,
Expand All @@ -401,9 +414,7 @@
impl<W: io::Write> Drop for AutoFinishEncoder<W> {
fn drop(&mut self) {
if let Some(mut encoder) = self.encoder.take() {
if let Err(err) = encoder.try_finish() {
panic!("Error when flushing frame on drop {:?} ", err);
}
let _ = encoder.try_finish();

Check warning on line 417 in src/frame/compress.rs

View check run for this annotation

Codecov / codecov/patch

src/frame/compress.rs#L417

Added line #L417 was not covered by tests
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/frame/mod.rs
Expand Up @@ -25,7 +25,7 @@ pub(crate) mod compress;
pub(crate) mod decompress;
pub(crate) mod header;

pub use compress::FrameEncoder;
pub use compress::{AutoFinishEncoder, FrameEncoder};
pub use decompress::FrameDecoder;
pub use header::{BlockMode, BlockSize, FrameInfo};

Expand Down