-
Notifications
You must be signed in to change notification settings - Fork 102
Deduplicate generic::bufread::Decoder impl of tokio/futures-io
#391
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
145 changes: 3 additions & 142 deletions
145
crates/async-compression/src/futures/bufread/generic/decoder.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
crates/async-compression/src/generic/bufread/decoder.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| use crate::codecs::Decode; | ||
| use crate::core::util::PartialBuffer; | ||
|
|
||
| use std::{io::Result, ops::ControlFlow}; | ||
|
|
||
| #[derive(Debug)] | ||
| enum State { | ||
| Decoding, | ||
| Flushing, | ||
| Done, | ||
| Next, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Decoder { | ||
| state: State, | ||
| multiple_members: bool, | ||
| } | ||
|
|
||
| impl Default for Decoder { | ||
| fn default() -> Self { | ||
| Self { | ||
| state: State::Decoding, | ||
| multiple_members: false, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Decoder { | ||
| pub fn multiple_members(&mut self, enabled: bool) { | ||
| self.multiple_members = enabled; | ||
| } | ||
|
|
||
| pub fn do_poll_read<D: Decode>( | ||
| &mut self, | ||
| output: &mut PartialBuffer<&mut [u8]>, | ||
| decoder: &mut D, | ||
| input: &mut PartialBuffer<&[u8]>, | ||
| mut first: bool, | ||
| ) -> ControlFlow<Result<()>> { | ||
| loop { | ||
| self.state = match self.state { | ||
| State::Decoding => { | ||
| if input.unwritten().is_empty() && !first { | ||
| // Avoid attempting to reinitialise the decoder if the | ||
| // reader has returned EOF. | ||
| self.multiple_members = false; | ||
|
|
||
| State::Flushing | ||
| } else { | ||
| match decoder.decode(input, output) { | ||
| Ok(true) => State::Flushing, | ||
| // ignore the first error, occurs when input is empty | ||
| // but we need to run decode to flush | ||
| Err(err) if !first => return ControlFlow::Break(Err(err)), | ||
| // poll for more data for the next decode | ||
| _ => break, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| State::Flushing => { | ||
| match decoder.finish(output) { | ||
| Ok(true) => { | ||
| if self.multiple_members { | ||
| if let Err(err) = decoder.reinit() { | ||
| return ControlFlow::Break(Err(err)); | ||
| } | ||
|
|
||
| // The decode stage might consume all the input, | ||
| // the next stage might need to poll again if it's empty. | ||
| first = true; | ||
| State::Next | ||
| } else { | ||
| State::Done | ||
| } | ||
| } | ||
| Ok(false) => State::Flushing, | ||
| Err(err) => return ControlFlow::Break(Err(err)), | ||
| } | ||
| } | ||
|
|
||
| State::Done => return ControlFlow::Break(Ok(())), | ||
|
|
||
| State::Next => { | ||
| if input.unwritten().is_empty() { | ||
| if first { | ||
| // poll for more data to check if there's another stream | ||
| break; | ||
| } | ||
| State::Done | ||
| } else { | ||
| State::Decoding | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if output.unwritten().is_empty() { | ||
| return ControlFlow::Break(Ok(())); | ||
| } | ||
| } | ||
|
|
||
| if output.unwritten().is_empty() { | ||
| ControlFlow::Break(Ok(())) | ||
| } else { | ||
| ControlFlow::Continue(()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| macro_rules! impl_do_poll_read { | ||
| () => { | ||
| use crate::generic::bufread::Decoder as GenericDecoder; | ||
|
|
||
| use std::ops::ControlFlow; | ||
|
|
||
| use futures_core::ready; | ||
| use pin_project_lite::pin_project; | ||
|
|
||
| pin_project! { | ||
| #[derive(Debug)] | ||
| pub struct Decoder<R, D> { | ||
| #[pin] | ||
| reader: R, | ||
| decoder: D, | ||
| inner: GenericDecoder, | ||
| } | ||
| } | ||
|
|
||
| impl<R: AsyncBufRead, D: Decode> Decoder<R, D> { | ||
| pub fn new(reader: R, decoder: D) -> Self { | ||
| Self { | ||
| reader, | ||
| decoder, | ||
| inner: GenericDecoder::default(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<R, D> Decoder<R, D> { | ||
| pub fn get_ref(&self) -> &R { | ||
| &self.reader | ||
| } | ||
|
|
||
| pub fn get_mut(&mut self) -> &mut R { | ||
| &mut self.reader | ||
| } | ||
|
|
||
| pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R> { | ||
| self.project().reader | ||
| } | ||
|
|
||
| pub fn into_inner(self) -> R { | ||
| self.reader | ||
| } | ||
|
|
||
| pub fn multiple_members(&mut self, enabled: bool) { | ||
| self.inner.multiple_members(enabled); | ||
| } | ||
| } | ||
|
|
||
| impl<R: AsyncBufRead, D: Decode> Decoder<R, D> { | ||
| fn do_poll_read( | ||
| self: Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| output: &mut PartialBuffer<&mut [u8]>, | ||
| ) -> Poll<Result<()>> { | ||
| let mut this = self.project(); | ||
|
|
||
| if let ControlFlow::Break(res) = this.inner.do_poll_read( | ||
| output, | ||
| this.decoder, | ||
| &mut PartialBuffer::new(&[][..]), | ||
| true, | ||
| ) { | ||
| return Poll::Ready(res); | ||
| } | ||
|
|
||
| loop { | ||
| let mut input = | ||
| PartialBuffer::new(ready!(this.reader.as_mut().poll_fill_buf(cx))?); | ||
|
|
||
| let control_flow = | ||
| this.inner | ||
| .do_poll_read(output, this.decoder, &mut input, false); | ||
|
|
||
| let bytes_read = input.written().len(); | ||
| this.reader.as_mut().consume(bytes_read); | ||
|
|
||
| if let ControlFlow::Break(res) = control_flow { | ||
| break Poll::Ready(res); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| pub(crate) use impl_do_poll_read; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| mod decoder; | ||
|
|
||
| pub(crate) use decoder::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub(crate) mod bufread; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @robjtede I decided to keep the state as simple as possible, anything else can be passed by parameter