Skip to content

Commit

Permalink
fix(buffer): get_buf to not return consumed part of buffer
Browse files Browse the repository at this point in the history
Closes #406
  • Loading branch information
seanmonstar committed Mar 30, 2015
1 parent ce52315 commit 04e3b56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ impl<R: Read> BufReader<R> {
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }

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 }
Expand Down Expand Up @@ -93,3 +98,18 @@ fn reserve(v: &mut Vec<u8>) {
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");
}
}
1 change: 0 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ mod tests {
Host: example.domain\r\n\
Expect: 100-continue\r\n\
Content-Length: 10\r\n\
Connection: close\r\n\
\r\n\
1234567890\
");
Expand Down

0 comments on commit 04e3b56

Please sign in to comment.