Skip to content
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

A way to pause StreamWriter #267

Open
HeroicKatora opened this issue Dec 8, 2020 · 0 comments
Open

A way to pause StreamWriter #267

HeroicKatora opened this issue Dec 8, 2020 · 0 comments

Comments

@HeroicKatora
Copy link
Member

Currently, StreamWriter either borrows or takes ownership of the underlying writer when it is constructed. There is no way to rescind that borrow while retaining the current encoding state, e.g. to get access to the underlying encoder or to remove the borrow. Also dropping the writer will always execute some amount of IO-code. Since Rust checks borrow at compile time using scopes, that leads to the unfortunate situation where you easily can't store the StreamWriter next to the struct it was borrowed from and encapsulate the stream writer state further except by choosing the 'static, owned version which you can't back out of and which consumes the raw png encoder.

One idea was to introduce a StreamWriter::pause method and a complementary continue function. They would be similar to into_raw/from_raw pairs in that the caller should uphold the proper usage of their values but safe since we 'merely' protect the written image from being faulty, not a memory safety invariant.

impl<W> StreamWriter<'_, W> {
    fn pause(self) -> PartialChunkData;
}
impl<W> Encoder<W> {
    fn resume(&mut self, _: PartialChunkData) -> StreamWriter<'_, W>
}

// Potentially:
impl<W> StreamWriter<'static, W> {
    fn resume(writer: W, PartialChunkData);
}

The main problem here is the underlying zlib stream. It contains some amount of state that must be preserved to ensure that at least proper usage leads to a functional image. However, it also wraps the stream and thus must be unwrapped or saved itself. It's not clear how to achieve this with deflate, yet the switch to miniz_oxide for encoding could probably achieve the effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant