Write messages directly onto message buffer instead of allocating on own buffer (#283)

* initial commit

* comment

* fmt
This commit is contained in:
zainkabani
2023-01-16 23:22:06 -05:00
committed by GitHub
parent 3f70956775
commit ab0bad6da0

View File

@@ -493,9 +493,21 @@ where
}
};
let mut buf = vec![0u8; len as usize - 4];
let mut bytes = BytesMut::with_capacity(len as usize + 1);
match stream.read_exact(&mut buf).await {
bytes.put_u8(code);
bytes.put_i32(len);
bytes.resize(bytes.len() + len as usize - mem::size_of::<i32>(), b'0');
match stream
.read_exact(
&mut bytes[mem::size_of::<u8>() + mem::size_of::<i32>()
..mem::size_of::<u8>() + mem::size_of::<i32>() + len as usize
- mem::size_of::<i32>()],
)
.await
{
Ok(_) => (),
Err(_) => {
return Err(Error::SocketError(format!(
@@ -505,12 +517,6 @@ where
}
};
let mut bytes = BytesMut::with_capacity(len as usize + 1);
bytes.put_u8(code);
bytes.put_i32(len);
bytes.put_slice(&buf);
Ok(bytes)
}