Skip to content

Commit

Permalink
[git-packetline] [FAIL] For some reason the is a lifetime mismatch ag…
Browse files Browse the repository at this point in the history
…ain…

…even though this is the same thing as the Option field before, but with
space to store a ref to the underlying parent.

If this doesn't work, a hand-implemented version of the future probably
wouldn't work either.
  • Loading branch information
Byron committed May 18, 2021
1 parent 22740c5 commit b4ff4e7
Showing 1 changed file with 64 additions and 55 deletions.
119 changes: 64 additions & 55 deletions git-packetline/src/read/sidebands/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
};
use futures_io::{AsyncBufRead, AsyncRead};
use futures_lite::ready;
use std::future::Future;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
Expand All @@ -23,7 +23,6 @@ where
#[pin]
state: State<'a, T>,
handle_progress: Option<F>,
read_line: Option<Pin<Box<dyn Future<Output = ReadLineResult<'a>> + 'a>>>,
pos: usize,
cap: usize,
}
Expand All @@ -50,7 +49,6 @@ where
WithSidebands {
state: State::Idle { parent },
handle_progress: None,
read_line: None,
pos: 0,
cap: 0,
}
Expand Down Expand Up @@ -80,7 +78,6 @@ where
WithSidebands {
state: State::Idle { parent },
handle_progress: Some(handle_progress),
read_line: None,
pos: 0,
cap: 0,
}
Expand All @@ -91,7 +88,6 @@ where
WithSidebands {
state: State::Idle { parent },
handle_progress: None,
read_line: None,
pos: 0,
cap: 0,
}
Expand Down Expand Up @@ -138,56 +134,69 @@ where
F: FnMut(bool, &[u8]),
{
fn poll_fill_buf(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<&[u8]>> {
// use futures_lite::FutureExt;
// use std::io;
// {
// let this = self.as_mut().get_mut();
// if this.pos >= this.cap {
// let (ofs, cap) = loop {
// // todo!("poll a future based on a field of ourselves - self-ref once again");
// this.read_line = Some(this.parent.take().unwrap().read_line().boxed());
// let line = match ready!(this.read_line.as_mut().expect("set above").poll(cx)) {
// Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
// None => break (0, 0),
// };
// match this.handle_progress.as_mut() {
// Some(handle_progress) => {
// let band = line
// .decode_band()
// .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
// const ENCODED_BAND: usize = 1;
// match band {
// Band::Data(d) => break (U16_HEX_BYTES + ENCODED_BAND, d.len()),
// Band::Progress(d) => {
// let text = Text::from(d).0;
// handle_progress(false, text);
// }
// Band::Error(d) => {
// let text = Text::from(d).0;
// handle_progress(true, text);
// }
// };
// }
// None => {
// break match line.as_slice() {
// Some(d) => (U16_HEX_BYTES, d.len()),
// None => {
// return Poll::Ready(Err(io::Error::new(
// io::ErrorKind::UnexpectedEof,
// "encountered non-data line in a data-line only context",
// )))
// }
// }
// }
// }
// };
// this.cap = cap + ofs;
// this.pos = ofs;
// }
// }
// let range = self.pos..self.cap;
// Poll::Ready(Ok(&self.get_mut().parent.as_ref().unwrap().buf[range]))
todo!("all")
use futures_lite::FutureExt;
use std::io;
{
let this = self.get_mut();
if this.pos >= this.cap {
let (ofs, cap) = loop {
match this.state {
State::Idle { ref mut parent } => {
this.state = State::ReadLine {
read_line: parent.read_line().boxed(),
parent_inactive: *parent as *mut _,
}
}
State::ReadLine {
ref mut read_line,
parent_inactive,
} => {
let line = match ready!(read_line.poll(cx)) {
Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
None => break (0, 0),
};
match this.handle_progress.as_mut() {
Some(handle_progress) => {
let band = line
.decode_band()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
const ENCODED_BAND: usize = 1;
match band {
Band::Data(d) => break (U16_HEX_BYTES + ENCODED_BAND, d.len()),
Band::Progress(d) => {
let text = Text::from(d).0;
handle_progress(false, text);
}
Band::Error(d) => {
let text = Text::from(d).0;
handle_progress(true, text);
}
};
}
None => {
break match line.as_slice() {
Some(d) => (U16_HEX_BYTES, d.len()),
None => {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"encountered non-data line in a data-line only context",
)))
}
}
}
}
}
}
};
this.cap = cap + ofs;
this.pos = ofs;
}
}
let range = self.pos..self.cap;
match self.state {
State::Idle { parent } => Poll::Ready(Ok(&parent.buf[range])),
State::ReadLine { .. } => unreachable!("at least in theory"),
}
}

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

0 comments on commit b4ff4e7

Please sign in to comment.