about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2015-02-21 14:59:29 -0800
committerSteven Fackler <sfackler@gmail.com>2015-02-21 14:59:29 -0800
commitb46e3eec7aec546d8c4e212cc4e27ec870071e74 (patch)
tree19b08a763cdcdb95f1818310cff8f0f930c1f77f /src/libstd
parent03753ba5a21fe0e4bfc34e9691d4b13f24c90af4 (diff)
downloadrust-b46e3eec7aec546d8c4e212cc4e27ec870071e74.tar.gz
rust-b46e3eec7aec546d8c4e212cc4e27ec870071e74.zip
Implement BufRead for Take
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/io/mod.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs
index 5e810926ee4..d0d97c44ce5 100644
--- a/src/libstd/io/mod.rs
+++ b/src/libstd/io/mod.rs
@@ -681,6 +681,21 @@ impl<T: Read> Read for Take<T> {
     }
 }
 
+impl<T: BufRead> BufRead for Take<T> {
+    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`