1 macro_rules! encoder { 2 ($(#[$attr:meta])* $name:ident<$inner:ident> $({ $($constructor:tt)* })*) => { 3 pin_project_lite::pin_project! { 4 $(#[$attr])* 5 #[derive(Debug)] 6 /// 7 /// This structure implements an [`AsyncWrite`](futures_io::AsyncWrite) interface and will 8 /// take in uncompressed data and write it compressed to an underlying stream. 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 $($constructor)* 21 )* 22 23 /// Acquires a reference to the underlying writer 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 writer that this encoder is 29 /// wrapping. 30 /// 31 /// Note that care must be taken to avoid tampering with the state of the writer 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 writer that this encoder is 38 /// wrapping. 39 /// 40 /// Note that care must be taken to avoid tampering with the state of the writer 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 writer. 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::AsyncWrite> futures_io::AsyncWrite for $name<$inner> { 56 fn poll_write( 57 self: std::pin::Pin<&mut Self>, 58 cx: &mut std::task::Context<'_>, 59 buf: &[u8], 60 ) -> std::task::Poll<std::io::Result<usize>> { 61 self.project().inner.poll_write(cx, buf) 62 } 63 64 fn poll_flush( 65 self: std::pin::Pin<&mut Self>, 66 cx: &mut std::task::Context<'_>, 67 ) -> std::task::Poll<std::io::Result<()>> { 68 self.project().inner.poll_flush(cx) 69 } 70 71 fn poll_close( 72 self: std::pin::Pin<&mut Self>, 73 cx: &mut std::task::Context<'_>, 74 ) -> std::task::Poll<std::io::Result<()>> { 75 self.project().inner.poll_close(cx) 76 } 77 } 78 79 const _: () = { 80 fn _assert() { 81 use crate::util::{_assert_send, _assert_sync}; 82 use core::pin::Pin; 83 use futures_io::AsyncWrite; 84 85 _assert_send::<$name<Pin<Box<dyn AsyncWrite + Send>>>>(); 86 _assert_sync::<$name<Pin<Box<dyn AsyncWrite + Sync>>>>(); 87 } 88 }; 89 } 90 } 91