typed_io/
typed_read.rs

1use crate::{Endianness, ReadConstant, ReadEndianness, ReadVariable};
2use std::io::{Read, Result};
3
4pub trait TypedRead: Read + Sized {
5    // ReadEndianness
6    fn read_endianness_with_callback<F, T: ReadEndianness>(
7        &mut self,
8        callback: F,
9        endianness: Endianness,
10    ) -> Result<T>
11    where
12        F: Fn(&mut [u8]),
13    {
14        <T>::read_endianness_bytes_with_callback(self, callback, endianness)
15    }
16    fn read_be<T: ReadEndianness>(&mut self) -> Result<T> {
17        <T>::read_be_bytes(self)
18    }
19    fn read_le<T: ReadEndianness>(&mut self) -> Result<T> {
20        <T>::read_le_bytes(self)
21    }
22    fn read_ne<T: ReadEndianness>(&mut self) -> Result<T> {
23        <T>::read_ne_bytes(self)
24    }
25
26    // ReadVariable
27    fn read_var_len_with_callback<F, T: ReadVariable>(
28        &mut self,
29        callback: F,
30        length: usize,
31    ) -> Result<T>
32    where
33        F: Fn(&mut [u8]),
34    {
35        <T>::read_variable_bytes_with_callback(self, callback, length)
36    }
37    fn read_var_len<T: ReadVariable>(&mut self, length: usize) -> Result<T> {
38        <T>::read_variable_bytes(self, length)
39    }
40
41    // ReadConstant
42    fn read_const_len_with_callback<F, T: ReadConstant<LENGTH>, const LENGTH: usize>(
43        &mut self,
44        callback: F,
45    ) -> Result<T>
46    where
47        F: Fn(&mut [u8]),
48    {
49        <T>::read_constant_bytes_with_callback(self, callback)
50    }
51    fn read_const_len<T: ReadConstant<LENGTH>, const LENGTH: usize>(&mut self) -> Result<T> {
52        <T>::read_constant_bytes(self)
53    }
54}
55
56impl<R: Read> TypedRead for R {}
57
58#[cfg(test)]
59mod tests {
60    use super::TypedRead;
61    use std::io;
62
63    #[test]
64    fn read_string_for() {
65        let mut v: &[u8] = &[98, 107, 98, 107, 98];
66        let bytes: [u8; 5] = v.read_const_len().unwrap();
67        let s = std::str::from_utf8(&bytes).unwrap();
68        assert_eq!(s, "bkbkb");
69        let mut v: &[u8] = &[
70            0xE3, 0x81, 0xB3, 0xE3, 0x81, 0x8B, 0xE3, 0x81, 0xB3, 0xE3, 0x81, 0x8B, 0xE3, 0x81,
71            0xB3,
72        ];
73        let s: String = v.read_const_len::<_, 15>().unwrap();
74        assert_eq!(s, "びかびかび");
75
76        let mut v: &[u8] = &[
77            0xE3, 0x81, 0xB3, 0xE3, 0x81, 0x8B, 0xE3, 0x81, 0xB3, 0xE3, 0x81, 0x8B, 0xE3, 0x81,
78            0xB3,
79        ];
80        let s: String = v.read_var_len(15).unwrap();
81        assert_eq!(s, "びかびかび");
82    }
83
84    #[test]
85    fn read_unsigned_integer_type() -> io::Result<()> {
86        let mut v: &[u8] = &[
87            1, 2, 0, 3, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88            0, 5,
89        ];
90
91        let u8_bytes: u8 = v.read_le()?;
92        assert_eq!(u8_bytes, 1);
93
94        let u16_bytes: u16 = v.read_le()?;
95        assert_eq!(u16_bytes, 2);
96
97        let u32_bytes: u32 = v.read_le()?;
98        assert_eq!(u32_bytes, 3);
99
100        let u64_bytes: u64 = v.read_le()?;
101        assert_eq!(u64_bytes, 4);
102
103        let u128_bytes: u128 = v.read_be()?;
104        assert_eq!(u128_bytes, 5);
105
106        Ok(())
107    }
108}