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.
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:
If the finish() method of the LzmaWriter returned the inner io::Write, it would reduce to:
The flate2 library does this and makes for some clean code.