| 1 |
use crate::{Decoder, Encoder}; |
| 2 |
use bytes::{Bytes, BytesMut}; |
| 3 |
use std::io::Error; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
pub struct BytesCodec; |
| 30 |
|
| 31 |
impl Encoder for BytesCodec { |
| 32 |
type Item<'a> = Bytes; |
| 33 |
type Error = Error; |
| 34 |
|
| 35 |
fn encode(&mut self, src: Self::Item<'_>, dst: &mut BytesMut) -> Result<(), Self::Error> { |
| 36 |
dst.extend_from_slice(&src); |
| 37 |
Ok(()) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl Decoder for BytesCodec { |
| 42 |
type Item = Bytes; |
| 43 |
type Error = Error; |
| 44 |
|
| 45 |
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { |
| 46 |
let len = src.len(); |
| 47 |
if len > 0 { |
| 48 |
Ok(Some(src.split_to(len).freeze())) |
| 49 |
} else { |
| 50 |
Ok(None) |
| 51 |
} |
| 52 |
} |
| 53 |
} |