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