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::codec::$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 a compressed stream.
19                ///
20                $($inherent_methods)*
21            )*
22        }
23
24        impl<$inner> $name<$inner> {
25            /// Acquires a reference to the underlying reader that this encoder is wrapping.
26            pub fn get_ref(&self) -> &$inner {
27                self.inner.get_ref()
28            }
29
30            /// Acquires a mutable reference to the underlying reader that this encoder is
31            /// wrapping.
32            ///
33            /// Note that care must be taken to avoid tampering with the state of the reader which
34            /// may otherwise confuse this encoder.
35            pub fn get_mut(&mut self) -> &mut $inner {
36                self.inner.get_mut()
37            }
38
39            /// Acquires a pinned mutable reference to the underlying reader that this encoder is
40            /// wrapping.
41            ///
42            /// Note that care must be taken to avoid tampering with the state of the reader which
43            /// may otherwise confuse this encoder.
44            pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
45                self.project().inner.get_pin_mut()
46            }
47
48            /// Consumes this encoder returning the underlying reader.
49            ///
50            /// Note that this may discard internal state of this encoder, so care should be taken
51            /// to avoid losing resources when this is called.
52            pub fn into_inner(self) -> $inner {
53                self.inner.into_inner()
54            }
55        }
56
57        impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncRead for $name<$inner> {
58            fn poll_read(
59                self: std::pin::Pin<&mut Self>,
60                cx: &mut std::task::Context<'_>,
61                buf: &mut tokio::io::ReadBuf<'_>,
62            ) -> std::task::Poll<std::io::Result<()>> {
63                self.project().inner.poll_read(cx, buf)
64            }
65        }
66
67        impl<$inner: tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<$inner> {
68            fn poll_write(
69                self: std::pin::Pin<&mut Self>,
70                cx: &mut std::task::Context<'_>,
71                buf: &[u8],
72            ) -> std::task::Poll<std::io::Result<usize>> {
73                self.get_pin_mut().poll_write(cx, buf)
74            }
75
76            fn poll_flush(
77                self: std::pin::Pin<&mut Self>,
78                cx: &mut std::task::Context<'_>,
79            ) -> std::task::Poll<std::io::Result<()>> {
80                self.get_pin_mut().poll_flush(cx)
81            }
82
83            fn poll_shutdown(
84                self: std::pin::Pin<&mut Self>,
85                cx: &mut std::task::Context<'_>,
86            ) -> std::task::Poll<std::io::Result<()>> {
87                self.get_pin_mut().poll_shutdown(cx)
88            }
89
90            fn poll_write_vectored(
91                self: std::pin::Pin<&mut Self>,
92                cx: &mut std::task::Context<'_>,
93                bufs: &[std::io::IoSlice<'_>],
94            ) -> std::task::Poll<std::io::Result<usize>> {
95                self.get_pin_mut().poll_write_vectored(cx, bufs)
96            }
97
98            fn is_write_vectored(&self) -> bool {
99                self.get_ref().is_write_vectored()
100            }
101        }
102
103        const _: () = {
104            fn _assert() {
105                use crate::util::{_assert_send, _assert_sync};
106                use core::pin::Pin;
107                use tokio::io::AsyncBufRead;
108
109                _assert_send::<$name<Pin<Box<dyn AsyncBufRead + Send>>>>();
110                _assert_sync::<$name<Pin<Box<dyn AsyncBufRead + Sync>>>>();
111            }
112        };
113    }
114}