]> git.lizzy.rs Git - rust.git/commitdiff
Implement BufRead for Take
authorSteven Fackler <sfackler@gmail.com>
Sat, 21 Feb 2015 22:59:29 +0000 (14:59 -0800)
committerSteven Fackler <sfackler@gmail.com>
Sat, 21 Feb 2015 22:59:29 +0000 (14:59 -0800)
src/libstd/io/mod.rs

index 5e810926ee4fbd2f178bdca8da8e3ee91087b740..d0d97c44ce553d0b01787f1b2dfe3802a28829e2 100644 (file)
@@ -681,6 +681,21 @@ fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
     }
 }
 
+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`