use bytes::Bytes;
use bytes::{Buf, BufMut};
use tonic::codec::{Codec, DecodeBuf, Decoder, EncodeBuf, Encoder};
#[derive(Clone, Default)]
pub struct RawCodec;
#[derive(Clone, Default)]
pub struct RawEncoder;
#[derive(Clone, Default)]
pub struct RawDecoder;
impl Encoder for RawEncoder {
type Item = Bytes;
type Error = tonic::Status;
fn encode(&mut self, item: Self::Item, dst: &mut EncodeBuf<'_>) -> Result<(), Self::Error> {
dst.put_slice(&item);
Ok(())
}
}
impl Decoder for RawDecoder {
type Item = Bytes;
type Error = tonic::Status;
fn decode(&mut self, src: &mut DecodeBuf<'_>) -> Result<Option<Self::Item>, Self::Error> {
let n = src.remaining();
Ok(Some(src.copy_to_bytes(n)))
}
}
impl Codec for RawCodec {
type Encode = Bytes;
type Decode = Bytes;
type Encoder = RawEncoder;
type Decoder = RawDecoder;
fn encoder(&mut self) -> Self::Encoder {
RawEncoder
}
fn decoder(&mut self) -> Self::Decoder {
RawDecoder
}
}