rseip_core/codec/
mod.rs

1// rseip
2//
3// rseip - Ethernet/IP (CIP) in pure Rust.
4// Copyright: 2021, Joylei <leingliu@gmail.com>
5// License: MIT
6
7/*!
8The library provides `Encode` to encode values, `Decode` to decode values, and `TagValue` to manipulate tag data values. The library already implements `Encode` and `Decode` for some rust types: `bool`,`i8`,`u8`,`i16`,`u16`,`i32`,`u32`,`i64`,`u64`,`f32`,`f64`,`i128`,`u128`,`()`,`Option`,`Tuple`,`Vec`,`[T;N]`,`SmallVec`. For structure type, you need to implement `Encode` and `Decode` by yourself.
9*/
10
11pub mod decode;
12pub mod encode;
13
14pub use decode::*;
15pub use encode::*;
16
17use bytes::{Buf, Bytes};
18
19/// take remaining bytes
20#[derive(Debug, Clone)]
21pub struct BytesHolder(Bytes);
22
23impl From<BytesHolder> for Bytes {
24    #[inline]
25    fn from(src: BytesHolder) -> Self {
26        src.0
27    }
28}
29
30impl<'de> Decode<'de> for BytesHolder {
31    #[inline]
32    fn decode<D>(mut decoder: D) -> Result<Self, D::Error>
33    where
34        D: Decoder<'de>,
35    {
36        let size = decoder.remaining();
37        let data = decoder.buf_mut().copy_to_bytes(size);
38        Ok(Self(data))
39    }
40}