Debian Codegraph @

src/futures/write/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 [`AsyncWrite`](futures_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::futures::write::Encoder<$inner, crate::codec::$name>,
12 }
13 }
14
15 impl<$inner: futures_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: futures_io::AsyncWrite> futures_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_close(
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_close(cx)
78 }
79 }
80
81 impl<$inner: futures_io::AsyncRead> futures_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 [u8]
86 ) -> std::task::Poll<std::io::Result<usize>> {
87 self.get_pin_mut().poll_read(cx, buf)
88 }
89
90 fn poll_read_vectored(
91 self: std::pin::Pin<&mut Self>,
92 cx: &mut std::task::Context<'_>,
93 bufs: &mut [futures_io::IoSliceMut<'_>]
94 ) -> std::task::Poll<std::io::Result<usize>> {
95 self.get_pin_mut().poll_read_vectored(cx, bufs)
96 }
97 }
98
99 impl<$inner: futures_io::AsyncBufRead> futures_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 fn _assert() {
114 use crate::util::{_assert_send, _assert_sync};
115 use core::pin::Pin;
116 use futures_io::AsyncWrite;
117
118 _assert_send::<$name<Pin<Box<dyn AsyncWrite + Send>>>>();
119 _assert_sync::<$name<Pin<Box<dyn AsyncWrite + Sync>>>>();
120 }
121 };
122 }
123 }