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