inn_network/codec/
forward.rs

1//-------------------------------------------------------------------
2// MIT License
3// Copyright (c) 2022 black-mongo
4// @author CameronYang
5// @doc
6//
7// @end
8// Created : 2022-04-17T20:39:01+08:00
9//-------------------------------------------------------------------
10
11pub struct ForwardCodec;
12
13use crate::codec::*;
14use actix_codec::Decoder;
15use actix_codec::Encoder;
16
17impl Encoder<Vec<u8>> for ForwardCodec {
18    type Error = Error;
19    fn encode(&mut self, item: Vec<u8>, dst: &mut BytesMut) -> Result<(), Self::Error> {
20        dst.extend(item);
21        Ok(())
22    }
23}
24impl Decoder for ForwardCodec {
25    type Item = Vec<u8>;
26    type Error = Error;
27    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
28        if src.is_empty() {
29            Ok(None)
30        } else {
31            Ok(Some(src.split_to(src.len()).to_vec()))
32        }
33    }
34}