1 macro_rules! decoder { 2 ($(#[$attr:meta])* $name:ident) => { 3 pin_project_lite::pin_project! { 4 $(#[$attr])* 5 #[derive(Debug)] 6 /// 7 /// This structure implements an [`AsyncRead`](futures_io::AsyncRead) interface and will 8 /// read compressed data from an underlying stream and emit a stream of uncompressed data. 9 pub struct $name<R> { 10 #[pin] 11 inner: crate::futures::bufread::Decoder<R, crate::codec::$name>, 12 } 13 } 14 15 impl<R: futures_io::AsyncBufRead> $name<R> { 16 /// Creates a new decoder which will read compressed data from the given stream and 17 /// emit a uncompressed stream. 18 pub fn new(read: R) -> $name<R> { 19 $name { 20 inner: crate::futures::bufread::Decoder::new(read, crate::codec::$name::new()), 21 } 22 } 23 24 /// Configure multi-member/frame decoding, if enabled this will reset the decoder state 25 /// when reaching the end of a compressed member/frame and expect either EOF or another 26 /// compressed member/frame to follow it in the stream. 27 pub fn multiple_members(&mut self, enabled: bool) { 28 self.inner.multiple_members(enabled); 29 } 30 31 /// Acquires a reference to the underlying reader that this decoder is wrapping. 32 pub fn get_ref(&self) -> &R { 33 self.inner.get_ref() 34 } 35 36 /// Acquires a mutable reference to the underlying reader that this decoder 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 decoder. 41 pub fn get_mut(&mut self) -> &mut R { 42 self.inner.get_mut() 43 } 44 45 /// Acquires a pinned mutable reference to the underlying reader that this decoder 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 decoder. 50 pub fn get_pin_mut(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut R> { 51 self.project().inner.get_pin_mut() 52 } 53 54 /// Consumes this decoder returning the underlying reader. 55 /// 56 /// Note that this may discard internal state of this decoder, so care should be taken 57 /// to avoid losing resources when this is called. 58 pub fn into_inner(self) -> R { 59 self.inner.into_inner() 60 } 61 } 62 63 impl<R: futures_io::AsyncBufRead> futures_io::AsyncRead for $name<R> { 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 const _: () = { 74 fn _assert() { 75 use crate::util::{_assert_send, _assert_sync}; 76 use core::pin::Pin; 77 use futures_io::AsyncBufRead; 78 79 _assert_send::<$name<Pin<Box<dyn AsyncBufRead + Send>>>>(); 80 _assert_sync::<$name<Pin<Box<dyn AsyncBufRead + Sync>>>>(); 81 } 82 }; 83 } 84 } 85