From dc6c8fb1318677e0b2ab70e211f6c4fc14a77be2 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 27 Sep 2019 16:36:55 +0200 Subject: [PATCH] feat(io): add stub for BufRead for Take --- src/io/read/take.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/io/read/take.rs b/src/io/read/take.rs index 1dfe61a..cd7e40d 100644 --- a/src/io/read/take.rs +++ b/src/io/read/take.rs @@ -1,7 +1,7 @@ use std::cmp; use std::pin::Pin; -use crate::io::{self, Read}; +use crate::io::{self, BufRead, Read}; use crate::task::{Context, Poll}; /// Reader adaptor which limits the bytes read from an underlying reader. @@ -186,6 +186,41 @@ 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 consume(mut self: Pin<&mut Self>, amt: usize) { + // Don't let callers reset the limit by passing an overlarge value + let amt = cmp::min(amt as u64, self.limit) as usize; + self.limit -= amt as u64; + + let rd = Pin::new(&mut self.inner); + rd.consume(amt); + } +} + #[cfg(test)] mod tests { use crate::io;