rw_utils/
num_read.rs

1use std::io;
2use std::io::Read;
3use std::mem::size_of;
4
5///
6/// Trait that provides various methods to read numbers.
7/// Automatically implemented for all implementations of io::Read.
8/// This trait is sealed and cannot be implemented manually.
9///
10pub trait NumRead : private::Sealed {
11    ///
12    /// Reads one byte and treats 0 as false and any other value as true.
13    ///
14    fn read_bool(&mut self) -> io::Result<bool>;
15
16    ///
17    /// Reads 1 byte and treats it as an u8
18    ///
19    fn read_u8(&mut self) -> io::Result<u8>;
20
21    ///
22    /// Reads 2 bytes and treats them as an u16 in little endian byte order
23    ///
24    fn read_u16_le(&mut self) -> io::Result<u16>;
25
26    ///
27    /// Reads 2 bytes and treats them as an u16 in big endian byte order
28    ///
29    fn read_u16_be(&mut self) -> io::Result<u16>;
30
31    ///
32    /// Reads 2 bytes and treats them as an u16 in native byte order
33    ///
34    fn read_u16_ne(&mut self) -> io::Result<u16>;
35
36    ///
37    /// Reads 4 bytes and treats them as an u32 in little endian byte order
38    ///
39    fn read_u32_le(&mut self) -> io::Result<u32>;
40
41    ///
42    /// Reads 4 bytes and treats them as an u32 in big endian byte order
43    ///
44    fn read_u32_be(&mut self) -> io::Result<u32>;
45
46    ///
47    /// Reads 4 bytes and treats them as an u32 in native byte order
48    ///
49    fn read_u32_ne(&mut self) -> io::Result<u32>;
50
51    ///
52    /// Reads 8 bytes and treats them as an u64 in little endian byte order
53    ///
54    fn read_u64_le(&mut self) -> io::Result<u64>;
55
56    ///
57    /// Reads 8 bytes and treats them as an u64 in big endian byte order
58    ///
59    fn read_u64_be(&mut self) -> io::Result<u64>;
60
61    ///
62    /// Reads 8 bytes and treats them as an u64 in native byte order
63    ///
64    fn read_u64_ne(&mut self) -> io::Result<u64>;
65
66    ///
67    /// Reads 16 bytes and treats them as an u128 in little endian byte order
68    ///
69    fn read_u128_le(&mut self) -> io::Result<u128>;
70
71    ///
72    /// Reads 16 bytes and treats them as an u128 in big endian byte order
73    ///
74    fn read_u128_be(&mut self) -> io::Result<u128>;
75
76    ///
77    /// Reads 16 bytes and treats them as an u128 in native byte order
78    ///
79    fn read_u128_ne(&mut self) -> io::Result<u128>;
80
81    ///
82    /// Reads 1 byte and treats it as an i8
83    ///
84    fn read_i8(&mut self) -> io::Result<i8>;
85
86    ///
87    /// Reads 2 bytes and treats them as an i16 in little endian byte order
88    ///
89    fn read_i16_le(&mut self) -> io::Result<i16>;
90
91    ///
92    /// Reads 2 bytes and treats them as an i16 in big endian byte order
93    ///
94    fn read_i16_be(&mut self) -> io::Result<i16>;
95
96    ///
97    /// Reads 2 bytes and treats them as an u16 in native byte order
98    ///
99    fn read_i16_ne(&mut self) -> io::Result<i16>;
100
101    ///
102    /// Reads 4 bytes and treats them as an i32 in little endian byte order
103    ///
104    fn read_i32_le(&mut self) -> io::Result<i32>;
105
106    ///
107    /// Reads 4 bytes and treats them as an i32 in big endian byte order
108    ///
109    fn read_i32_be(&mut self) -> io::Result<i32>;
110
111    ///
112    /// Reads 4 bytes and treats them as an i32 in native byte order
113    ///
114    fn read_i32_ne(&mut self) -> io::Result<i32>;
115
116    ///
117    /// Reads 8 bytes and treats them as an i64 in little endian byte order
118    ///
119    fn read_i64_le(&mut self) -> io::Result<i64>;
120
121    ///
122    /// Reads 8 bytes and treats them as an i64 in big endian byte order
123    ///
124    fn read_i64_be(&mut self) -> io::Result<i64>;
125
126    ///
127    /// Reads 8 bytes and treats them as an u64 in native byte order
128    ///
129    fn read_i64_ne(&mut self) -> io::Result<i64>;
130
131    ///
132    /// Reads 16 bytes and treats them as an u128 in little endian byte order
133    ///
134    fn read_i128_le(&mut self) -> io::Result<i128>;
135
136    ///
137    /// Reads 16 bytes and treats them as an u128 in big endian byte order
138    ///
139    fn read_i128_be(&mut self) -> io::Result<i128>;
140
141    ///
142    /// Reads 16 bytes and treats them as an u128 in native byte order
143    ///
144    fn read_i128_ne(&mut self) -> io::Result<i128>;
145
146    ///
147    /// Reads 4 bytes and treats them as an f32 in little endian byte order
148    ///
149    fn read_f32_le(&mut self) -> io::Result<f32>;
150
151    ///
152    /// Reads 4 bytes and treats them as an f32 in big endian byte order
153    ///
154    fn read_f32_be(&mut self) -> io::Result<f32>;
155
156    ///
157    /// Reads 4 bytes and treats them as an f32 in native byte order
158    ///
159    fn read_f32_ne(&mut self) -> io::Result<f32>;
160
161    ///
162    /// Reads 8 bytes and treats them as an f64 in little endian byte order
163    ///
164    fn read_f64_le(&mut self) -> io::Result<f64>;
165
166    ///
167    /// Reads 8 bytes and treats them as an f64 in big endian byte order
168    ///
169    fn read_f64_be(&mut self) -> io::Result<f64>;
170
171    ///
172    /// Reads 8 bytes and treats them as an f64 in native byte order
173    ///
174    fn read_f64_ne(&mut self) -> io::Result<f64>;
175
176    ///
177    /// Reads bytes to fill the slice. Each byte is treated as a u8.
178    ///
179    fn read_u8_slice(&mut self, slice: &mut [u8]) -> io::Result<()>;
180
181    ///
182    /// Reads bytes to fill the slice. Each byte is treated as a i8.
183    ///
184    fn read_i8_slice(&mut self, slice: &mut [i8]) -> io::Result<()>;
185
186    ///
187    /// Reads bytes to fill the slice. Each group of 2 bytes is treated as an u16 in little endian
188    ///
189    fn read_u16_slice_le(&mut self, slice: &mut [u16]) -> io::Result<()>;
190
191    ///
192    /// Reads bytes to fill the slice. Each group of 2 bytes is treated as an u16 in big endian
193    ///
194    fn read_u16_slice_be(&mut self, slice: &mut [u16]) -> io::Result<()>;
195
196    ///
197    /// Reads bytes to fill the slice. Each group of 2 bytes is treated as an u16 in native endian
198    ///
199    fn read_u16_slice_ne(&mut self, slice: &mut [u16]) -> io::Result<()>;
200
201    ///
202    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an u32 in little endian
203    ///
204    fn read_u32_slice_le(&mut self, slice: &mut [u32]) -> io::Result<()>;
205
206    ///
207    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an u32 in big endian
208    ///
209    fn read_u32_slice_be(&mut self, slice: &mut [u32]) -> io::Result<()>;
210
211    ///
212    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an u32 in native endian
213    ///
214    fn read_u32_slice_ne(&mut self, slice: &mut [u32]) -> io::Result<()>;
215
216    ///
217    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an u64 in little endian
218    ///
219    fn read_u64_slice_le(&mut self, slice: &mut [u64]) -> io::Result<()>;
220
221    ///
222    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an u64 in big endian
223    ///
224    fn read_u64_slice_be(&mut self, slice: &mut [u64]) -> io::Result<()>;
225
226    ///
227    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an u64 in native endian
228    ///
229    fn read_u64_slice_ne(&mut self, slice: &mut [u64]) -> io::Result<()>;
230
231    ///
232    /// Reads bytes to fill the slice. Each group of 16 bytes is treated as an u128 in little endian
233    ///
234    fn read_u128_slice_le(&mut self, slice: &mut [u128]) -> io::Result<()>;
235
236    ///
237    /// Reads bytes to fill the slice. Each group of 16 bytes is treated as an u128 in big endian
238    ///
239    fn read_u128_slice_be(&mut self, slice: &mut [u128]) -> io::Result<()>;
240
241    ///
242    /// Reads bytes to fill the slice. Each group of 16 bytes is treated as an u128 in native endian
243    ///
244    fn read_u128_slice_ne(&mut self, slice: &mut [u128]) -> io::Result<()>;
245
246    ///
247    /// Reads bytes to fill the slice. Each group of 2 bytes is treated as an i16 in little endian
248    ///
249    fn read_i16_slice_le(&mut self, slice: &mut [i16]) -> io::Result<()>;
250
251    ///
252    /// Reads bytes to fill the slice. Each group of 2 bytes is treated as an i16 in big endian
253    ///
254    fn read_i16_slice_be(&mut self, slice: &mut [i16]) -> io::Result<()>;
255
256    ///
257    /// Reads bytes to fill the slice. Each group of 2 bytes is treated as an i16 in native endian
258    ///
259    fn read_i16_slice_ne(&mut self, slice: &mut [i16]) -> io::Result<()>;
260
261    ///
262    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an i32 in little endian
263    ///
264    fn read_i32_slice_le(&mut self, slice: &mut [i32]) -> io::Result<()>;
265
266    ///
267    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an i32 in big endian
268    ///
269    fn read_i32_slice_be(&mut self, slice: &mut [i32]) -> io::Result<()>;
270
271    ///
272    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an i32 in native endian
273    ///
274    fn read_i32_slice_ne(&mut self, slice: &mut [i32]) -> io::Result<()>;
275
276    ///
277    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an i64 in little endian
278    ///
279    fn read_i64_slice_le(&mut self, slice: &mut [i64]) -> io::Result<()>;
280
281    ///
282    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an i64 in big endian
283    ///
284    fn read_i64_slice_be(&mut self, slice: &mut [i64]) -> io::Result<()>;
285
286    ///
287    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an i64 in native endian
288    ///
289    fn read_i64_slice_ne(&mut self, slice: &mut [i64]) -> io::Result<()>;
290
291    ///
292    /// Reads bytes to fill the slice. Each group of 16 bytes is treated as an i128 in little endian
293    ///
294    fn read_i128_slice_le(&mut self, slice: &mut [i128]) -> io::Result<()>;
295
296    ///
297    /// Reads bytes to fill the slice. Each group of 16 bytes is treated as an i128 in big endian
298    ///
299    fn read_i128_slice_be(&mut self, slice: &mut [i128]) -> io::Result<()>;
300
301    ///
302    /// Reads bytes to fill the slice. Each group of 16 bytes is treated as an i128 in native endian
303    ///
304    fn read_i128_slice_ne(&mut self, slice: &mut [i128]) -> io::Result<()>;
305
306    ///
307    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an f64 in little endian
308    ///
309    fn read_f64_slice_le(&mut self, slice: &mut [f64]) -> io::Result<()>;
310
311    ///
312    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an f64 in big endian
313    ///
314    fn read_f64_slice_be(&mut self, slice: &mut [f64]) -> io::Result<()>;
315
316    ///
317    /// Reads bytes to fill the slice. Each group of 8 bytes is treated as an f64 in native endian
318    ///
319    fn read_f64_slice_ne(&mut self, slice: &mut [f64]) -> io::Result<()>;
320
321    ///
322    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an f32 in little endian
323    ///
324    fn read_f32_slice_le(&mut self, slice: &mut [f32]) -> io::Result<()>;
325
326    ///
327    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an f32 in big endian
328    ///
329    fn read_f32_slice_be(&mut self, slice: &mut [f32]) -> io::Result<()>;
330
331    ///
332    /// Reads bytes to fill the slice. Each group of 4 bytes is treated as an f32 in native endian
333    ///
334    fn read_f32_slice_ne(&mut self, slice: &mut [f32]) -> io::Result<()>;
335
336    ///
337    /// reads size amount of data into a new Vec<u8>
338    ///
339    fn read_u8_vec(&mut self, size: usize) -> io::Result<Vec<u8>>;
340
341    ///
342    /// reads size amount of data into a new Vec<i8>
343    ///
344    fn read_i8_vec(&mut self, size: usize) -> io::Result<Vec<i8>>;
345
346    ///
347    /// reads size amount of data into a new Vec.
348    /// Each group of 2 bytes is treated as an u16 in little endian
349    /// Size refers to the size of the Vec not the amount of bytes.
350    ///
351    fn read_u16_vec_le(&mut self, size: usize) -> io::Result<Vec<u16>>;
352
353    ///
354    /// reads size amount of data into a new Vec.
355    /// Each group of 2 bytes is treated as an u16 in big endian
356    /// Size refers to the size of the Vec not the amount of bytes.
357    ///
358    fn read_u16_vec_be(&mut self, size: usize) -> io::Result<Vec<u16>>;
359
360    ///
361    /// reads size amount of data into a new Vec.
362    /// Each group of 2 bytes is treated as an u16 in native endian
363    /// Size refers to the size of the Vec not the amount of bytes.
364    ///
365    fn read_u16_vec_ne(&mut self, size: usize) -> io::Result<Vec<u16>>;
366
367    ///
368    /// reads size amount of data into a new Vec.
369    /// Each group of 4 bytes is treated as an u32 in little endian
370    /// Size refers to the size of the Vec not the amount of bytes.
371    ///
372    fn read_u32_vec_le(&mut self, size: usize) -> io::Result<Vec<u32>>;
373
374    ///
375    /// reads size amount of data into a new Vec.
376    /// Each group of 4 bytes is treated as an u32 in big endian
377    /// Size refers to the size of the Vec not the amount of bytes.
378    ///
379    fn read_u32_vec_be(&mut self, size: usize) -> io::Result<Vec<u32>>;
380
381    ///
382    /// reads size amount of data into a new Vec.
383    /// Each group of 4 bytes is treated as an u32 in native endian
384    /// Size refers to the size of the Vec not the amount of bytes.
385    ///
386    fn read_u32_vec_ne(&mut self, size: usize) -> io::Result<Vec<u32>>;
387
388    ///
389    /// reads size amount of data into a new Vec.
390    /// Each group of 8 bytes is treated as an u64 in little endian
391    /// Size refers to the size of the Vec not the amount of bytes.
392    ///
393    fn read_u64_vec_le(&mut self, size: usize) -> io::Result<Vec<u64>>;
394
395    ///
396    /// reads size amount of data into a new Vec.
397    /// Each group of 8 bytes is treated as an u64 in big endian
398    /// Size refers to the size of the Vec not the amount of bytes.
399    ///
400    fn read_u64_vec_be(&mut self, size: usize) -> io::Result<Vec<u64>>;
401
402    ///
403    /// reads size amount of data into a new Vec.
404    /// Each group of 8 bytes is treated as an u64 in native endian
405    /// Size refers to the size of the Vec not the amount of bytes.
406    ///
407    fn read_u64_vec_ne(&mut self, size: usize) -> io::Result<Vec<u64>>;
408
409    ///
410    /// reads size amount of data into a new Vec.
411    /// Each group of 16 bytes is treated as an u128 in little endian
412    /// Size refers to the size of the Vec not the amount of bytes.
413    ///
414    fn read_u128_vec_le(&mut self, size: usize) -> io::Result<Vec<u128>>;
415
416    ///
417    /// reads size amount of data into a new Vec.
418    /// Each group of 16 bytes is treated as an u128 in big endian
419    /// Size refers to the size of the Vec not the amount of bytes.
420    ///
421    fn read_u128_vec_be(&mut self, size: usize) -> io::Result<Vec<u128>>;
422
423    ///
424    /// reads size amount of data into a new Vec.
425    /// Each group of 16 bytes is treated as an u128 in native endian.
426    /// Size refers to the size of the Vec not the amount of bytes.
427    ///
428    fn read_u128_vec_ne(&mut self, size: usize) -> io::Result<Vec<u128>>;
429
430    ///
431    /// reads size amount of data into a new Vec.
432    /// Each group of 2 bytes is treated as an i16 in little endian.
433    /// Size refers to the size of the Vec not the amount of bytes.
434    ///
435    fn read_i16_vec_le(&mut self, size: usize) -> io::Result<Vec<i16>>;
436
437    ///
438    /// reads size amount of data into a new Vec.
439    /// Each group of 2 bytes is treated as an i16 in big endian.
440    /// Size refers to the size of the Vec not the amount of bytes.
441    ///
442    fn read_i16_vec_be(&mut self, size: usize) -> io::Result<Vec<i16>>;
443
444    ///
445    /// reads size amount of data into a new Vec.
446    /// Each group of 2 bytes is treated as an i16 in native endian.
447    /// Size refers to the size of the Vec not the amount of bytes.
448    ///
449    fn read_i16_vec_ne(&mut self, size: usize) -> io::Result<Vec<i16>>;
450
451    ///
452    /// reads size amount of data into a new Vec.
453    /// Each group of 4 bytes is treated as an i32 in little endian.
454    /// Size refers to the size of the Vec not the amount of bytes.
455    ///
456    fn read_i32_vec_le(&mut self, size: usize) -> io::Result<Vec<i32>>;
457
458    ///
459    /// reads size amount of data into a new Vec.
460    /// Each group of 4 bytes is treated as an i32 in big endian.
461    /// Size refers to the size of the Vec not the amount of bytes.
462    ///
463    fn read_i32_vec_be(&mut self, size: usize) -> io::Result<Vec<i32>>;
464
465    ///
466    /// reads size amount of data into a new Vec.
467    /// Each group of 4 bytes is treated as an i32 in native endian.
468    /// Size refers to the size of the Vec not the amount of bytes.
469    ///
470    fn read_i32_vec_ne(&mut self, size: usize) -> io::Result<Vec<i32>>;
471
472    ///
473    /// reads size amount of data into a new Vec.
474    /// Each group of 8 bytes is treated as an i64 in little endian.
475    /// Size refers to the size of the Vec not the amount of bytes.
476    ///
477    fn read_i64_vec_le(&mut self, size: usize) -> io::Result<Vec<i64>>;
478
479    ///
480    /// reads size amount of data into a new Vec.
481    /// Each group of 8 bytes is treated as an i64 in big endian.
482    /// Size refers to the size of the Vec not the amount of bytes.
483    ///
484    fn read_i64_vec_be(&mut self, size: usize) -> io::Result<Vec<i64>>;
485
486    ///
487    /// reads size amount of data into a new Vec.
488    /// Each group of 8 bytes is treated as an i64 in native endian.
489    /// Size refers to the size of the Vec not the amount of bytes.
490    ///
491    fn read_i64_vec_ne(&mut self, size: usize) -> io::Result<Vec<i64>>;
492
493    ///
494    /// reads size amount of data into a new Vec.
495    /// Each group of 16 bytes is treated as an i128 in little endian.
496    /// Size refers to the size of the Vec not the amount of bytes.
497    ///
498    fn read_i128_vec_le(&mut self, size: usize) -> io::Result<Vec<i128>>;
499
500    ///
501    /// reads size amount of data into a new Vec.
502    /// Each group of 16 bytes is treated as an i128 in big endian.
503    /// Size refers to the size of the Vec not the amount of bytes.
504    ///
505    fn read_i128_vec_be(&mut self, size: usize) -> io::Result<Vec<i128>>;
506
507    ///
508    /// reads size amount of data into a new Vec.
509    /// Each group of 16 bytes is treated as an i128 in native endian.
510    /// Size refers to the size of the Vec not the amount of bytes.
511    ///
512    fn read_i128_vec_ne(&mut self, size: usize) -> io::Result<Vec<i128>>;
513
514    ///
515    /// reads size amount of data into a new Vec.
516    /// Each group of 8 bytes is treated as an f64 in little endian.
517    /// Size refers to the size of the Vec not the amount of bytes.
518    ///
519    fn read_f64_vec_le(&mut self, size: usize) -> io::Result<Vec<f64>>;
520
521    ///
522    /// reads size amount of data into a new Vec.
523    /// Each group of 8 bytes is treated as an f64 in big endian.
524    /// Size refers to the size of the Vec not the amount of bytes.
525    ///
526    fn read_f64_vec_be(&mut self, size: usize) -> io::Result<Vec<f64>>;
527
528    ///
529    /// reads size amount of data into a new Vec.
530    /// Each group of 8 bytes is treated as an f64 in native endian.
531    /// Size refers to the size of the Vec not the amount of bytes.
532    ///
533    fn read_f64_vec_ne(&mut self, size: usize) -> io::Result<Vec<f64>>;
534
535    ///
536    /// reads size amount of data into a new Vec.
537    /// Each group of 8 bytes is treated as an f32 in little endian.
538    /// Size refers to the size of the Vec not the amount of bytes.
539    ///
540    fn read_f32_vec_le(&mut self, size: usize) -> io::Result<Vec<f32>>;
541
542    ///
543    /// reads size amount of data into a new Vec.
544    /// Each group of 8 bytes is treated as an f32 in big endian.
545    /// Size refers to the size of the Vec not the amount of bytes.
546    ///
547    fn read_f32_vec_be(&mut self, size: usize) -> io::Result<Vec<f32>>;
548
549    ///
550    /// reads size amount of data into a new Vec.
551    /// Each group of 8 bytes is treated as an f32 in native endian.
552    /// Size refers to the size of the Vec not the amount of bytes.
553    ///
554    fn read_f32_vec_ne(&mut self, size: usize) -> io::Result<Vec<f32>>;
555}
556
557macro_rules! define_endian_numeric_read_functions {
558    ($type:ty, $le_name:ident ,$conv_le:expr, $be_name:ident ,$conv_be:expr, $ne_name:ident) => {
559        #[cfg(target_endian = "big")]
560        fn $le_name(&mut self) -> io::Result<$type> {
561            let mut buf = [0u8; size_of::<$type>()];
562            self.read_exact(buf.as_mut_slice())?;
563            return Ok($conv_le(buf));
564        }
565
566        #[cfg(target_endian = "little")]
567        fn $le_name(&mut self) -> io::Result<$type> {
568            let mut v: $type = 0 as $type;
569            self.read_exact(unsafe { std::slice::from_raw_parts_mut::<u8>(((&mut v) as *mut $type).cast(), size_of::<$type>()) })?;
570            return Ok(v);
571        }
572
573        #[cfg(target_endian = "big")]
574        fn $be_name(&mut self) -> io::Result<$type> {
575            let mut v: $type = 0 as $type;
576            self.read_exact(unsafe { std::slice::from_raw_parts_mut::<u8>(((&mut v) as *mut $type).cast(), size_of::<$type>()) })?;
577            return Ok(v);
578        }
579
580
581        #[cfg(target_endian = "little")]
582        fn $be_name(&mut self) -> io::Result<$type> {
583            let mut buf = [0u8; size_of::<$type>()];
584            self.read_exact(buf.as_mut_slice())?;
585            return Ok($conv_be(buf));
586        }
587
588        fn $ne_name(&mut self) -> io::Result<$type> {
589            let mut v: $type = 0 as $type;
590            self.read_exact(unsafe { std::slice::from_raw_parts_mut::<u8>(((&mut v) as *mut $type).cast(), size_of::<$type>()) })?;
591            return Ok(v);
592        }
593    };
594}
595
596macro_rules! define_endian_read_slice_with_helper_functions {
597    ($type:ty, $helper:ty, $le_name:ident, $be_name:ident, $ne_name:ident) => {
598        #[cfg(target_endian = "little")]
599        fn $le_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
600            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
601            return self.read_exact(sl);
602        }
603
604        #[cfg(target_endian = "big")]
605        fn $le_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
606            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
607            let helper :&mut [$helper] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) };
608            self.read_exact(sl)?;
609            for i in 0 .. helper.len() {
610                helper[i] = helper[i].to_le();
611            }
612            return Ok(());
613        }
614
615        #[cfg(target_endian = "little")]
616        fn $be_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
617            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
618            let helper :&mut [$helper] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) };
619            self.read_exact(sl)?;
620            for i in 0 .. helper.len() {
621                helper[i] = helper[i].to_be();
622            }
623            return Ok(());
624        }
625
626        #[cfg(target_endian = "big")]
627        fn $be_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
628            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
629            return self.read_exact(sl);
630        }
631
632        fn $ne_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
633            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
634            return self.read_exact(sl);
635        }
636    }
637}
638macro_rules! define_endian_read_slice_functions {
639    ($type:ty, $le_name:ident, $be_name:ident, $ne_name:ident) => {
640        #[cfg(target_endian = "little")]
641        fn $le_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
642            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
643            return self.read_exact(sl);
644        }
645
646        #[cfg(target_endian = "big")]
647        fn $le_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
648            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
649            self.read_exact(sl)?;
650            for i in 0 .. slice.len() {
651                slice[i] = slice[i].to_le();
652            }
653            return Ok(());
654        }
655
656
657        #[cfg(target_endian = "little")]
658        fn $be_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
659            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
660            self.read_exact(sl)?;
661            for i in 0 .. slice.len() {
662                slice[i] = slice[i].to_be();
663            }
664            return Ok(());
665        }
666
667        #[cfg(target_endian = "big")]
668        fn $be_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
669            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
670            return self.read_exact(sl);
671        }
672
673        fn $ne_name(&mut self, slice: &mut [$type]) -> io::Result<()> {
674            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len() * size_of::<$type>()) };
675            return self.read_exact(sl);
676        }
677    }
678}
679
680macro_rules! define_endian_read_vec_with_helper_functions {
681    ($type:ty, $helper:ty, $le_name:ident, $be_name:ident, $ne_name:ident) => {
682        fn $ne_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
683            let mut vec : Vec<$type> = vec![0 as $type; size];
684            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
685            self.read_exact(sl)?;
686            return Ok(vec);
687        }
688
689        #[cfg(target_endian = "little")]
690        fn $le_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
691            let mut vec : Vec<$type> = vec![0 as $type; size];
692            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
693            self.read_exact(sl)?;
694            return Ok(vec);
695        }
696
697        #[cfg(target_endian = "big")]
698        fn $le_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
699            let mut vec : Vec<$type> = vec![0 as $type; size];
700            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
701            let helper :&mut [$helper] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len()) };
702            self.read_exact(sl)?;
703            for i in 0 .. helper.len() {
704                helper[i] = helper[i].to_le();
705            }
706            return Ok(vec);
707        }
708
709
710        #[cfg(target_endian = "little")]
711        fn $be_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
712            let mut vec : Vec<$type> = vec![0 as $type; size];
713            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
714            let helper :&mut [$helper] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len()) };
715            self.read_exact(sl)?;
716            for i in 0 .. helper.len() {
717                helper[i] = helper[i].to_be();
718            }
719            return Ok(vec);
720        }
721
722        #[cfg(target_endian = "big")]
723        fn $be_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
724            let mut vec : Vec<$type> = vec![0 as $type; size];
725            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
726            self.read_exact(sl)?;
727            return Ok(vec);
728        }
729    }
730}
731
732macro_rules! define_endian_read_vec_functions {
733    ($type:ty, $le_name:ident, $be_name:ident, $ne_name:ident) => {
734        fn $ne_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
735            let mut vec : Vec<$type> = vec![0 as $type; size];
736            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
737            self.read_exact(sl)?;
738            return Ok(vec);
739        }
740
741        #[cfg(target_endian = "little")]
742        fn $le_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
743            let mut vec : Vec<$type> = vec![0 as $type; size];
744            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
745            self.read_exact(sl)?;
746            return Ok(vec);
747        }
748
749        #[cfg(target_endian = "big")]
750        fn $le_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
751            let mut vec : Vec<$type> = vec![0 as $type; size];
752            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
753            self.read_exact(sl)?;
754            for i in 0 .. vec.len() {
755                vec[i] = vec[i].to_le();
756            }
757            return Ok(vec);
758        }
759
760
761        #[cfg(target_endian = "little")]
762        fn $be_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
763            let mut vec : Vec<$type> = vec![0 as $type; size];
764            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
765            self.read_exact(sl)?;
766            for i in 0 .. vec.len() {
767                vec[i] = vec[i].to_be();
768            }
769            return Ok(vec);
770        }
771
772        #[cfg(target_endian = "big")]
773        fn $be_name(&mut self, size: usize) -> io::Result<Vec<$type>> {
774            let mut vec : Vec<$type> = vec![0 as $type; size];
775            let sl :&mut [u8] = unsafe { std::slice::from_raw_parts_mut(vec.as_mut_ptr().cast(), vec.len() * size_of::<$type>()) };
776            self.read_exact(sl)?;
777            return Ok(vec);
778        }
779    }
780}
781
782impl<T> NumRead for T where T: Read {
783    fn read_bool(&mut self) -> io::Result<bool> {
784        let mut v = [0u8];
785        self.read_exact(v.as_mut_slice())?;
786        return Ok(v[0] != 0);
787    }
788
789    fn read_u8(&mut self) -> io::Result<u8> {
790        let mut v = [0u8];
791        self.read_exact(v.as_mut_slice())?;
792        return Ok(v[0]);
793    }
794
795    fn read_i8(&mut self) -> io::Result<i8> {
796        let mut v = [0u8];
797        self.read_exact(v.as_mut_slice())?;
798        return Ok(v[0] as i8);
799    }
800
801    define_endian_numeric_read_functions!(u16, read_u16_le, u16::from_le_bytes, read_u16_be, u16::from_be_bytes, read_u16_ne);
802    define_endian_numeric_read_functions!(u32, read_u32_le, u32::from_le_bytes, read_u32_be, u32::from_be_bytes, read_u32_ne);
803    define_endian_numeric_read_functions!(u64, read_u64_le, u64::from_le_bytes, read_u64_be, u64::from_be_bytes, read_u64_ne);
804    define_endian_numeric_read_functions!(u128, read_u128_le, u128::from_le_bytes, read_u128_be, u128::from_be_bytes, read_u128_ne);
805
806    define_endian_numeric_read_functions!(i16, read_i16_le, i16::from_le_bytes, read_i16_be, i16::from_be_bytes, read_i16_ne);
807    define_endian_numeric_read_functions!(i32, read_i32_le, i32::from_le_bytes, read_i32_be, i32::from_be_bytes, read_i32_ne);
808    define_endian_numeric_read_functions!(i64, read_i64_le, i64::from_le_bytes, read_i64_be, i64::from_be_bytes, read_i64_ne);
809    define_endian_numeric_read_functions!(i128, read_i128_le, i128::from_le_bytes, read_i128_be, i128::from_be_bytes, read_i128_ne);
810
811    define_endian_numeric_read_functions!(f32, read_f32_le, f32::from_le_bytes, read_f32_be, f32::from_be_bytes, read_f32_ne);
812    define_endian_numeric_read_functions!(f64, read_f64_le, f64::from_le_bytes, read_f64_be, f64::from_be_bytes, read_f64_ne);
813
814
815
816
817    fn read_u8_slice(&mut self, slice: &mut [u8]) -> io::Result<()> {
818        return self.read_exact(slice);
819    }
820
821    fn read_i8_slice(&mut self, slice: &mut [i8]) -> io::Result<()> {
822        let sl: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) };
823        return self.read_exact(sl);
824    }
825
826    define_endian_read_slice_functions!(u16, read_u16_slice_le, read_u16_slice_be, read_u16_slice_ne);
827    define_endian_read_slice_functions!(u32, read_u32_slice_le, read_u32_slice_be, read_u32_slice_ne);
828    define_endian_read_slice_functions!(u64, read_u64_slice_le, read_u64_slice_be, read_u64_slice_ne);
829    define_endian_read_slice_functions!(u128, read_u128_slice_le, read_u128_slice_be, read_u128_slice_ne);
830
831    define_endian_read_slice_functions!(i16, read_i16_slice_le, read_i16_slice_be, read_i16_slice_ne);
832    define_endian_read_slice_functions!(i32, read_i32_slice_le, read_i32_slice_be, read_i32_slice_ne);
833    define_endian_read_slice_functions!(i64, read_i64_slice_le, read_i64_slice_be, read_i64_slice_ne);
834    define_endian_read_slice_functions!(i128, read_i128_slice_le, read_i128_slice_be, read_i128_slice_ne);
835
836    fn read_u8_vec(&mut self, size: usize) -> io::Result<Vec<u8>> {
837        let mut x = vec![0u8; size];
838        self.read_exact(x.as_mut_slice())?;
839        return Ok(x);
840    }
841    fn read_i8_vec(&mut self, size: usize) -> io::Result<Vec<i8>> {
842        let mut x = vec![0i8; size];
843        self.read_i8_slice(x.as_mut_slice())?;
844        return Ok(x);
845    }
846
847    define_endian_read_vec_functions!(u16, read_u16_vec_le, read_u16_vec_be, read_u16_vec_ne);
848    define_endian_read_vec_functions!(u32, read_u32_vec_le, read_u32_vec_be, read_u32_vec_ne);
849    define_endian_read_vec_functions!(u64, read_u64_vec_le, read_u64_vec_be, read_u64_vec_ne);
850    define_endian_read_vec_functions!(u128, read_u128_vec_le, read_u128_vec_be, read_u128_vec_ne);
851
852    define_endian_read_vec_functions!(i16, read_i16_vec_le, read_i16_vec_be, read_i16_vec_ne);
853    define_endian_read_vec_functions!(i32, read_i32_vec_le, read_i32_vec_be, read_i32_vec_ne);
854    define_endian_read_vec_functions!(i64, read_i64_vec_le, read_i64_vec_be, read_i64_vec_ne);
855    define_endian_read_vec_functions!(i128, read_i128_vec_le, read_i128_vec_be, read_i128_vec_ne);
856
857    define_endian_read_slice_with_helper_functions!(f64, u64, read_f64_slice_le, read_f64_slice_be, read_f64_slice_ne);
858    define_endian_read_slice_with_helper_functions!(f32, u32, read_f32_slice_le, read_f32_slice_be, read_f32_slice_ne);
859    define_endian_read_vec_with_helper_functions!(f64, u64, read_f64_vec_le, read_f64_vec_be, read_f64_vec_ne);
860    define_endian_read_vec_with_helper_functions!(f32, u32, read_f32_vec_le, read_f32_vec_be, read_f32_vec_ne);
861
862}
863
864mod private {
865    use std::io::Read;
866
867    impl <T> Sealed for T where T: Read {}
868    pub trait Sealed {
869
870    }
871}