Skip to content

Commit 01e15c8

Browse files
committed
[git-packetline] get it to compile by resorting to another buffer
1 parent 96d0ecf commit 01e15c8

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

git-packetline/src/read/sidebands/async_io.rs

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
decode,
33
immutable::{Band, Text},
4-
PacketLine, StreamingPeekableIter, U16_HEX_BYTES,
4+
PacketLine, StreamingPeekableIter, MAX_DATA_LEN,
55
};
66
use futures_io::{AsyncBufRead, AsyncRead};
77
use futures_lite::ready;
@@ -24,6 +24,7 @@ where
2424
parent: Option<&'a mut StreamingPeekableIter<T>>,
2525
handle_progress: Option<F>,
2626
read_line: Option<Pin<Box<dyn Future<Output = ReadLineResult<'a>> + 'a>>>,
27+
buf: Vec<u8>,
2728
pos: usize,
2829
cap: usize,
2930
}
@@ -49,6 +50,7 @@ where
4950
parent: Some(parent),
5051
handle_progress: None,
5152
read_line: None,
53+
buf: Vec::with_capacity(MAX_DATA_LEN),
5254
pos: 0,
5355
cap: 0,
5456
}
@@ -69,6 +71,7 @@ where
6971
parent: Some(parent),
7072
handle_progress: Some(handle_progress),
7173
read_line: None,
74+
buf: Vec::with_capacity(MAX_DATA_LEN),
7275
pos: 0,
7376
cap: 0,
7477
}
@@ -80,6 +83,7 @@ where
8083
parent: Some(parent),
8184
handle_progress: None,
8285
read_line: None,
86+
buf: Vec::with_capacity(MAX_DATA_LEN),
8387
pos: 0,
8488
cap: 0,
8589
}
@@ -119,51 +123,52 @@ where
119123
{
120124
fn poll_fill_buf(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<&[u8]>> {
121125
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+
}
158163
}
159164
}
160165
}
161-
}
162-
};
163-
this.cap = cap + ofs;
164-
this.pos = ofs;
166+
};
167+
this.pos = 0;
168+
}
165169
}
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]))
167172
}
168173

169174
fn consume(self: Pin<&mut Self>, amt: usize) {

0 commit comments

Comments
 (0)