push_decode/decoders/
int.rs

1use core::fmt;
2use core::marker::PhantomData;
3use crate::Decoder;
4use crate::error::UnexpectedEnd;
5use crate::int::*;
6
7pub struct IntDecoder<T: Int, Endian: ByteOrder>(T::InnerDecoder, PhantomData<fn() -> T>, PhantomData<Endian>);
8
9impl<T: Int, Endian: ByteOrder> IntDecoder<T, Endian> {
10    pub fn new() -> Self {
11        IntDecoder(Default::default(), Default::default(), Default::default())
12    }
13}
14
15impl<T: Int, Endian: ByteOrder> Default for IntDecoder<T, Endian> {
16    fn default() -> Self {
17        Self::new()
18    }
19}
20
21impl<T: Int, Endian: ByteOrder> fmt::Debug for IntDecoder<T, Endian> {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        write!(f, "IntDecoder<{}>({:?})", core::any::type_name::<Endian>(), self.0)
24    }
25}
26
27impl<T: Int> Decoder for IntDecoder<T, BigEndian> {
28    type Value = T;
29    type Error = UnexpectedEnd;
30
31    fn decode_chunk(&mut self, bytes: &mut &[u8]) -> Result<(), Self::Error> {
32        self.0.decode_chunk(bytes)
33    }
34
35    fn end(self) -> Result<Self::Value, Self::Error> {
36        self.0.end().map(Int::from_be_bytes)
37    }
38}
39
40impl<T: Int> Decoder for IntDecoder<T, LittleEndian> {
41    type Value = T;
42    type Error = UnexpectedEnd;
43
44    fn decode_chunk(&mut self, bytes: &mut &[u8]) -> Result<(), Self::Error> {
45        self.0.decode_chunk(bytes)
46    }
47
48    fn end(self) -> Result<Self::Value, Self::Error> {
49        self.0.end().map(Int::from_le_bytes)
50    }
51}