|
1 | 1 | use crate::{
|
2 | 2 | decode,
|
3 | 3 | immutable::{Band, Text},
|
4 |
| - PacketLine, StreamingPeekableIter, U16_HEX_BYTES, |
| 4 | + PacketLine, StreamingPeekableIter, MAX_DATA_LEN, |
5 | 5 | };
|
6 | 6 | use futures_io::{AsyncBufRead, AsyncRead};
|
7 | 7 | use futures_lite::ready;
|
|
24 | 24 | parent: Option<&'a mut StreamingPeekableIter<T>>,
|
25 | 25 | handle_progress: Option<F>,
|
26 | 26 | read_line: Option<Pin<Box<dyn Future<Output = ReadLineResult<'a>> + 'a>>>,
|
| 27 | + buf: Vec<u8>, |
27 | 28 | pos: usize,
|
28 | 29 | cap: usize,
|
29 | 30 | }
|
|
49 | 50 | parent: Some(parent),
|
50 | 51 | handle_progress: None,
|
51 | 52 | read_line: None,
|
| 53 | + buf: Vec::with_capacity(MAX_DATA_LEN), |
52 | 54 | pos: 0,
|
53 | 55 | cap: 0,
|
54 | 56 | }
|
|
69 | 71 | parent: Some(parent),
|
70 | 72 | handle_progress: Some(handle_progress),
|
71 | 73 | read_line: None,
|
| 74 | + buf: Vec::with_capacity(MAX_DATA_LEN), |
72 | 75 | pos: 0,
|
73 | 76 | cap: 0,
|
74 | 77 | }
|
|
80 | 83 | parent: Some(parent),
|
81 | 84 | handle_progress: None,
|
82 | 85 | read_line: None,
|
| 86 | + buf: Vec::with_capacity(MAX_DATA_LEN), |
83 | 87 | pos: 0,
|
84 | 88 | cap: 0,
|
85 | 89 | }
|
@@ -119,51 +123,52 @@ where
|
119 | 123 | {
|
120 | 124 | fn poll_fill_buf(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<&[u8]>> {
|
121 | 125 | use futures_lite::FutureExt;
|
122 |
| - use std::io; |
123 |
| - let this = self.as_mut().get_mut(); |
124 |
| - if this.pos >= this.cap { |
125 |
| - let (ofs, cap) = loop { |
126 |
| - // todo!("poll a future based on a field of ourselves - self-ref once again"); |
127 |
| - this.read_line = Some(this.parent.take().unwrap().read_line().boxed()); |
128 |
| - let line = match ready!(this.read_line.as_mut().expect("set above").poll(_cx)) { |
129 |
| - Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?, |
130 |
| - None => break (0, 0), |
131 |
| - }; |
132 |
| - match this.handle_progress.as_mut() { |
133 |
| - Some(handle_progress) => { |
134 |
| - let band = line |
135 |
| - .decode_band() |
136 |
| - .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; |
137 |
| - const ENCODED_BAND: usize = 1; |
138 |
| - match band { |
139 |
| - Band::Data(d) => break (U16_HEX_BYTES + ENCODED_BAND, d.len()), |
140 |
| - Band::Progress(d) => { |
141 |
| - let text = Text::from(d).0; |
142 |
| - handle_progress(false, text); |
143 |
| - } |
144 |
| - Band::Error(d) => { |
145 |
| - let text = Text::from(d).0; |
146 |
| - handle_progress(true, text); |
147 |
| - } |
148 |
| - }; |
149 |
| - } |
150 |
| - None => { |
151 |
| - break match line.as_slice() { |
152 |
| - Some(d) => (U16_HEX_BYTES, d.len()), |
153 |
| - None => { |
154 |
| - return Poll::Ready(Err(io::Error::new( |
155 |
| - io::ErrorKind::UnexpectedEof, |
156 |
| - "encountered non-data line in a data-line only context", |
157 |
| - ))) |
| 126 | + use std::{io, io::Read}; |
| 127 | + { |
| 128 | + let this = self.as_mut().get_mut(); |
| 129 | + if this.pos >= this.cap { |
| 130 | + this.cap = loop { |
| 131 | + // todo!("poll a future based on a field of ourselves - self-ref once again"); |
| 132 | + this.read_line = Some(this.parent.take().unwrap().read_line().boxed()); |
| 133 | + let line = match ready!(this.read_line.as_mut().expect("set above").poll(_cx)) { |
| 134 | + Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?, |
| 135 | + None => break 0, |
| 136 | + }; |
| 137 | + match this.handle_progress.as_mut() { |
| 138 | + Some(handle_progress) => { |
| 139 | + let mut band = line |
| 140 | + .decode_band() |
| 141 | + .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; |
| 142 | + match band { |
| 143 | + Band::Data(ref mut d) => break d.read(&mut this.buf)?, |
| 144 | + Band::Progress(d) => { |
| 145 | + let text = Text::from(d).0; |
| 146 | + handle_progress(false, text); |
| 147 | + } |
| 148 | + Band::Error(d) => { |
| 149 | + let text = Text::from(d).0; |
| 150 | + handle_progress(true, text); |
| 151 | + } |
| 152 | + }; |
| 153 | + } |
| 154 | + None => { |
| 155 | + break match line.as_slice() { |
| 156 | + Some(ref mut d) => d.read(&mut this.buf)?, |
| 157 | + None => { |
| 158 | + return Poll::Ready(Err(io::Error::new( |
| 159 | + io::ErrorKind::UnexpectedEof, |
| 160 | + "encountered non-data line in a data-line only context", |
| 161 | + ))) |
| 162 | + } |
158 | 163 | }
|
159 | 164 | }
|
160 | 165 | }
|
161 |
| - } |
162 |
| - }; |
163 |
| - this.cap = cap + ofs; |
164 |
| - this.pos = ofs; |
| 166 | + }; |
| 167 | + this.pos = 0; |
| 168 | + } |
165 | 169 | }
|
166 |
| - Poll::Ready(Ok(&this.parent.as_ref().unwrap().buf[this.pos..this.cap])) |
| 170 | + let cap = self.cap; |
| 171 | + Poll::Ready(Ok(&self.get_mut().buf[..cap])) |
167 | 172 | }
|
168 | 173 |
|
169 | 174 | fn consume(self: Pin<&mut Self>, amt: usize) {
|
|
0 commit comments