Skip to content

Commit b46e3ee

Browse files
committed
Implement BufRead for Take
1 parent 03753ba commit b46e3ee

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/libstd/io/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,21 @@ impl<T: Read> Read for Take<T> {
681681
}
682682
}
683683

684+
impl<T: BufRead> BufRead for Take<T> {
685+
fn fill_buf(&mut self) -> Result<&[u8]> {
686+
let buf = try!(self.inner.fill_buf());
687+
let cap = cmp::min(buf.len() as u64, self.limit) as usize;
688+
Ok(&buf[..cap])
689+
}
690+
691+
fn consume(&mut self, amt: usize) {
692+
// Don't let callers reset the limit by passing an overlarge value
693+
let amt = cmp::min(amt as u64, self.limit) as usize;
694+
self.limit -= amt as u64;
695+
self.inner.consume(amt);
696+
}
697+
}
698+
684699
/// An adaptor which will emit all read data to a specified writer as well.
685700
///
686701
/// For more information see `ReadExt::tee`

0 commit comments

Comments
 (0)