1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 03:42:59 +01:00

add BytesCodec

This commit is contained in:
Nikolay Kim 2018-10-24 10:43:30 -07:00
parent ff914f79fc
commit e1418018c6
2 changed files with 35 additions and 0 deletions

33
src/codec/bcodec.rs Normal file
View File

@ -0,0 +1,33 @@
use std::io;
use bytes::{Bytes, BytesMut};
use tokio_codec::{Decoder, Encoder};
/// Bytes codec.
///
/// Reads/Writes chunks of bytes from a stream.
#[derive(Debug, Copy, Clone)]
pub struct BytesCodec;
impl Encoder for BytesCodec {
type Item = Bytes;
type Error = io::Error;
fn encode(&mut self, item: Bytes, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.extend_from_slice(&item[..]);
Ok(())
}
}
impl Decoder for BytesCodec {
type Item = BytesMut;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if src.is_empty() {
Ok(None)
} else {
Ok(Some(src.take()))
}
}
}

View File

@ -12,11 +12,13 @@
#![deny(missing_docs, missing_debug_implementations, warnings)] #![deny(missing_docs, missing_debug_implementations, warnings)]
mod bcodec;
mod framed; mod framed;
mod framed2; mod framed2;
mod framed_read; mod framed_read;
mod framed_write; mod framed_write;
pub use self::bcodec::BytesCodec;
pub use self::framed::{Framed, FramedParts}; pub use self::framed::{Framed, FramedParts};
pub use self::framed2::{Framed2, FramedParts2}; pub use self::framed2::{Framed2, FramedParts2};
pub use self::framed_read::FramedRead; pub use self::framed_read::FramedRead;