fpgaminer / rust-lzma

A Rust crate that provides a simple interface for LZMA compression and decompression.
MIT License
79 stars 14 forks source link

LzmaWriter.finish() should return the inner std::io::Write #14

Closed jpap closed 6 years ago

jpap commented 7 years ago

I can appreciate that perhaps the LzmaWriter was designed to write to files on disk, where the compressed data isn't immediately re-read from disk in the same process. However if we want to compress to memory, where the stream we write to is backed by memory, this becomes a pain.

We currently have to hack around the stream being moved into the LzmaWriter:

let mut backing: Vec<u8> = Vec::with_capacity(backing_size);
{
    let mut compressor = lzma::LzmaWriter::new_compressor(&mut backing, 6).unwrap();
    // ... perform writes ...
    compressor.finish().unwrap();
}
// ... now use compressed data in backing.

If the finish() method of the LzmaWriter returned the inner io::Write, it would reduce to:

let mut compressor = lzma::LzmaWriter::new_compressor(Vec::with_capacity(backing_size), 6).unwrap();
// ... perform writes ...
let backing = compressor.finish().unwrap();
// ... now use compressed data in backing.

The flate2 library does this and makes for some clean code.