use bytes::{Buf, BufMut}; use tonic::codec::{Codec, DecodeBuf, Decoder, EncodeBuf, Encoder}; use tonic::Status; #[derive(Default, Clone)] pub struct RawBytesCodec; impl Codec for RawBytesCodec { type Encode = Vec; type Decode = Vec; type Encoder = RawBytesEncoder; type Decoder = RawBytesDecoder; fn encoder(&mut self) -> Self::Encoder { RawBytesEncoder } fn decoder(&mut self) -> Self::Decoder { RawBytesDecoder } } #[derive(Default, Clone)] pub struct RawBytesEncoder; impl Encoder for RawBytesEncoder { type Item = Vec; type Error = Status; fn encode(&mut self, item: Self::Item, dst: &mut EncodeBuf<'_>) -> Result<(), Self::Error> { dst.put_slice(&item); Ok(()) } } #[derive(Default, Clone)] pub struct RawBytesDecoder; impl Decoder for RawBytesDecoder { type Item = Vec; type Error = Status; fn decode(&mut self, src: &mut DecodeBuf<'_>) -> Result, Self::Error> { let chunk = src.chunk().to_vec(); src.advance(chunk.len()); Ok(Some(chunk)) } }