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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::{
archetype,
archetype::DeserializeColumn,
component::Component,
registry::{
Null,
Registry,
},
};
use alloc::{
format,
string::String,
vec::Vec,
};
use core::{
any::type_name,
mem::ManuallyDrop,
};
use serde::{
de,
de::SeqAccess,
Deserialize,
};
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
pub trait Sealed<'de>: Registry + 'de {
/// # Safety
/// When called externally, the `Registry` `R` provided to the method must by the same as the
/// `Registry` on which this method is being called.
///
/// When called internally, the `identifier_iter` must have the same amount of bits left as
/// there are components remaining.
unsafe fn deserialize_components_by_column<R, V>(
components: &mut Vec<(*mut u8, usize)>,
length: usize,
seq: &mut V,
identifier_iter: archetype::identifier::Iter<R>,
current_index: usize,
identifier: archetype::IdentifierRef<R>,
) -> Result<(), V::Error>
where
R: Sealed<'de>,
V: SeqAccess<'de>;
/// # Safety
/// `components` must contain the same number of values as there are set bits in the
/// `identifier_iter`.
///
/// Each `(*mut u8, usize)` in `components` must be the pointer and capacity respectively of a
/// `Vec<C>` of size `length`, where `C` is the component corresponding to the set bit in
/// `identifier_iter`.
///
/// When called externally, the `Registry` `R` provided to the method must by the same as the
/// `Registry` on which this method is being called.
///
/// When called internally, the `identifier_iter` must have the same amount of bits left as
/// there are components remaining.
unsafe fn deserialize_components_by_row<R, V>(
components: &mut [(*mut u8, usize)],
length: usize,
seq: &mut V,
identifier_iter: archetype::identifier::Iter<R>,
current_index: usize,
identifier: archetype::IdentifierRef<R>,
) -> Result<(), V::Error>
where
R: Sealed<'de>,
V: SeqAccess<'de>;
/// # Safety
/// When called externally, the `Registry` `R` provided to the method must by the same as the
/// `Registry` on which this method is being called.
///
/// When called internally, the `identifier_iter` must have the same amount of bits left as
/// there are components remaining.
unsafe fn expected_row_component_names<R>(
names: &mut String,
identifier_iter: archetype::identifier::Iter<R>,
) where
R: Registry;
}
impl<'de> Sealed<'de> for Null {
unsafe fn deserialize_components_by_column<R, V>(
_components: &mut Vec<(*mut u8, usize)>,
_length: usize,
_seq: &mut V,
_identifier_iter: archetype::identifier::Iter<R>,
_current_index: usize,
_identifier: archetype::IdentifierRef<R>,
) -> Result<(), V::Error>
where
R: Sealed<'de>,
V: SeqAccess<'de>,
{
Ok(())
}
unsafe fn deserialize_components_by_row<R, V>(
_components: &mut [(*mut u8, usize)],
_length: usize,
_seq: &mut V,
_identifier_iter: archetype::identifier::Iter<R>,
_current_index: usize,
_identifier: archetype::IdentifierRef<R>,
) -> Result<(), V::Error>
where
R: Sealed<'de>,
V: SeqAccess<'de>,
{
Ok(())
}
unsafe fn expected_row_component_names<R>(
_names: &mut String,
_identifier_iter: archetype::identifier::Iter<R>,
) where
R: Registry,
{
}
}
impl<'de, C, R> Sealed<'de> for (C, R)
where
C: Component + Deserialize<'de>,
R: Sealed<'de>,
{
unsafe fn deserialize_components_by_column<R_, V>(
components: &mut Vec<(*mut u8, usize)>,
length: usize,
seq: &mut V,
mut identifier_iter: archetype::identifier::Iter<R_>,
current_index: usize,
identifier: archetype::IdentifierRef<R_>,
) -> Result<(), V::Error>
where
R_: Sealed<'de>,
V: SeqAccess<'de>,
{
if
// SAFETY: `identifier_iter` is guaranteed by the safety contract of this method to
// return a value for every component within the registry.
unsafe { identifier_iter.next().unwrap_unchecked() } {
// TODO: Better error messages?
let component_column = seq
.next_element_seed(DeserializeColumn::<C>::new(length))?
.ok_or_else(|| {
de::Error::invalid_length(
current_index + 1,
&format!("columns for each of (entity::Identifier{})", {
let mut names = String::new();
// SAFETY: The identifier iter passed here contains the same amount of
// bits as there are components in `R_`.
unsafe {
R_::expected_row_component_names(&mut names, identifier.iter());
}
names
})
.as_str(),
)
})?;
components.push((component_column.0.cast::<u8>(), component_column.1));
}
// SAFETY: Since one bit was consumed from `identifier_iter`, it still has the same number
// of bits remaining as there are components remaining.
unsafe {
R::deserialize_components_by_column(
components,
length,
seq,
identifier_iter,
current_index + 1,
identifier,
)
}
}
unsafe fn deserialize_components_by_row<R_, V>(
mut components: &mut [(*mut u8, usize)],
length: usize,
seq: &mut V,
mut identifier_iter: archetype::identifier::Iter<R_>,
current_index: usize,
identifier: archetype::IdentifierRef<R_>,
) -> Result<(), V::Error>
where
R_: Sealed<'de>,
V: SeqAccess<'de>,
{
if
// SAFETY: `identifier_iter` is guaranteed by the safety contract of this method to
// return a value for every component within the registry.
unsafe { identifier_iter.next().unwrap_unchecked() } {
let component_column =
// SAFETY: `components` is guaranteed to have the same number of values as there
// set bits in `identifier_iter`. Since a bit must have been set to enter this
// block, there must be at least one component column.
unsafe { components.get_unchecked_mut(0) };
let mut v = ManuallyDrop::new(
// SAFETY: The pointer and capacity are guaranteed by the safety contract of this
// method to define a valid `Vec<C>` of length `length`.
unsafe {
Vec::<C>::from_raw_parts(
component_column.0.cast::<C>(),
length,
component_column.1,
)
},
);
v.push(seq.next_element()?.ok_or_else(|| {
// TODO: the length returned here is incorrect.
de::Error::invalid_length(
current_index + 1,
&format!("(entity::Identifier{})", {
let mut names = String::new();
// SAFETY: The identifier iter passed here contains the same amount of bits
// as there are components in `R_`.
unsafe { R_::expected_row_component_names(&mut names, identifier.iter()) };
names
})
.as_str(),
)
})?);
component_column.0 = v.as_mut_ptr().cast::<u8>();
component_column.1 = v.capacity();
components =
// SAFETY: `components` is guaranteed to have the same number of values as there
// set bits in `identifier_iter`. Since a bit must have been set to enter this
// block, there must be at least one component column.
unsafe { components.get_unchecked_mut(1..) };
}
// SAFETY: At this point, one bit of `identifier_iter` has been consumed. There are two
// possibilities here: either the bit was set or it was not.
//
// If the bit was set, then the `components` slice will no longer include the first value,
// which means the slice will still contain the same number of pointer and capacity tuples
// as there are set bits in `identifier_iter`. Additionally, since the first value was
// removed from the slice, which corresponded to the component identified by the consumed
// bit, all remaining component values will still correspond to valid `Vec<C>`s identified
// by the remaining set bits in `identifier_iter`.
//
// If the bit was not set, then `components` is unaltered, and there are still the same
// number of elements as there are set bits in `identifier_iter`, which still make valid
// `Vec<C>`s for each `C` identified by the remaining set bits in `identifier_iter`.
//
// Furthermore, regardless of whether the bit was set or not, `R` is one component smaller
// than `(C, R)`, and since `identifier_iter` has had one bit consumed, it still has the
// same number of bits remaining as `R` has components remaining.
unsafe {
R::deserialize_components_by_row(
components,
length,
seq,
identifier_iter,
current_index + 1,
identifier,
)
}
}
unsafe fn expected_row_component_names<R_>(
names: &mut String,
mut identifier_iter: archetype::identifier::Iter<R_>,
) where
R_: Registry,
{
if
// SAFETY: `identifier_iter` is guaranteed by the safety contract of this method to
// return a value for every component within the registry.
unsafe { identifier_iter.next().unwrap_unchecked() } {
names.push_str(", ");
names.push_str(type_name::<C>());
}
// SAFETY: Since one bit was consumed in `identifier_iter`, there are the same number of
// bits remaining as there are components in `R`.
unsafe { R::expected_row_component_names(names, identifier_iter) }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Registry;
use alloc::vec;
use serde_derive::Deserialize;
#[derive(Deserialize)]
struct A;
#[derive(Deserialize)]
struct B;
#[derive(Deserialize)]
struct C;
type Registry = Registry!(A, B, C);
#[test]
fn expected_row_component_names_empty() {
let identifier = unsafe { archetype::Identifier::<Registry>::new(vec![0]) };
let mut result = String::new();
unsafe { Registry::expected_row_component_names(&mut result, identifier.iter()) };
assert_eq!(result, "");
}
#[test]
fn expected_row_component_names_some() {
let identifier = unsafe { archetype::Identifier::<Registry>::new(vec![5]) };
let mut result = String::new();
unsafe { Registry::expected_row_component_names(&mut result, identifier.iter()) };
assert_eq!(
result,
format!(", {}, {}", type_name::<A>(), type_name::<C>())
);
}
}