diff --git a/src/buf/byte.rs b/src/buf/byte.rs index aae272cba0e985fe7ca19e443933268d1333209e..1e187dd92572e8a917147957f9b1cc0bd2eefb2d 100644 --- a/src/buf/byte.rs +++ b/src/buf/byte.rs @@ -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 { diff --git a/test/test_byte_buf.rs b/test/test_byte_buf.rs index 576bb0bb5569705c904c840ebe959cb6da23bf36..a0e69376672c5f22d259fea6c2b1ebdffc6f594c 100644 --- a/test/test_byte_buf.rs +++ b/test/test_byte_buf.rs @@ -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);