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

Simplify and encapsulate ReadDecoder implementation #523

Merged
merged 9 commits into from
Oct 21, 2024

Conversation

anforowicz
Copy link
Contributor

Hello!

This PR isn't strictly necessary for any major feature requests or bug fixes, but I think that this PR is still desirable - I think the resulting encapsulation helps with readability of the code.

I hope that splitting this PR into smaller, granular commits helps to follow along and review the changes.

PTAL?

Before this commit calling `Reader.finish` a 2nd time would return a
random, accidental error (`UnexpectedEof`), because of how
`StreamingDecoder`'s `state` is set after processing the `IEND` chunk.
After this commit, `Reader.finish` will handle this condition
explicitly, in a similar same way to how `next_frame` handles being
called when we have already consumed all the frames.
After a recent commit, all public APIs of a `Reader` take care of not
going beyond the `IEND` chunk and returning `PolledAfterEndOfImage`
instead.  This means that tracking `at_eof` at the level of
`ReadDecoder` is obsolete and leads to unnecessary complexity.  This
commit refactors away this complexity.
`ReadDecoder::finish_decoding` now reuses `decode_next` instead of
duplicating some of its code.  Some duplication (e.g. handling of
`Decoded::Nothing` remains - this will be taken care of in a subsequent
commit).
This commit means that `decode_next` can be a private method of
`ReadDecoder` (this is not enforced yet, before a subsequent commit
moves `ReadDecoder` into a separate `mod`ule).
Before this commit `fn decode_next` would `loop` to skip
`Decoded::Nothing` events.  This is unnecessary, because all the callers
of `decode_next` already account for `Decoded::Nothing` (explicitly or
implicitly via a wildcard).
This commit helps to keep some aspects of `ReadDecoder` private
(e.g. its fields, `decode_next` methods, etc.).  This commit
also means that `mod.rs` no longer directly depends on `Decoded`
nor `StreamingDecoder`.
@anforowicz anforowicz force-pushed the simplify-read-decoder-even-more branch from 8e0f284 to 2fb4b8e Compare October 10, 2024 00:35
@anforowicz
Copy link
Contributor Author

My appologies for the force push above. I've realized that in the last commit (when moving some of the code into the new file) I didn't preserve the comment tweaks that I've made in the earlier commits after a self-review...

Copy link
Contributor

@fintelia fintelia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a positive change

src/decoder/read_decoder.rs Outdated Show resolved Hide resolved
@kornelski kornelski merged commit 5212eee into image-rs:master Oct 21, 2024
18 checks passed
@kornelski
Copy link
Contributor

BTW, I've redesigned the same bits in the gif crate.

@anforowicz anforowicz deleted the simplify-read-decoder-even-more branch October 21, 2024 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants