codec: support byte slices in LengthDelimitedCodec (#8355)

This commit is contained in:
Dhruv Vaishnav
2026-08-10 12:18:22 +02:00
committed by GitHub
parent 8b13642a1f
commit 43e6ef50e4
2 changed files with 84 additions and 34 deletions
+11 -3
View File
@@ -603,10 +603,10 @@ impl Decoder for LengthDelimitedCodec {
}
}
impl Encoder<Bytes> for LengthDelimitedCodec {
impl Encoder<&[u8]> for LengthDelimitedCodec {
type Error = io::Error;
fn encode(&mut self, data: Bytes, dst: &mut BytesMut) -> Result<(), io::Error> {
fn encode(&mut self, data: &[u8], dst: &mut BytesMut) -> Result<(), io::Error> {
let n = data.len();
if n > self.builder.max_frame_len {
@@ -641,12 +641,20 @@ impl Encoder<Bytes> for LengthDelimitedCodec {
}
// Write the frame to the buffer
dst.extend_from_slice(&data[..]);
dst.extend_from_slice(data);
Ok(())
}
}
impl Encoder<Bytes> for LengthDelimitedCodec {
type Error = io::Error;
fn encode(&mut self, data: Bytes, dst: &mut BytesMut) -> Result<(), io::Error> {
Encoder::<&[u8]>::encode(self, data.as_ref(), dst)
}
}
impl Default for LengthDelimitedCodec {
fn default() -> Self {
Self::new()