async_compression/tokio/bufread/macros/
decoder.rs

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