libflate icon indicating copy to clipboard operation
libflate copied to clipboard

impl Drop

Open goertzenator opened this issue 6 years ago • 10 comments

The Encoders 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.

goertzenator avatar Dec 22 '17 16:12 goertzenator

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).

sile avatar Dec 24 '17 05:12 sile

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))
}

sile avatar Dec 25 '17 16:12 sile

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.

goertzenator avatar Dec 25 '17 20:12 goertzenator

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.

sile avatar Jan 02 '18 14:01 sile

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 avatar Mar 13 '19 23:03 DevQps

@DevQps Thanks for your question. This feature has not been merged into master branch.

sile avatar Mar 16 '19 02:03 sile

@sile Alright! Thanks for informing us! If there is anything else I can do, feel free to contact!

DevQps avatar Mar 16 '19 08:03 DevQps

@DevQps To be honest, I forgot about this feature. If you need it, I will consider it again, but what about it?

sile avatar Mar 17 '19 02:03 sile

@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 avatar Mar 17 '19 09:03 DevQps

@DevQps I see, thanks! I will keep this PR open.

sile avatar Mar 17 '19 11:03 sile