async_compression/tokio/bufread/generic/
encoder.rs

1use crate::{
2    codecs::EncodeV2,
3    core::util::{PartialBuffer, WriteBuffer},
4    generic::bufread::impl_encoder,
5};
6use std::{
7    io::{IoSlice, Result},
8    pin::Pin,
9    task::{Context, Poll},
10};
11use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
12
13impl_encoder!();
14
15impl<R: AsyncBufRead, E: EncodeV2> AsyncRead for Encoder<R, E> {
16    fn poll_read(
17        self: Pin<&mut Self>,
18        cx: &mut Context<'_>,
19        buf: &mut ReadBuf<'_>,
20    ) -> Poll<Result<()>> {
21        if buf.remaining() == 0 {
22            return Poll::Ready(Ok(()));
23        }
24
25        let mut output = WriteBuffer::new_initialized(buf.initialize_unfilled());
26        match self.do_poll_read(cx, &mut output)? {
27            Poll::Pending if output.written().is_empty() => Poll::Pending,
28            _ => {
29                let len = output.written_len();
30                buf.advance(len);
31                Poll::Ready(Ok(()))
32            }
33        }
34    }
35}
36
37impl<R: AsyncWrite, E> AsyncWrite for Encoder<R, E> {
38    fn poll_write(
39        mut self: Pin<&mut Self>,
40        cx: &mut Context<'_>,
41        buf: &[u8],
42    ) -> Poll<Result<usize>> {
43        self.get_pin_mut().poll_write(cx, buf)
44    }
45
46    fn poll_write_vectored(
47        mut self: Pin<&mut Self>,
48        cx: &mut Context<'_>,
49        mut bufs: &[IoSlice<'_>],
50    ) -> Poll<Result<usize>> {
51        self.get_pin_mut().poll_write_vectored(cx, bufs)
52    }
53
54    fn is_write_vectored(&self) -> bool {
55        self.get_ref().is_write_vectored()
56    }
57
58    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
59        self.get_pin_mut().poll_flush(cx)
60    }
61
62    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
63        self.get_pin_mut().poll_shutdown(cx)
64    }
65}