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