Add bytes() method to MutByteBuf to allow reading of data already written

without necessitating flipping to ByteBuf.
This commit is contained in:
Paul Cavallaro
2015-07-17 22:22:58 -04:00
parent d20661676f
commit 23ad12f842
2 changed files with 16 additions and 0 deletions
+4
View File
@@ -303,6 +303,10 @@ impl MutByteBuf {
len as usize
}
}
pub fn bytes<'a>(&'a self) -> &'a [u8] {
&self.buf.mem.bytes()[..self.buf.pos()]
}
}
impl MutBuf for MutByteBuf {
+12
View File
@@ -17,6 +17,18 @@ pub fn test_initial_buf_empty() {
assert!(buf.remaining() == 128);
}
#[test]
pub fn test_byte_buf_bytes() {
let mut buf = ByteBuf::mut_with_capacity(32);
buf.write(&b"hello "[..]).unwrap();
assert_eq!(&b"hello "[..], buf.bytes());
buf.write(&b"world"[..]).unwrap();
assert_eq!(&b"hello world"[..], buf.bytes());
let buf = buf.flip();
assert_eq!(&b"hello world"[..], buf.bytes());
}
#[test]
pub fn test_byte_buf_read_write() {
let mut buf = ByteBuf::mut_with_capacity(32);