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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::marker::PhantomData;
use std::ptr;
use crate::abitype;
use crate::boxed::RecordClassId;
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum FieldType {
Bool = 0,
Char = 1,
Float = 2,
Int = 3,
InternedSym = 4,
Boxed = 5,
}
impl FieldType {
pub fn from_abi_type(abi_type: &abitype::AbiType) -> Self {
match abi_type {
abitype::AbiType::Bool => FieldType::Bool,
abitype::AbiType::Char => FieldType::Char,
abitype::AbiType::Float => FieldType::Float,
abitype::AbiType::Int => FieldType::Int,
abitype::AbiType::InternedSym => FieldType::InternedSym,
abitype::AbiType::Boxed(_) => FieldType::Boxed,
abitype::AbiType::Callback(_) => {
unimplemented!("callback record fields");
}
}
}
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Field {
offset: u32,
field_type: FieldType,
is_last: bool,
}
impl Field {
pub fn new(field_type: FieldType, offset: usize) -> Field {
Self {
offset: offset as u32,
field_type,
is_last: false,
}
}
pub fn field_type(self) -> FieldType {
self.field_type
}
pub fn offset(self) -> usize {
self.offset as usize
}
pub fn is_last(self) -> bool {
self.is_last
}
}
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct ClassRef<'a> {
fields: *const Field,
phantom_lifetime: PhantomData<&'a Field>,
}
impl<'a> ClassRef<'a> {
pub fn is_empty(self) -> bool {
self.fields.is_null()
}
pub fn field_iter(self) -> FieldIterator<'a> {
FieldIterator {
fields: self.fields,
phantom_lifetime: PhantomData,
}
}
}
#[derive(Clone)]
pub struct BoxedClass {
fields: Box<[Field]>,
}
impl BoxedClass {
pub fn from_fields(fields_iter: impl Iterator<Item = Field>) -> Self {
let mut fields: Box<[Field]> = fields_iter.collect();
if let Some(last_field) = fields.last_mut() {
last_field.is_last = true;
}
BoxedClass { fields }
}
pub fn as_ref(&self) -> ClassRef<'_> {
ClassRef {
fields: if self.fields.is_empty() {
std::ptr::null()
} else {
self.fields.as_ptr()
},
phantom_lifetime: PhantomData,
}
}
}
pub struct FieldIterator<'a> {
fields: *const Field,
phantom_lifetime: PhantomData<&'a Field>,
}
impl<'a> FieldIterator<'a> {
pub fn empty() -> FieldIterator<'static> {
FieldIterator {
fields: ptr::null(),
phantom_lifetime: PhantomData,
}
}
}
impl<'a> Iterator for FieldIterator<'a> {
type Item = Field;
fn next(&mut self) -> Option<Field> {
if self.fields.is_null() {
return None;
}
let next_field = unsafe { *self.fields };
if next_field.is_last {
self.fields = ptr::null();
} else {
self.fields = unsafe { self.fields.add(1) };
}
Some(next_field)
}
}
#[repr(C)]
#[derive(Clone)]
pub struct ClassMap {
const_classes: *const ClassRef<'static>,
dynamic_classes: Vec<BoxedClass>,
}
impl ClassMap {
const DYNAMIC_RECORD_CLASS_ID_BASE: RecordClassId = 1u32 << 30;
pub fn empty() -> ClassMap {
Self::with_const_classes(std::ptr::null())
}
pub(crate) fn with_const_classes(const_classes: *const ClassRef<'static>) -> ClassMap {
Self {
const_classes,
dynamic_classes: vec![],
}
}
pub fn push_dynamic_class(&mut self, boxed_class: BoxedClass) -> RecordClassId {
let record_class_id =
(self.dynamic_classes.len() as RecordClassId) + Self::DYNAMIC_RECORD_CLASS_ID_BASE;
self.dynamic_classes.push(boxed_class);
record_class_id
}
pub fn class_for_record_class_id(&self, record_class_id: RecordClassId) -> ClassRef<'_> {
if record_class_id >= Self::DYNAMIC_RECORD_CLASS_ID_BASE {
let dynamic_class_index =
(record_class_id - Self::DYNAMIC_RECORD_CLASS_ID_BASE) as usize;
self.dynamic_classes[dynamic_class_index].as_ref()
} else {
unsafe { *self.const_classes.add(record_class_id as usize) }
}
}
}