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