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