From a1aa3f823d68d8df0be8a767ac54ec6272ba0ad0 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 27 Sep 2019 18:59:30 +0200 Subject: [PATCH] finish BufRead --- src/io/read/chain.rs | 42 ++++++++++++++++++++---------------------- src/io/read/take.rs | 37 +++++++++++++++---------------------- 2 files changed, 35 insertions(+), 44 deletions(-) diff --git a/src/io/read/chain.rs b/src/io/read/chain.rs index c05a43c..e29b9bc 100644 --- a/src/io/read/chain.rs +++ b/src/io/read/chain.rs @@ -139,28 +139,26 @@ impl Read for Chain { } impl BufRead for Chain { - fn poll_fill_buf(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - // FIXME: how to make this compile? - - // let Self { - // first, - // second, - // done_first - // } = &mut *self; - - // if !*done_first { - // let rd = Pin::new(first); - - // match futures_core::ready!(rd.poll_fill_buf(cx)) { - // Ok(buf) if buf.is_empty() => { *done_first = true; } - // Ok(buf) => return Poll::Ready(Ok(buf)), - // Err(err) => return Poll::Ready(Err(err)), - // } - // } - - // let rd = Pin::new(second); - // rd.poll_fill_buf(cx) - unimplemented!() + fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let Self { + first, + second, + done_first, + } = unsafe { self.get_unchecked_mut() }; + + if !*done_first { + let first = unsafe { Pin::new_unchecked(first) }; + match futures_core::ready!(first.poll_fill_buf(cx)) { + Ok(buf) if buf.is_empty() => { + *done_first = true; + } + Ok(buf) => return Poll::Ready(Ok(buf)), + Err(err) => return Poll::Ready(Err(err)), + } + } + + let second = unsafe { Pin::new_unchecked(second) }; + second.poll_fill_buf(cx) } fn consume(mut self: Pin<&mut Self>, amt: usize) { diff --git a/src/io/read/take.rs b/src/io/read/take.rs index cd7e40d..b63f76d 100644 --- a/src/io/read/take.rs +++ b/src/io/read/take.rs @@ -187,28 +187,21 @@ pub fn take_read_internal( } impl BufRead for Take { - fn poll_fill_buf(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - // FIXME: how to get this to compile? - unimplemented!(); - - // let Self { - // inner, - // limit, - // } = &mut *self; - - // if *limit == 0 { - // return Poll::Ready(Ok(&[])); - // } - - // let rd = Pin::new(inner); - - // match futures_core::ready!(rd.poll_fill_buf(cx)) { - // Ok(buf) => { - // let cap = cmp::min(buf.len() as u64, *limit) as usize; - // Poll::Ready(Ok(&buf[..cap])) - // } - // Err(e) => Poll::Ready(Err(e)), - // } + fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let Self { inner, limit } = unsafe { self.get_unchecked_mut() }; + let inner = unsafe { Pin::new_unchecked(inner) }; + + if *limit == 0 { + return Poll::Ready(Ok(&[])); + } + + match futures_core::ready!(inner.poll_fill_buf(cx)) { + Ok(buf) => { + let cap = cmp::min(buf.len() as u64, *limit) as usize; + Poll::Ready(Ok(&buf[..cap])) + } + Err(e) => Poll::Ready(Err(e)), + } } fn consume(mut self: Pin<&mut Self>, amt: usize) {