fix(buffer): get_buf to not return consumed part of buffer

Closes #406
This commit is contained in:
Sean McArthur
2015-03-29 21:20:09 -07:00
parent ce5231508e
commit 04e3b56515
2 changed files with 21 additions and 2 deletions

View File

@@ -27,7 +27,12 @@ impl<R: Read> BufReader<R> {
pub fn get_mut(&mut self) -> &mut R { &mut self.inner } pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
pub fn get_buf(&self) -> &[u8] { pub fn get_buf(&self) -> &[u8] {
self.buf.get_ref() let pos = self.buf.position() as usize;
if pos < self.buf.get_ref().len() {
&self.buf.get_ref()[pos..]
} else {
&[]
}
} }
pub fn into_inner(self) -> R { self.inner } pub fn into_inner(self) -> R { self.inner }
@@ -93,3 +98,18 @@ fn reserve(v: &mut Vec<u8>) {
v.reserve(cmp::min(cap * 4, MAX_BUFFER_SIZE) - cap); v.reserve(cmp::min(cap * 4, MAX_BUFFER_SIZE) - cap);
} }
} }
#[cfg(test)]
mod tests {
use std::io::BufRead;
use super::BufReader;
#[test]
fn test_consume_and_get_buf() {
let mut rdr = BufReader::new(&b"foo bar baz"[..]);
rdr.read_into_buf().unwrap();
rdr.consume(8);
assert_eq!(rdr.get_buf(), b"baz");
}
}

View File

@@ -228,7 +228,6 @@ mod tests {
Host: example.domain\r\n\ Host: example.domain\r\n\
Expect: 100-continue\r\n\ Expect: 100-continue\r\n\
Content-Length: 10\r\n\ Content-Length: 10\r\n\
Connection: close\r\n\
\r\n\ \r\n\
1234567890\ 1234567890\
"); ");