Debian Codegraph @

src/futures/bufread/macros/encoder.rs

raw
1 macro_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::codec::$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 a compressed stream.
19 ///
20 $($inherent_methods)*
21 )*
22
23 /// Acquires a reference to the underlying reader that this encoder is wrapping.
24 pub fn get_ref(&self) -> &$inner {
25 self.inner.get_ref()
26 }
27
28 /// Acquires a mutable reference to the underlying reader that this encoder is
29 /// wrapping.
30 ///
31 /// Note that care must be taken to avoid tampering with the state of the reader which
32 /// may otherwise confuse this encoder.
33 pub fn get_mut(&mut self) -> &mut $inner {
34 self.inner.get_mut()
35 }
36
37 /// Acquires a pinned mutable reference to the underlying reader that this encoder is
38 /// wrapping.
39 ///
40 /// Note that care must be taken to avoid tampering with the state of the reader which
41 /// may otherwise confuse this encoder.
42 pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut $inner> {
43 self.project().inner.get_pin_mut()
44 }
45
46 /// Consumes this encoder returning the underlying reader.
47 ///
48 /// Note that this may discard internal state of this encoder, so care should be taken
49 /// to avoid losing resources when this is called.
50 pub fn into_inner(self) -> $inner {
51 self.inner.into_inner()
52 }
53 }
54
55 impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<$inner> {
56 fn poll_read(
57 self: std::pin::Pin<&mut Self>,
58 cx: &mut std::task::Context<'_>,
59 buf: &mut [u8],
60 ) -> std::task::Poll<std::io::Result<usize>> {
61 self.project().inner.poll_read(cx, buf)
62 }
63 }
64
65 impl<$inner: futures_io::AsyncWrite> futures_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_close(
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_close(cx)
86 }
87
88 fn poll_write_vectored(
89 self: std::pin::Pin<&mut Self>,
90 cx: &mut std::task::Context<'_>,
91 bufs: &[std::io::IoSlice<'_>]
92 ) -> std::task::Poll<std::io::Result<usize>> {
93 self.project().inner.poll_write_vectored(cx, bufs)
94 }
95 }
96
97 const _: () = {
98 fn _assert() {
99 use crate::util::{_assert_send, _assert_sync};
100 use core::pin::Pin;
101 use futures_io::AsyncBufRead;
102
103 _assert_send::<$name<Pin<Box<dyn AsyncBufRead + Send>>>>();
104 _assert_sync::<$name<Pin<Box<dyn AsyncBufRead + Sync>>>>();
105 }
106 };
107 }
108 }