From b46e3eec7aec546d8c4e212cc4e27ec870071e74 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 21 Feb 2015 14:59:29 -0800 Subject: [PATCH] Implement BufRead for Take --- src/libstd/io/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 5e810926ee4f..d0d97c44ce55 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -681,6 +681,21 @@ impl Read for Take { } } +impl BufRead for Take { + fn fill_buf(&mut self) -> Result<&[u8]> { + let buf = try!(self.inner.fill_buf()); + let cap = cmp::min(buf.len() as u64, self.limit) as usize; + Ok(&buf[..cap]) + } + + fn consume(&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; + self.inner.consume(amt); + } +} + /// An adaptor which will emit all read data to a specified writer as well. /// /// For more information see `ReadExt::tee`