-
Notifications
You must be signed in to change notification settings - Fork 79
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
finish can be problematic #9
Comments
Writer
You can workaround this by code like: struct Wrapper<W: Write> {
s: Option<Encoder<W>>,
}
impl<W: Write> Write for Wrapper<W> {
fn write(&mut self, buffer: &[u8]) -> Result<usize> {
self.s.as_mut().unwrap().write(buffer)
}
fn flush(&mut self) -> Result<()> {
self.s.as_mut().unwrap().flush()
}
}
impl<W: Write> Drop for Wrapper<W> {
fn drop(&mut self) {
match self.s.take() {
Some(s) => {s.finish();}
None => {}
}
}
}
I make this feature because I can. I don't know how this problem solved in another encoders. |
Thanks. This Wrapper would work, indeed. I have had a look at other implementations (std::io::BufWriter, flate2, snappy_framed, which are the ones I need to switch). The four of them are handling thing differently:
Do we need a WriterWrapper trait in stdlib ? cc @alexcrichton |
I create rust-lang/rust#32625 |
I try with RFC: rust-lang/rfcs#1568 |
Just hit this issue. Problem for me happens when I try to use this with other libraries, like slog, which take input writers as Seems a bit strange that the |
Running this workaround on Rust version 0.40. throws an error (possibly due to updated error checking).
Packages calling this will not compile unless this is added above the "struct". As a workaround, this will allow it to compile with a warning, although error handling would be ideal.
|
Hello,
I ran into a problem while using Encoder in a context where it has to be switchable for other Write implémentation : the finish method has to be called to send the last bytes to the inner writer. Other Write implementation for similar purpose (like flate2 or snappy framed) will just flush the last page on drop().
Working around it is tricky: as finish() consumes self, you can not make a trivial wrapper implementing Drop around the Encoder because drop only has a &mut self...
And, you can not just add drop() to the implementation alongside finish(), because finish() returns the inner Write. I just think we can't have both, unless we have two variants of Encoder.
I see the point of being able to get the inner Write back, but I'm wondering if this feature is preferable to compatibility with other encoder (and safety, as calling drop() can not be forgotten).
What do you think ?
The text was updated successfully, but these errors were encountered: