1use core::marker::PhantomData;
2use crate::cursor::Cursor;
3use crate::Result;
4
5pub use byteorder::*;
6pub type LECursor<T> = EndianCursor<LittleEndian, T>;
7pub type BECursor<T> = EndianCursor<BigEndian, T>;
8
9pub struct EndianCursor<B: ByteOrder, T: AsRef<[u8]>> {
10 cursor: Cursor<T>,
11 _phantom: PhantomData<B>,
12}
13
14impl<B: ByteOrder, T: AsRef<[u8]>> EndianCursor<B, T> {
15 pub fn new(inner: T) -> Self {
16 Self {
17 cursor: Cursor::new(inner),
18 _phantom: PhantomData,
19 }
20 }
21
22 #[inline]
23 pub fn read_u8(&mut self) -> Result<u8> {
24 const LEN: usize = core::mem::size_of::<u8>();
25 let mut buf = [0; LEN];
26
27 self.cursor.read_exact(&mut buf)?;
28
29 Ok(buf[0])
30 }
31
32 #[inline]
33 pub fn read_i8(&mut self) -> Result<i8> {
34 const LEN: usize = core::mem::size_of::<i8>();
35 let mut buf = [0; LEN];
36
37 self.cursor.read_exact(&mut buf)?;
38
39 Ok(buf[0] as i8)
40 }
41
42 #[inline]
43 pub fn read_u16(&mut self) -> Result<u16> {
44 const LEN: usize = core::mem::size_of::<u16>();
45 let mut buf = [0; LEN];
46
47 self.cursor.read_exact(&mut buf)?;
48
49 Ok(B::read_u16(&buf))
50 }
51
52 #[inline]
53 pub fn read_i16(&mut self) -> Result<i16> {
54 const LEN: usize = core::mem::size_of::<i16>();
55 let mut buf = [0; LEN];
56
57 self.cursor.read_exact(&mut buf)?;
58
59 Ok(B::read_i16(&buf))
60 }
61
62 #[inline]
63 pub fn read_u24(&mut self) -> Result<u32> {
64 const LEN: usize = core::mem::size_of::<u32>();
65 let mut buf = [0; LEN];
66
67 self.cursor.read_exact(&mut buf)?;
68
69 Ok(B::read_u24(&buf))
70 }
71
72 #[inline]
73 pub fn read_i24(&mut self) -> Result<i32> {
74 const LEN: usize = core::mem::size_of::<i32>();
75 let mut buf = [0; LEN];
76
77 self.cursor.read_exact(&mut buf)?;
78
79 Ok(B::read_i24(&buf))
80 }
81
82 #[inline]
83 pub fn read_u32(&mut self) -> Result<u32> {
84 const LEN: usize = core::mem::size_of::<u32>();
85 let mut buf = [0; LEN];
86
87 self.cursor.read_exact(&mut buf)?;
88
89 Ok(B::read_u32(&buf))
90 }
91
92 #[inline]
93 pub fn read_i32(&mut self) -> Result<i32> {
94 const LEN: usize = core::mem::size_of::<i32>();
95 let mut buf = [0; LEN];
96
97 self.cursor.read_exact(&mut buf)?;
98
99 Ok(B::read_i32(&buf))
100 }
101
102 #[inline]
103 pub fn read_u48(&mut self) -> Result<u64> {
104 const LEN: usize = core::mem::size_of::<u64>();
105 let mut buf = [0; LEN];
106
107 self.cursor.read_exact(&mut buf)?;
108
109 Ok(B::read_u48(&buf))
110 }
111
112 #[inline]
113 pub fn read_i48(&mut self) -> Result<i64> {
114 const LEN: usize = core::mem::size_of::<i64>();
115 let mut buf = [0; LEN];
116
117 self.cursor.read_exact(&mut buf)?;
118
119 Ok(B::read_i48(&buf))
120 }
121
122 #[inline]
123 pub fn read_u64(&mut self) -> Result<u64> {
124 const LEN: usize = core::mem::size_of::<u64>();
125 let mut buf = [0; LEN];
126
127 self.cursor.read_exact(&mut buf)?;
128
129 Ok(B::read_u64(&buf))
130 }
131
132 #[inline]
133 pub fn read_i64(&mut self) -> Result<i64> {
134 const LEN: usize = core::mem::size_of::<i64>();
135 let mut buf = [0; LEN];
136
137 self.cursor.read_exact(&mut buf)?;
138
139 Ok(B::read_i64(&buf))
140 }
141
142 #[inline]
143 pub fn read_u128(&mut self) -> Result<u128> {
144 const LEN: usize = core::mem::size_of::<u128>();
145 let mut buf = [0; LEN];
146
147 self.cursor.read_exact(&mut buf)?;
148
149 Ok(B::read_u128(&buf))
150 }
151
152 #[inline]
153 pub fn read_i128(&mut self) -> Result<i128> {
154 const LEN: usize = core::mem::size_of::<i128>();
155 let mut buf = [0; LEN];
156
157 self.cursor.read_exact(&mut buf)?;
158
159 Ok(B::read_i128(&buf))
160 }
161
162 #[inline]
163 pub fn read_uint(&mut self, nbytes: usize) -> Result<u64> {
164 const LEN: usize = core::mem::size_of::<u64>();
165 let mut buf = [0; LEN];
166
167 self.cursor.read_exact(&mut buf[..nbytes])?;
168
169 Ok(B::read_uint(&buf[..nbytes], nbytes))
170 }
171
172 #[inline]
173 pub fn read_int(&mut self, nbytes: usize) -> Result<i64> {
174 const LEN: usize = core::mem::size_of::<i64>();
175 let mut buf = [0; LEN];
176
177 self.cursor.read_exact(&mut buf[..nbytes])?;
178
179 Ok(B::read_int(&buf[..nbytes], nbytes))
180 }
181
182 #[inline]
183 pub fn read_uint128(&mut self, nbytes: usize) -> Result<u128> {
184 const LEN: usize = core::mem::size_of::<u128>();
185 let mut buf = [0; LEN];
186
187 self.cursor.read_exact(&mut buf[..nbytes])?;
188
189 Ok(B::read_uint128(&buf[..nbytes], nbytes))
190 }
191
192 #[inline]
193 pub fn read_int128(&mut self, nbytes: usize) -> Result<i128> {
194 const LEN: usize = core::mem::size_of::<i128>();
195 let mut buf = [0; LEN];
196
197 self.cursor.read_exact(&mut buf[..nbytes])?;
198
199 Ok(B::read_int128(&buf[..nbytes], nbytes))
200 }
201
202 #[inline]
203 pub fn read_f32(&mut self) -> Result<f32> {
204 const LEN: usize = core::mem::size_of::<f32>();
205 let mut buf = [0; LEN];
206
207 self.cursor.read_exact(&mut buf)?;
208
209 Ok(B::read_f32(&buf))
210 }
211
212 #[inline]
213 pub fn read_f64(&mut self) -> Result<f64> {
214 const LEN: usize = core::mem::size_of::<f64>();
215 let mut buf = [0; LEN];
216
217 self.cursor.read_exact(&mut buf)?;
218
219 Ok(B::read_f64(&buf))
220 }
221}