Skip to content
Merged
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
190 changes: 3 additions & 187 deletions crates/async-compression/src/futures/write/generic/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,186 +1,12 @@
use crate::{futures::write::BufWriter, generic::write::impl_encoder};
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSliceMut};
use std::{
io,
pin::Pin,
task::{Context, Poll},
};

use crate::codecs::Encode;
use crate::core::util::PartialBuffer;
use crate::futures::write::{AsyncBufWrite, BufWriter};
use futures_core::ready;
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSliceMut};
use pin_project_lite::pin_project;

#[derive(Debug)]
enum State {
Encoding,
Finishing,
Done,
}

pin_project! {
#[derive(Debug)]
pub struct Encoder<W, E> {
#[pin]
writer: BufWriter<W>,
encoder: E,
state: State,
}
}

impl<W, E> Encoder<W, E> {
pub fn get_ref(&self) -> &W {
self.writer.get_ref()
}

pub fn get_mut(&mut self) -> &mut W {
self.writer.get_mut()
}

pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
self.project().writer.get_pin_mut()
}

pub(crate) fn get_encoder_ref(&self) -> &E {
&self.encoder
}

pub fn into_inner(self) -> W {
self.writer.into_inner()
}
}

impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
pub fn new(writer: W, encoder: E) -> Self {
Self {
writer: BufWriter::new(writer),
encoder,
state: State::Encoding,
}
}

pub fn with_capacity(writer: W, encoder: E, cap: usize) -> Self {
Self {
writer: BufWriter::with_capacity(cap, writer),
encoder,
state: State::Encoding,
}
}

fn do_poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
input: &mut PartialBuffer<&[u8]>,
) -> Poll<io::Result<()>> {
let mut this = self.project();

loop {
let output = ready!(this.writer.as_mut().poll_partial_flush_buf(cx))?;
let mut output = PartialBuffer::new(output);

*this.state = match this.state {
State::Encoding => {
this.encoder.encode(input, &mut output)?;
State::Encoding
}

State::Finishing | State::Done => {
return Poll::Ready(Err(io::Error::other("Write after close")))
}
};

let produced = output.written().len();
this.writer.as_mut().produce(produced);

if input.unwritten().is_empty() {
return Poll::Ready(Ok(()));
}
}
}

fn do_poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let mut this = self.project();

loop {
let output = ready!(this.writer.as_mut().poll_partial_flush_buf(cx))?;
let mut output = PartialBuffer::new(output);

let done = match this.state {
State::Encoding => this.encoder.flush(&mut output)?,

State::Finishing | State::Done => {
return Poll::Ready(Err(io::Error::other("Flush after close")))
}
};

let produced = output.written().len();
this.writer.as_mut().produce(produced);

if done {
return Poll::Ready(Ok(()));
}
}
}

fn do_poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let mut this = self.project();

loop {
let output = ready!(this.writer.as_mut().poll_partial_flush_buf(cx))?;
let mut output = PartialBuffer::new(output);

*this.state = match this.state {
State::Encoding | State::Finishing => {
if this.encoder.finish(&mut output)? {
State::Done
} else {
State::Finishing
}
}

State::Done => State::Done,
};

let produced = output.written().len();
this.writer.as_mut().produce(produced);

if let State::Done = this.state {
return Poll::Ready(Ok(()));
}
}
}
}

impl<W: AsyncWrite, E: Encode> AsyncWrite for Encoder<W, E> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
if buf.is_empty() {
return Poll::Ready(Ok(0));
}

let mut input = PartialBuffer::new(buf);

match self.do_poll_write(cx, &mut input)? {
Poll::Pending if input.written().is_empty() => Poll::Pending,
_ => Poll::Ready(Ok(input.written().len())),
}
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
ready!(self.as_mut().do_poll_flush(cx))?;
ready!(self.project().writer.as_mut().poll_flush(cx))?;
Poll::Ready(Ok(()))
}

fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
ready!(self.as_mut().do_poll_close(cx))?;
ready!(self.project().writer.as_mut().poll_close(cx))?;
Poll::Ready(Ok(()))
}
}
impl_encoder!(poll_close);

impl<W: AsyncRead, E> AsyncRead for Encoder<W, E> {
fn poll_read(
Expand All @@ -199,13 +25,3 @@ impl<W: AsyncRead, E> AsyncRead for Encoder<W, E> {
self.get_pin_mut().poll_read_vectored(cx, bufs)
}
}

impl<W: AsyncBufRead, E> AsyncBufRead for Encoder<W, E> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}
49 changes: 24 additions & 25 deletions crates/async-compression/src/generic/write/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Default for Decoder {
}

impl Decoder {
pub fn do_poll_write(
fn do_poll_write(
&mut self,
cx: &mut Context<'_>,
input: &mut PartialBuffer<&[u8]>,
Expand Down Expand Up @@ -73,6 +73,25 @@ impl Decoder {
}
}

pub fn poll_write(
&mut self,
cx: &mut Context<'_>,
buf: &[u8],
writer: Pin<&mut dyn AsyncBufWrite>,
decoder: &mut impl Decode,
) -> Poll<io::Result<usize>> {
if buf.is_empty() {
return Poll::Ready(Ok(0));
}

let mut input = PartialBuffer::new(buf);

match self.do_poll_write(cx, &mut input, writer, decoder)? {
Poll::Pending if input.written().is_empty() => Poll::Pending,
_ => Poll::Ready(Ok(input.written().len())),
}
}

pub fn do_poll_flush(
&mut self,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -169,17 +188,6 @@ macro_rules! impl_decoder {
}

impl<W: AsyncWrite, D: Decode> Decoder<W, D> {
fn do_poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
input: &mut PartialBuffer<&[u8]>,
) -> Poll<io::Result<()>> {
let mut this = self.project();

this.inner
.do_poll_write(cx, input, this.writer, this.decoder)
}

fn do_poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let mut this = self.project();

Expand All @@ -193,22 +201,14 @@ macro_rules! impl_decoder {
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
if buf.is_empty() {
return Poll::Ready(Ok(0));
}

let mut input = PartialBuffer::new(buf);
let mut this = self.project();

match self.do_poll_write(cx, &mut input)? {
Poll::Pending if input.written().is_empty() => Poll::Pending,
_ => Poll::Ready(Ok(input.written().len())),
}
this.inner.poll_write(cx, buf, this.writer, this.decoder)
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
ready!(self.as_mut().do_poll_flush(cx))?;
ready!(self.project().writer.as_mut().poll_flush(cx))?;
Poll::Ready(Ok(()))
self.project().writer.poll_flush(cx)
}

fn $poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Expand All @@ -218,8 +218,7 @@ macro_rules! impl_decoder {

let this = self.project();
if this.inner.is_done() {
ready!(this.writer.$poll_close(cx))?;
Poll::Ready(Ok(()))
this.writer.$poll_close(cx)
} else {
Poll::Ready(Err(io::Error::other(
"Attempt to close before finishing input",
Expand Down
Loading