tokio_codec/
bytes_codec.rs1use bytes::{BufMut, Bytes, BytesMut};
2use std::io;
3use tokio_io::_tokio_codec::{Decoder, Encoder};
4
5#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
7pub struct BytesCodec(());
8
9impl BytesCodec {
10 pub fn new() -> BytesCodec {
12 BytesCodec(())
13 }
14}
15
16impl Decoder for BytesCodec {
17 type Item = BytesMut;
18 type Error = io::Error;
19
20 fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<BytesMut>, io::Error> {
21 if buf.len() > 0 {
22 let len = buf.len();
23 Ok(Some(buf.split_to(len)))
24 } else {
25 Ok(None)
26 }
27 }
28}
29
30impl Encoder for BytesCodec {
31 type Item = Bytes;
32 type Error = io::Error;
33
34 fn encode(&mut self, data: Bytes, buf: &mut BytesMut) -> Result<(), io::Error> {
35 buf.reserve(data.len());
36 buf.put(data);
37 Ok(())
38 }
39}