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

support intermediate flushes when encoding #155

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/codec/flate/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ impl Encode for FlateEncoder {
FlushCompress::Sync,
)?;

loop {
let old_len = output.written().len();
self.encode(
&mut PartialBuffer::new(&[][..]),
output,
FlushCompress::None,
)?;
if output.written().len() == old_len {
break;
}
}

self.flushed = true;
Ok(!output.unwritten().is_empty())
}
Expand Down
49 changes: 40 additions & 9 deletions src/tokio/bufread/generic/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tokio::io::{AsyncBufRead, AsyncRead, ReadBuf};
enum State {
Encoding,
Flushing,
Finishing,
Done,
}

Expand Down Expand Up @@ -57,27 +58,56 @@ impl<R: AsyncBufRead, E: Encode> Encoder<R, E> {
output: &mut PartialBuffer<&mut [u8]>,
) -> Poll<Result<()>> {
let mut this = self.project();
let mut read = 0usize;

loop {
*this.state = match this.state {
State::Encoding => {
let input = ready!(this.reader.as_mut().poll_fill_buf(cx))?;
if input.is_empty() {
State::Flushing
} else {
let mut input = PartialBuffer::new(input);
let res = this.reader.as_mut().poll_fill_buf(cx);

match res {
Poll::Pending => {
if read == 0 {
return Poll::Pending;
} else {
State::Flushing
}
}
Poll::Ready(res) => {
let input = res?;

if input.is_empty() {
State::Finishing
} else {
let mut input = PartialBuffer::new(input);
this.encoder.encode(&mut input, output)?;
let len = input.written().len();
this.reader.as_mut().consume(len);
read += len;
State::Encoding
}
}
}
}

State::Flushing => {
if read == 0 {
let mut input = PartialBuffer::new(&[][..]);
this.encoder.encode(&mut input, output)?;
let len = input.written().len();
this.reader.as_mut().consume(len);
}

if this.encoder.flush(output)? {
State::Encoding
} else {
State::Flushing
}
}

State::Flushing => {
State::Finishing => {
if this.encoder.finish(output)? {
State::Done
} else {
State::Flushing
State::Finishing
}
}

Expand All @@ -87,6 +117,7 @@ impl<R: AsyncBufRead, E: Encode> Encoder<R, E> {
if let State::Done = *this.state {
return Poll::Ready(Ok(()));
}

if output.unwritten().is_empty() {
return Poll::Ready(Ok(()));
}
Expand Down