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