libflate
libflate copied to clipboard
impl Drop
The Encoder
s should have impl Drop
that finishes the compressed stream upon drop()
. BufWriter
does something similar (flushes its buffer upon drop()
).
My use case is Write
trait objects. Consider the following trait object that writes compressed output to stdout
...
fn make_writer_object() -> Box<Write> {
let stdout = io::stdout();
let buf_stdout = io::BufWriter::new(stdout);
let gzip = gzip::Encoder::new(buf_stdout).unwrap();
Box::new(gzip)
}
This doesn't work because the last bit of compressed data gets cut off.
Thanks for your advice. It seems useful.
I added impl Drop
for encoders at the commit https://github.com/sile/libflate/commit/e7ec99bae233052fcf001b43d70294bd6e27c829.
Because this change breaks compatibility with previous versions, I will publish this as the version 0.2.0
(in a few days).
I reverted the above commit because implementing Drop
trait directly for encoders seemed to make it difficult to handle errors.
Instead, I tried adding AutoFinish
(and AutoFinshUnchecked
) for this purpose ( https://github.com/sile/libflate/commit/2fd7870b3d671a7c1a9e64c9bbd45ea8943f6392 ).
It can be used as follows:
use libflate::finish::AutoFinish;
fn make_writer_object() -> Box<Write> {
let stdout = io::stdout();
let buf_stdout = io::BufWriter::new(stdout);
let gzip = gzip::Encoder::new(buf_stdout).unwrap();
Box::new(AutoFinish::new(gzip))
}
For comparison I've also been using tar::Builder
which has to solve problems similar to gzip encoder. In tar
, if you care about errors you can explicitly call finish()
or into_inner()
to get a proper result, and if you don't care you can just drop()
and ignore any errors. This is a simpler API in my opinion.
Anyway, are you interested in help API with development? I had some ideas about unifying blocking/non-blocking code and supporting concatenated streams. I'm not interested in the underlying compression details, but I do like fiddling with APIs.
For comparison I've also been using
tar::Builder
which has to solve problems similar to gzip encoder.
Thank you for the information. It seems okay to implement the Drop
trait if it follows Rust's practices.
Anyway, are you interested in help API with development? I had some ideas about unifying blocking/non-blocking code and supporting concatenated streams.
Great! Your contributions are welcome. If your ideas are good, I would like to adopt them.
Sorry for bumping this old issue! But did it eventually land somewhere inside a new version? I see that the 0.2.0 is not yet released right? I am not sure if it was incorporated in another version?
@DevQps Thanks for your question. This feature has not been merged into master
branch.
@sile Alright! Thanks for informing us! If there is anything else I can do, feel free to contact!
@DevQps To be honest, I forgot about this feature. If you need it, I will consider it again, but what about it?
@sile No problem! I personally don't need it, so I am not sure whether it would be best to just leave it open for someone to potentially pick this up, or to just close it since it's so old. But I'll leave that up to you! :)
@DevQps I see, thanks! I will keep this PR open.