Skip to content
Snippets Groups Projects
Commit 23ad12f8 authored by Paul Cavallaro's avatar Paul Cavallaro
Browse files

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

without necessitating flipping to ByteBuf.
parent d2066167
No related branches found
No related tags found
No related merge requests found
......@@ -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 {
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment