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

Impl Drop for Lzmawriter #24

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/error.rs
Expand Up @@ -32,7 +32,7 @@ impl fmt::Display for LzmaError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LzmaError::Io(ref err) => write!(f, "{}", err),
_ => write!(f, "{}", self.description()),
_ => write!(f, "{}", self.to_string()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -41,7 +41,7 @@ pub use writer::LzmaWriter;
pub use error::LzmaError;


pub const EXTREME_PRESET: u32 = (1 << 31);
pub const EXTREME_PRESET: u32 = 1 << 31;


/// Compress `buf` and return the result.
Expand Down
25 changes: 20 additions & 5 deletions src/writer.rs
Expand Up @@ -30,7 +30,7 @@ use lzma_stream_wrapper::{LzmaStreamWrapper, LzmaCodeResult};
const DEFAULT_BUF_SIZE: usize = 4 * 1024;


pub struct LzmaWriter<T> {
pub struct LzmaWriter<T: Write> {
inner: T,
stream: LzmaStreamWrapper,
buffer: Vec<u8>,
Expand Down Expand Up @@ -71,9 +71,9 @@ impl<T: Write> LzmaWriter<T> {
impl<W: Write> LzmaWriter<W> {
/// Finalizes the LZMA stream so that it finishes compressing or decompressing.
///
/// This *must* be called after all writing is done to ensure the last pieces of the compressed
/// This can be called after all writing is done to ensure the last pieces of the compressed
/// or decompressed stream get written out.
pub fn finish(mut self) -> Result<W, LzmaError> {
pub fn finish(mut self) -> Result<(), LzmaError> {
loop {
match self.lzma_code_and_write(&[], lzma_action::LzmaFinish) {
Ok(LzmaCodeResult {
Expand All @@ -85,8 +85,7 @@ impl<W: Write> LzmaWriter<W> {
Err(err) => return Err(err),
}
}

Ok(self.inner)
Ok(())
}

fn lzma_code_and_write(&mut self, input: &[u8], action: lzma_action) -> Result<LzmaCodeResult, LzmaError> {
Expand Down Expand Up @@ -127,3 +126,19 @@ impl<W: Write> Write for LzmaWriter<W> {
self.inner.flush()
}
}

impl<W: Write> Drop for LzmaWriter<W> {
fn drop(&mut self) {
loop {
match self.lzma_code_and_write(&[], lzma_action::LzmaFinish) {
Ok(LzmaCodeResult {
ret: Ok(lzma_ret::LzmaStreamEnd),
bytes_read: _,
bytes_written: _,
}) => break,
Ok(_) => continue,
Err(_) => break,
}
}
}
}