use bytes::{BufMut, BytesMut};
pub trait ReadBuf: BufMut {
fn init_mut(&mut self) -> &mut [u8];
}
impl ReadBuf for &'_ mut [u8] {
#[inline(always)]
fn init_mut(&mut self) -> &mut [u8] {
self
}
}
impl ReadBuf for BytesMut {
#[inline(always)]
fn init_mut(&mut self) -> &mut [u8] {
let remaining = self.capacity() - self.len();
self.put_bytes(0, remaining);
self
}
}
#[test]
fn test_read_buf_bytes_mut() {
let mut buf = BytesMut::with_capacity(8);
buf.put_u32(0x12345678);
assert_eq!(buf.init_mut(), [0x12, 0x34, 0x56, 0x78, 0, 0, 0, 0]);
}