async_compression/tokio/bufread/macros/
encoder.rs

1macro_rules! encoder {
2    ($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($inherent_methods:tt)* })*) => {
3        pin_project_lite::pin_project! {
4            $(#[$attr])*
5            ///
6            /// This structure implements an [`AsyncRead`](tokio::io::AsyncRead) interface and will
7            /// read uncompressed data from an underlying stream and emit a stream of compressed data.
8            #[derive(Debug)]
9            pub struct $name<$inner> {
10                #[pin]
11                inner: crate::tokio::bufread::Encoder<$inner, crate::codecs::$name>,
12            }
13        }
14
15        impl<$inner: tokio::io::AsyncBufRead> $name<$inner> {
16            $(
17                /// Creates a new encoder which will read uncompressed data from the given stream
18                /// and emit an compressed stream.
19                ///
20                $($inherent_methods)*
21            )*
22
23            /// Creates a new encoder with the given codec, which will read uncompressed data from the given stream
24            /// and emit an compressed stream.
25            pub fn with_codec(read: $inner, codec: crate::codecs::$name) -> $name<$inner> {
26                $name {
27                   inner: crate::tokio::bufread::Encoder::new(read, codec)
28                }
29            }
30        }
31
32        impl<$inner> $name<$inner> {
33            /// Acquires a reference to the underlying reader that this encoder is wrapping.
34            pub fn get_ref(&self) -> &$inner {
35                self.inner.get_ref()
36            }
37
38            /// Acquires a mutable reference to the underlying reader that this encoder is
39            /// wrapping.
40            ///
41            /// Note that care must be taken to avoid tampering with the state of the reader which
42            /// may otherwise confuse this encoder.
43            pub fn get_mut(&mut self) -> &mut $inner {
44                self.inner.get_mut()
45            }
46
47            /// Acquires a pinned mutable reference to the underlying reader that this encoder is
48            /// wrapping.
49            ///
50            /// Note that care must be taken to avoid tampering with the state of the reader which
51            /// may otherwise confuse this encoder.
52            pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
53                self.project().inner.get_pin_mut()
54            }
55
56            /// Consumes this encoder returning the underlying reader.
57            ///
58            /// Note that this may discard internal state of this encoder, so care should be taken
59            /// to avoid losing resources when this is called.
60            pub fn into_inner(self) -> $inner {
61                self.inner.into_inner()
62            }
63        }
64
65        impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncRead for $name<$inner> {
66            fn poll_read(
67                self: std::pin::Pin<&mut Self>,
68                cx: &mut std::task::Context<'_>,
69                buf: &mut tokio::io::ReadBuf<'_>,
70            ) -> std::task::Poll<std::io::Result<()>> {
71                self.project().inner.poll_read(cx, buf)
72            }
73        }
74
75        impl<$inner: tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<$inner> {
76            fn poll_write(
77                self: std::pin::Pin<&mut Self>,
78                cx: &mut std::task::Context<'_>,
79                buf: &[u8],
80            ) -> std::task::Poll<std::io::Result<usize>> {
81                self.get_pin_mut().poll_write(cx, buf)
82            }
83
84            fn poll_flush(
85                self: std::pin::Pin<&mut Self>,
86                cx: &mut std::task::Context<'_>,
87            ) -> std::task::Poll<std::io::Result<()>> {
88                self.get_pin_mut().poll_flush(cx)
89            }
90
91            fn poll_shutdown(
92                self: std::pin::Pin<&mut Self>,
93                cx: &mut std::task::Context<'_>,
94            ) -> std::task::Poll<std::io::Result<()>> {
95                self.get_pin_mut().poll_shutdown(cx)
96            }
97
98            fn poll_write_vectored(
99                self: std::pin::Pin<&mut Self>,
100                cx: &mut std::task::Context<'_>,
101                bufs: &[std::io::IoSlice<'_>],
102            ) -> std::task::Poll<std::io::Result<usize>> {
103                self.get_pin_mut().poll_write_vectored(cx, bufs)
104            }
105
106            fn is_write_vectored(&self) -> bool {
107                self.get_ref().is_write_vectored()
108            }
109        }
110
111        const _: () = {
112            use crate::core::util::{_assert_send, _assert_sync};
113            use core::pin::Pin;
114            use tokio::io::AsyncBufRead;
115
116            _assert_send::<$name<Pin<Box<dyn AsyncBufRead + Send>>>>();
117            _assert_sync::<$name<Pin<Box<dyn AsyncBufRead + Sync>>>>();
118        };
119    }
120}