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