rseip_core/codec/decode/
little_endian.rs

1// rseip
2//
3// rseip - Ethernet/IP (CIP) in pure Rust.
4// Copyright: 2021, Joylei <leingliu@gmail.com>
5// License: MIT
6
7use super::*;
8use bytes::Bytes;
9use core::marker::PhantomData;
10
11#[derive(Debug)]
12pub struct LittleEndianDecoder<E> {
13    buf: Bytes,
14    _marker: PhantomData<E>,
15}
16
17impl<E> LittleEndianDecoder<E> {
18    pub fn new(buf: Bytes) -> Self {
19        Self {
20            buf,
21            _marker: Default::default(),
22        }
23    }
24
25    pub fn into_inner(self) -> Bytes {
26        self.buf
27    }
28}
29
30impl<'de, E: Error> Decoder<'de> for LittleEndianDecoder<E> {
31    type Buf = Bytes;
32    type Error = E;
33
34    #[inline(always)]
35    fn buf(&self) -> &Self::Buf {
36        &self.buf
37    }
38
39    #[inline(always)]
40    fn buf_mut(&mut self) -> &mut Self::Buf {
41        &mut self.buf
42    }
43
44    #[inline]
45    fn decode_sized<V: Visitor<'de>>(
46        &mut self,
47        size: usize,
48        visitor: V,
49    ) -> Result<V::Value, Self::Error>
50    where
51        Self: Sized,
52    {
53        self.ensure_size(size)?;
54        let buf = self.buf.split_to(size);
55        let decoder = Self::new(buf);
56        visitor.visit(decoder)
57    }
58}