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