mirror of
https://github.com/fafhrd91/actix-net
synced 2025-01-10 02:22:20 +01:00
34 lines
732 B
Rust
34 lines
732 B
Rust
|
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()))
|
||
|
}
|
||
|
}
|
||
|
}
|