rseip_core/codec/
decode.rs1mod impls;
8mod little_endian;
9pub mod visitor;
10
11use crate::Error;
12use bytes::Buf;
13pub use little_endian::LittleEndianDecoder;
14pub use visitor::Visitor;
15
16pub trait Decoder<'de> {
17 type Error: Error;
18 type Buf: Buf;
19 fn buf(&self) -> &Self::Buf;
21
22 fn buf_mut(&mut self) -> &mut Self::Buf;
24
25 #[inline(always)]
27 fn ensure_size(&self, expected: usize) -> Result<(), Self::Error> {
28 let actual_len = self.buf().remaining();
29 if actual_len < expected {
30 Err(Self::Error::invalid_length(actual_len, expected))
31 } else {
32 Ok(())
33 }
34 }
35
36 #[inline]
38 fn decode_any<T>(&mut self) -> Result<T, Self::Error>
39 where
40 T: Decode<'de>,
41 Self: Sized,
42 {
43 T::decode(self)
44 }
45
46 #[inline(always)]
48 fn decode_bool(&mut self) -> bool {
49 self.buf_mut().get_u8() != 0
50 }
51
52 #[inline(always)]
54 fn decode_i8(&mut self) -> i8 {
55 self.buf_mut().get_i8()
56 }
57
58 #[inline(always)]
60 fn decode_u8(&mut self) -> u8 {
61 self.buf_mut().get_u8()
62 }
63
64 #[inline(always)]
66 fn decode_i16(&mut self) -> i16 {
67 self.buf_mut().get_i16_le()
68 }
69
70 #[inline(always)]
72 fn decode_u16(&mut self) -> u16 {
73 self.buf_mut().get_u16_le()
74 }
75
76 #[inline(always)]
78 fn decode_i32(&mut self) -> i32 {
79 self.buf_mut().get_i32_le()
80 }
81
82 #[inline(always)]
84 fn decode_u32(&mut self) -> u32 {
85 self.buf_mut().get_u32_le()
86 }
87
88 #[inline(always)]
90 fn decode_i64(&mut self) -> i64 {
91 self.buf_mut().get_i64_le()
92 }
93
94 #[inline(always)]
96 fn decode_u64(&mut self) -> u64 {
97 self.buf_mut().get_u64_le()
98 }
99
100 #[inline(always)]
102 fn decode_f32(&mut self) -> f32 {
103 self.buf_mut().get_f32_le()
104 }
105
106 #[inline(always)]
108 fn decode_f64(&mut self) -> f64 {
109 self.buf_mut().get_f64_le()
110 }
111
112 #[inline(always)]
114 fn decode_i128(&mut self) -> i128 {
115 self.buf_mut().get_i128_le()
116 }
117
118 #[inline(always)]
120 fn decode_u128(&mut self) -> u128 {
121 self.buf_mut().get_u128_le()
122 }
123
124 #[inline(always)]
125 fn remaining(&mut self) -> usize {
126 self.buf().remaining()
127 }
128
129 #[inline(always)]
130 fn has_remaining(&mut self) -> bool {
131 self.buf().has_remaining()
132 }
133
134 #[inline]
136 fn decode_with<V: Visitor<'de>>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
137 where
138 Self: Sized,
139 {
140 visitor.visit(self)
141 }
142
143 fn decode_sized<V: Visitor<'de>>(
145 &mut self,
146 size: usize,
147 visitor: V,
148 ) -> Result<V::Value, Self::Error>
149 where
150 Self: Sized;
151}
152
153pub trait Decode<'de>: Sized {
154 fn decode<D>(decoder: D) -> Result<Self, D::Error>
155 where
156 D: Decoder<'de>;
157
158 #[doc(hidden)]
159 #[inline]
160 fn decode_in_place<D>(decoder: D, place: &mut Self) -> Result<(), D::Error>
161 where
162 D: Decoder<'de>,
163 {
164 *place = Self::decode(decoder)?;
165 Ok(())
166 }
167}