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 all 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
30 changes: 20 additions & 10 deletions src/frame/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ use crate::block::WINDOW_SIZE;
/// 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 @@ impl<W: io::Write> FrameEncoder<W> {

/// 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,17 @@ impl<W: io::Write> io::Write for FrameEncoder<W> {
}
}

/// 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
pub struct AutoFinishEncoder<W: Write> {
// We wrap this in an option to take it during drop.
encoder: Option<FrameEncoder<W>>,
Expand All @@ -401,9 +413,7 @@ pub struct AutoFinishEncoder<W: Write> {
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();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/frame/mod.rs
Original file line number Diff line number Diff line change
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