|
1 | 1 | use crate::{
|
2 | 2 | decode,
|
3 | 3 | immutable::{Band, Text},
|
4 |
| - PacketLine, StreamingPeekableIter, MAX_DATA_LEN, |
| 4 | + PacketLine, StreamingPeekableIter, U16_HEX_BYTES, |
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>, |
28 | 27 | pos: usize,
|
29 | 28 | cap: usize,
|
30 | 29 | }
|
|
50 | 49 | parent: Some(parent),
|
51 | 50 | handle_progress: None,
|
52 | 51 | read_line: None,
|
53 |
| - buf: Vec::with_capacity(MAX_DATA_LEN), |
54 | 52 | pos: 0,
|
55 | 53 | cap: 0,
|
56 | 54 | }
|
|
71 | 69 | parent: Some(parent),
|
72 | 70 | handle_progress: Some(handle_progress),
|
73 | 71 | read_line: None,
|
74 |
| - buf: Vec::with_capacity(MAX_DATA_LEN), |
75 | 72 | pos: 0,
|
76 | 73 | cap: 0,
|
77 | 74 | }
|
|
83 | 80 | parent: Some(parent),
|
84 | 81 | handle_progress: None,
|
85 | 82 | read_line: None,
|
86 |
| - buf: Vec::with_capacity(MAX_DATA_LEN), |
87 | 83 | pos: 0,
|
88 | 84 | cap: 0,
|
89 | 85 | }
|
@@ -123,52 +119,51 @@ where
|
123 | 119 | {
|
124 | 120 | fn poll_fill_buf(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<&[u8]>> {
|
125 | 121 | use futures_lite::FutureExt;
|
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 |
| - } |
| 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 | + ))) |
163 | 158 | }
|
164 | 159 | }
|
165 | 160 | }
|
166 |
| - }; |
167 |
| - this.pos = 0; |
168 |
| - } |
| 161 | + } |
| 162 | + }; |
| 163 | + this.cap = cap + ofs; |
| 164 | + this.pos = ofs; |
169 | 165 | }
|
170 |
| - let cap = self.cap; |
171 |
| - Poll::Ready(Ok(&self.get_mut().buf[..cap])) |
| 166 | + Poll::Ready(Ok(&this.parent.as_ref().unwrap().buf[this.pos..this.cap])) |
172 | 167 | }
|
173 | 168 |
|
174 | 169 | fn consume(self: Pin<&mut Self>, amt: usize) {
|
|
0 commit comments