1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use super::{Decoder, Encoder}; use bytes::{Bytes, BytesMut}; use std::convert::Infallible; #[derive(Clone, Debug, Default, PartialEq)] pub struct BytesCodec; impl Encoder for BytesCodec { type Error = Infallible; type Item = Bytes; fn encode(&mut self, src: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { dst.extend_from_slice(&src); Ok(()) } } impl Decoder for BytesCodec { type Error = Infallible; type Item = Bytes; fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { let len = src.len(); Ok(if len > 0 { Some(src.split_to(len).freeze()) } else { None }) } }