1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use super::LiResult;
use crate::read_array_ty;
use crate::read_ty;
use crate::IValue;
use it_memory_traits::MemoryView;
use std::cell::Cell;
use std::marker::PhantomData;
pub struct MemoryReader<MV: MemoryView<Store>, Store: it_memory_traits::Store> {
pub(self) view: MV,
_phantom: PhantomData<Store>,
}
impl<MV: MemoryView<Store>, Store: it_memory_traits::Store> MemoryReader<MV, Store> {
pub fn new(view: MV) -> Self {
Self {
view,
_phantom: PhantomData,
}
}
pub fn sequential_reader(
&self,
store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
offset: u32,
size: u32,
) -> LiResult<SequentialReader<'_, MV, Store>> {
self.view.check_bounds(store, offset, size)?;
let seq_reader = SequentialReader::new(&self, offset);
Ok(seq_reader)
}
pub fn read_raw_u8_array(
&self,
store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
offset: u32,
elements_count: u32,
) -> LiResult<Vec<u8>> {
let reader = self.sequential_reader(store, offset, elements_count)?;
let mut result = Vec::with_capacity(elements_count as usize);
for _ in 0..elements_count {
let value = reader.read_u8(store);
result.push(value);
}
Ok(result)
}
pub fn read_bool_array(
&self,
store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
offset: u32,
elements_count: u32,
) -> LiResult<Vec<IValue>> {
let reader = self.sequential_reader(store, offset, elements_count)?;
let mut result = Vec::with_capacity(elements_count as usize);
for _ in 0..elements_count {
let value = reader.read_u8(store);
result.push(IValue::Boolean(value != 0));
}
Ok(result)
}
read_array_ty!(read_u8_array, u8, U8);
read_array_ty!(read_s8_array, i8, S8);
read_array_ty!(read_u16_array, u16, U16);
read_array_ty!(read_s16_array, i16, S16);
read_array_ty!(read_u32_array, u32, U32);
read_array_ty!(read_s32_array, i32, S32);
read_array_ty!(read_i32_array, i32, I32);
read_array_ty!(read_f32_array, f32, F32);
read_array_ty!(read_u64_array, u64, U64);
read_array_ty!(read_s64_array, i64, S64);
read_array_ty!(read_i64_array, i64, I64);
read_array_ty!(read_f64_array, f64, F64);
}
pub struct SequentialReader<'r, MV: MemoryView<Store>, Store: it_memory_traits::Store> {
reader: &'r MemoryReader<MV, Store>,
offset: Cell<u32>,
}
impl<'r, MV: MemoryView<Store>, Store: it_memory_traits::Store> SequentialReader<'r, MV, Store> {
fn new(reader: &'r MemoryReader<MV, Store>, offset: u32) -> Self {
Self {
reader,
offset: Cell::new(offset),
}
}
pub fn read_bool(
&self,
store: &mut <Store as it_memory_traits::Store>::ActualStore<'_>,
) -> bool {
self.read_u8(store) != 0
}
read_ty!(read_u8, u8, 1);
read_ty!(read_i8, i8, 1);
read_ty!(read_u16, u16, 2);
read_ty!(read_i16, i16, 2);
read_ty!(read_u32, u32, 4);
read_ty!(read_i32, i32, 4);
read_ty!(read_f32, f32, 4);
read_ty!(read_u64, u64, 8);
read_ty!(read_i64, i64, 8);
read_ty!(read_f64, f64, 8);
}