mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-14 00:00:13 +02:00
Add bytes() method to MutByteBuf to allow reading of data already written
without necessitating flipping to ByteBuf.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user