Skip to main content

jni_bindgen_reflection/
io.rs

1use crate::*;
2use std::io::*;
3
4pub fn read_u1(r: &mut impl Read) -> Result<u8> {
5    let mut buffer = [0u8; 1];
6    r.read_exact(&mut buffer)?;
7    Ok(buffer[0])
8}
9
10pub fn read_u2(r: &mut impl Read) -> Result<u16> {
11    let mut buffer = [0u8; 2];
12    r.read_exact(&mut buffer)?;
13    Ok(u16::from_be_bytes(buffer))
14}
15
16pub fn read_u4(r: &mut impl Read) -> Result<u32> {
17    let mut buffer = [0u8; 4];
18    r.read_exact(&mut buffer)?;
19    Ok(u32::from_be_bytes(buffer))
20}
21
22pub fn read_u8(r: &mut impl Read) -> Result<u64> {
23    let mut buffer = [0u8; 8];
24    r.read_exact(&mut buffer)?;
25    Ok(u64::from_be_bytes(buffer))
26}
27
28//pub fn read_i1(r: &mut impl Read) -> Result< i8> { read_u1(r).map(|u| u as  i8) }
29//pub fn read_i2(r: &mut impl Read) -> Result<i16> { read_u2(r).map(|u| u as i16) }
30pub fn read_i4(r: &mut impl Read) -> Result<i32> { read_u4(r).map(|u| u as i32) }
31pub fn read_i8(r: &mut impl Read) -> Result<i64> { read_u8(r).map(|u| u as i64) }
32
33pub fn read_ignore(read: &mut impl Read, bytes: usize) -> io::Result<()> {
34    let mut info = Vec::new();
35    info.resize(bytes, 0u8);
36    read.read_exact(&mut info[..])?;
37    Ok(())
38}
39
40
41
42// I/O errors here "probably" indicate bugs in class parsing - break at the callsite for ease of debugging.
43// The other alternative is you're parsing bad/corrupt classes, so good luck with that.
44
45#[macro_export]
46macro_rules! io_data_error {
47    ($($arg:tt)*) => {{
48        use bugsalot::*;
49        let message = format!($($arg)*);
50        bug!("{}", &message);
51        std::io::Error::new(std::io::ErrorKind::InvalidData, message)
52    }};
53}
54
55#[macro_export]
56macro_rules! io_data_err {
57    ($($arg:tt)*) => { Err($crate::io_data_error!($($arg)*)) };
58}
59
60macro_rules! io_assert {
61    ($condition:expr) => {
62        if !$condition {
63            return $crate::io_data_err!("Assertion failed: {}", stringify!($condition));
64        }
65    };
66    ($condition:expr, $($arg:tt)*) => {
67        if !$condition {
68            return $crate::io_data_err!($($arg)*);
69        }
70    };
71}