diff --git a/src/sock/buffered.rs b/src/sock/buffered.rs index da3028a..2e64be5 100644 --- a/src/sock/buffered.rs +++ b/src/sock/buffered.rs @@ -232,22 +232,43 @@ impl AsyncRead for Buffered where T: AsyncRead { fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll> { - use futures::ready; + let this = self.get_mut(); let mut w =0; Poll::Ready(loop { + let read = if this.buffer.len() < SIZE { + let st = this.buffer.len(); this.buffer.resize(SIZE, 0); - let mut done=0; + let mut done=st; let mut r=0; + //XXX: Same issue even trying to save buffer length state over Pendings... Wtf is going on here? + macro_rules! ready { + (try $poll:expr) => { + match $poll { + Poll::Pending => { + this.buffer.resize(done, 0); + return Poll::Pending; + }, + Poll::Ready(Ok(x)) => x, + err => { + this.buffer.resize(done, 0); + return err; + } + } + } + } // XXX: V Same issue, runs the above code twice when re-polling after Pending. We need to make sure we jump back to this point in the code following a Pending poll to `stream.poll_read`, but I have no fucking clue how to do this? Eh...... We'll probably need to design the code differently. There is a lot of state that gets lost here and idk how to preserve it.... I hate this. - while done < SIZE && {r = ready!(Pin::new(&mut this.stream).poll_read(cx, &mut this.buffer[done..]))?; r > 0} + while done < SIZE && {r = ready!(try Pin::new(&mut this.stream).poll_read(cx, &mut this.buffer[done..])); r > 0} { done +=r; } + // This causes early eof (0) + //println!("Done: {}", done); + //this.buffer.resize(done, 0); done } else { SIZE @@ -318,7 +339,13 @@ mod tests let mut output = vec![0u8; DATA.len()*2]; // Bug found! Pinning and polling that stack future in `poll_read` does NOT work! + // (we unrolled the async function to a poll based one and we're STILL losing state.... FFS!) // The exact same works as a real async function. + +/* + rx.read(&mut output[..DATA.len()]).await?; + rx.read(&mut output[DATA.len()..]).await?; +*/ assert_eq!(rx.read(&mut output[..DATA.len()]).await?, DATA.len()); assert_eq!(rx.read(&mut output[DATA.len()..]).await?, DATA.len());