Debian Codegraph @

src/futures/bufread/generic/encoder.rs

raw
1 use core::{
2 pin::Pin,
3 task::{Context, Poll},
4 };
5 use std::io::Result;
6
7 use crate::{codec::Encode, util::PartialBuffer};
8 use futures_core::ready;
9 use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSlice};
10 use pin_project_lite::pin_project;
11
12 #[derive(Debug)]
13 enum State {
14 Encoding,
15 Flushing,
16 Done,
17 }
18
19 pin_project! {
20 #[derive(Debug)]
21 pub struct Encoder<R, E> {
22 #[pin]
23 reader: R,
24 encoder: E,
25 state: State,
26 }
27 }
28
29 impl<R: AsyncBufRead, E: Encode> Encoder<R, E> {
30 pub fn new(reader: R, encoder: E) -> Self {
31 Self {
32 reader,
33 encoder,
34 state: State::Encoding,
35 }
36 }
37
38 pub fn with_capacity(reader: R, encoder: E, _cap: usize) -> Self {
39 Self::new(reader, encoder)
40 }
41 }
42
43 impl<R, E> Encoder<R, E> {
44 pub fn get_ref(&self) -> &R {
45 &self.reader
46 }
47
48 pub fn get_mut(&mut self) -> &mut R {
49 &mut self.reader
50 }
51
52 pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R> {
53 self.project().reader
54 }
55
56 pub(crate) fn get_encoder_ref(&self) -> &E {
57 &self.encoder
58 }
59
60 pub fn into_inner(self) -> R {
61 self.reader
62 }
63 }
64
65 impl<R: AsyncBufRead, E: Encode> Encoder<R, E> {
66 fn do_poll_read(
67 self: Pin<&mut Self>,
68 cx: &mut Context<'_>,
69 output: &mut PartialBuffer<&mut [u8]>,
70 ) -> Poll<Result<()>> {
71 let mut this = self.project();
72
73 loop {
74 *this.state = match this.state {
75 State::Encoding => {
76 let input = ready!(this.reader.as_mut().poll_fill_buf(cx))?;
77 if input.is_empty() {
78 State::Flushing
79 } else {
80 let mut input = PartialBuffer::new(input);
81 this.encoder.encode(&mut input, output)?;
82 let len = input.written().len();
83 this.reader.as_mut().consume(len);
84 State::Encoding
85 }
86 }
87
88 State::Flushing => {
89 if this.encoder.finish(output)? {
90 State::Done
91 } else {
92 State::Flushing
93 }
94 }
95
96 State::Done => State::Done,
97 };
98
99 if let State::Done = *this.state {
100 return Poll::Ready(Ok(()));
101 }
102 if output.unwritten().is_empty() {
103 return Poll::Ready(Ok(()));
104 }
105 }
106 }
107 }
108
109 impl<R: AsyncBufRead, E: Encode> AsyncRead for Encoder<R, E> {
110 fn poll_read(
111 self: Pin<&mut Self>,
112 cx: &mut Context<'_>,
113 buf: &mut [u8],
114 ) -> Poll<Result<usize>> {
115 if buf.is_empty() {
116 return Poll::Ready(Ok(0));
117 }
118
119 let mut output = PartialBuffer::new(buf);
120 match self.do_poll_read(cx, &mut output)? {
121 Poll::Pending if output.written().is_empty() => Poll::Pending,
122 _ => Poll::Ready(Ok(output.written().len())),
123 }
124 }
125 }
126
127 impl<R: AsyncWrite, E> AsyncWrite for Encoder<R, E> {
128 fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
129 self.get_pin_mut().poll_write(cx, buf)
130 }
131
132 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
133 self.get_pin_mut().poll_flush(cx)
134 }
135
136 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
137 self.get_pin_mut().poll_close(cx)
138 }
139
140 fn poll_write_vectored(
141 self: Pin<&mut Self>,
142 cx: &mut Context<'_>,
143 bufs: &[IoSlice<'_>],
144 ) -> Poll<Result<usize>> {
145 self.get_pin_mut().poll_write_vectored(cx, bufs)
146 }
147 }