Skip to content

Commit dc6c8fb

Browse files
feat(io): add stub for BufRead for Take
1 parent d9aec10 commit dc6c8fb

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/io/read/take.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cmp;
22
use std::pin::Pin;
33

4-
use crate::io::{self, Read};
4+
use crate::io::{self, BufRead, Read};
55
use crate::task::{Context, Poll};
66

77
/// Reader adaptor which limits the bytes read from an underlying reader.
@@ -186,6 +186,41 @@ pub fn take_read_internal<R: Read + ?Sized>(
186186
}
187187
}
188188

189+
impl<T: BufRead + Unpin> BufRead for Take<T> {
190+
fn poll_fill_buf(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
191+
// FIXME: how to get this to compile?
192+
unimplemented!();
193+
194+
// let Self {
195+
// inner,
196+
// limit,
197+
// } = &mut *self;
198+
199+
// if *limit == 0 {
200+
// return Poll::Ready(Ok(&[]));
201+
// }
202+
203+
// let rd = Pin::new(inner);
204+
205+
// match futures_core::ready!(rd.poll_fill_buf(cx)) {
206+
// Ok(buf) => {
207+
// let cap = cmp::min(buf.len() as u64, *limit) as usize;
208+
// Poll::Ready(Ok(&buf[..cap]))
209+
// }
210+
// Err(e) => Poll::Ready(Err(e)),
211+
// }
212+
}
213+
214+
fn consume(mut self: Pin<&mut Self>, amt: usize) {
215+
// Don't let callers reset the limit by passing an overlarge value
216+
let amt = cmp::min(amt as u64, self.limit) as usize;
217+
self.limit -= amt as u64;
218+
219+
let rd = Pin::new(&mut self.inner);
220+
rd.consume(amt);
221+
}
222+
}
223+
189224
#[cfg(test)]
190225
mod tests {
191226
use crate::io;

0 commit comments

Comments
 (0)