| 1 |
use std::{ |
| 2 |
io, |
| 3 |
pin::Pin, |
| 4 |
task::{Context, Poll}, |
| 5 |
}; |
| 6 |
|
| 7 |
use crate::{ |
| 8 |
codec::Encode, |
| 9 |
futures::write::{AsyncBufWrite, BufWriter}, |
| 10 |
util::PartialBuffer, |
| 11 |
}; |
| 12 |
use futures_core::ready; |
| 13 |
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSliceMut}; |
| 14 |
use pin_project_lite::pin_project; |
| 15 |
|
| 16 |
#[derive(Debug)] |
| 17 |
enum State { |
| 18 |
Encoding, |
| 19 |
Finishing, |
| 20 |
Done, |
| 21 |
} |
| 22 |
|
| 23 |
pin_project! { |
| 24 |
#[derive(Debug)] |
| 25 |
pub struct Encoder<W, E> { |
| 26 |
#[pin] |
| 27 |
writer: BufWriter<W>, |
| 28 |
encoder: E, |
| 29 |
state: State, |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl<W, E> Encoder<W, E> { |
| 34 |
pub fn get_ref(&self) -> &W { |
| 35 |
self.writer.get_ref() |
| 36 |
} |
| 37 |
|
| 38 |
pub fn get_mut(&mut self) -> &mut W { |
| 39 |
self.writer.get_mut() |
| 40 |
} |
| 41 |
|
| 42 |
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> { |
| 43 |
self.project().writer.get_pin_mut() |
| 44 |
} |
| 45 |
|
| 46 |
pub(crate) fn get_encoder_ref(&self) -> &E { |
| 47 |
&self.encoder |
| 48 |
} |
| 49 |
|
| 50 |
pub fn into_inner(self) -> W { |
| 51 |
self.writer.into_inner() |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
impl<W: AsyncWrite, E: Encode> Encoder<W, E> { |
| 56 |
pub fn new(writer: W, encoder: E) -> Self { |
| 57 |
Self { |
| 58 |
writer: BufWriter::new(writer), |
| 59 |
encoder, |
| 60 |
state: State::Encoding, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
pub fn with_capacity(writer: W, encoder: E, cap: usize) -> Self { |
| 65 |
Self { |
| 66 |
writer: BufWriter::with_capacity(cap, writer), |
| 67 |
encoder, |
| 68 |
state: State::Encoding, |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
fn do_poll_write( |
| 73 |
self: Pin<&mut Self>, |
| 74 |
cx: &mut Context<'_>, |
| 75 |
input: &mut PartialBuffer<&[u8]>, |
| 76 |
) -> Poll<io::Result<()>> { |
| 77 |
let mut this = self.project(); |
| 78 |
|
| 79 |
loop { |
| 80 |
let output = ready!(this.writer.as_mut().poll_partial_flush_buf(cx))?; |
| 81 |
let mut output = PartialBuffer::new(output); |
| 82 |
|
| 83 |
*this.state = match this.state { |
| 84 |
State::Encoding => { |
| 85 |
this.encoder.encode(input, &mut output)?; |
| 86 |
State::Encoding |
| 87 |
} |
| 88 |
|
| 89 |
State::Finishing | State::Done => { |
| 90 |
return Poll::Ready(Err(io::Error::other("Write after close"))) |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
let produced = output.written().len(); |
| 95 |
this.writer.as_mut().produce(produced); |
| 96 |
|
| 97 |
if input.unwritten().is_empty() { |
| 98 |
return Poll::Ready(Ok(())); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
fn do_poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 104 |
let mut this = self.project(); |
| 105 |
|
| 106 |
loop { |
| 107 |
let output = ready!(this.writer.as_mut().poll_partial_flush_buf(cx))?; |
| 108 |
let mut output = PartialBuffer::new(output); |
| 109 |
|
| 110 |
let done = match this.state { |
| 111 |
State::Encoding => this.encoder.flush(&mut output)?, |
| 112 |
|
| 113 |
State::Finishing | State::Done => { |
| 114 |
return Poll::Ready(Err(io::Error::other("Flush after close"))) |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
let produced = output.written().len(); |
| 119 |
this.writer.as_mut().produce(produced); |
| 120 |
|
| 121 |
if done { |
| 122 |
return Poll::Ready(Ok(())); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
fn do_poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 128 |
let mut this = self.project(); |
| 129 |
|
| 130 |
loop { |
| 131 |
let output = ready!(this.writer.as_mut().poll_partial_flush_buf(cx))?; |
| 132 |
let mut output = PartialBuffer::new(output); |
| 133 |
|
| 134 |
*this.state = match this.state { |
| 135 |
State::Encoding | State::Finishing => { |
| 136 |
if this.encoder.finish(&mut output)? { |
| 137 |
State::Done |
| 138 |
} else { |
| 139 |
State::Finishing |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
State::Done => State::Done, |
| 144 |
}; |
| 145 |
|
| 146 |
let produced = output.written().len(); |
| 147 |
this.writer.as_mut().produce(produced); |
| 148 |
|
| 149 |
if let State::Done = this.state { |
| 150 |
return Poll::Ready(Ok(())); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl<W: AsyncWrite, E: Encode> AsyncWrite for Encoder<W, E> { |
| 157 |
fn poll_write( |
| 158 |
self: Pin<&mut Self>, |
| 159 |
cx: &mut Context<'_>, |
| 160 |
buf: &[u8], |
| 161 |
) -> Poll<io::Result<usize>> { |
| 162 |
if buf.is_empty() { |
| 163 |
return Poll::Ready(Ok(0)); |
| 164 |
} |
| 165 |
|
| 166 |
let mut input = PartialBuffer::new(buf); |
| 167 |
|
| 168 |
match self.do_poll_write(cx, &mut input)? { |
| 169 |
Poll::Pending if input.written().is_empty() => Poll::Pending, |
| 170 |
_ => Poll::Ready(Ok(input.written().len())), |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 175 |
ready!(self.as_mut().do_poll_flush(cx))?; |
| 176 |
ready!(self.project().writer.as_mut().poll_flush(cx))?; |
| 177 |
Poll::Ready(Ok(())) |
| 178 |
} |
| 179 |
|
| 180 |
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 181 |
ready!(self.as_mut().do_poll_close(cx))?; |
| 182 |
ready!(self.project().writer.as_mut().poll_close(cx))?; |
| 183 |
Poll::Ready(Ok(())) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
impl<W: AsyncRead, E> AsyncRead for Encoder<W, E> { |
| 188 |
fn poll_read( |
| 189 |
self: Pin<&mut Self>, |
| 190 |
cx: &mut Context<'_>, |
| 191 |
buf: &mut [u8], |
| 192 |
) -> Poll<io::Result<usize>> { |
| 193 |
self.get_pin_mut().poll_read(cx, buf) |
| 194 |
} |
| 195 |
|
| 196 |
fn poll_read_vectored( |
| 197 |
self: Pin<&mut Self>, |
| 198 |
cx: &mut Context<'_>, |
| 199 |
bufs: &mut [IoSliceMut<'_>], |
| 200 |
) -> Poll<io::Result<usize>> { |
| 201 |
self.get_pin_mut().poll_read_vectored(cx, bufs) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl<W: AsyncBufRead, E> AsyncBufRead for Encoder<W, E> { |
| 206 |
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> { |
| 207 |
self.get_pin_mut().poll_fill_buf(cx) |
| 208 |
} |
| 209 |
|
| 210 |
fn consume(self: Pin<&mut Self>, amt: usize) { |
| 211 |
self.get_pin_mut().consume(amt) |
| 212 |
} |
| 213 |
} |