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