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
use crate::{
archetype,
archetype::Archetype,
entity,
registry,
};
use alloc::vec::Vec;
use core::{
fmt,
mem::ManuallyDrop,
};
struct Components<R>
where
R: registry::Debug,
{
pointers: Vec<*const u8>,
identifier: archetype::IdentifierRef<R>,
}
impl<R> fmt::Debug for Components<R>
where
R: registry::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug_map = f.debug_map();
// SAFETY: `pointers` is guaranteed to contain the same number of values as there are
// components, each pointing to a valid component of type `C`. Also, `self.identifier` will
// yield the same number of values as there are components in `R`.
unsafe {
R::debug_components(&self.pointers, &mut debug_map, self.identifier.iter());
}
debug_map.finish()
}
}
struct Row<R>
where
R: registry::Debug,
{
identifier: entity::Identifier,
components: Components<R>,
}
impl<R> fmt::Debug for Row<R>
where
R: registry::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Row")
.field("identifier", &self.identifier)
.field("components", &self.components)
.finish()
}
}
impl<R> fmt::Debug for Archetype<R>
where
R: registry::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug_map = f.debug_map();
let entity_identifiers = ManuallyDrop::new(
// SAFETY: `self.identifiers`, together with `self.length`, are guaranteed to be the
// raw parts for a valid `Vec<entity::Identifier>`.
unsafe {
Vec::from_raw_parts(
self.entity_identifiers.0,
self.length,
self.entity_identifiers.1,
)
},
);
for i in 0..self.length {
let mut component_pointers = Vec::new();
// SAFETY: `self.components` contains the same number of values as
// `self.identifier.iter()` has bits. Each tuple in `components` corresponds to a valid
// `Vec<C>` for each component `C` with a length of `self.length`. Therefore, `i` will
// be within the bounds of each of those `Vec<C>`s as well.
unsafe {
R::extract_component_pointers(
i,
&self.components,
&mut component_pointers,
self.identifier.iter(),
);
}
debug_map.entry(
&i,
&Row::<R> {
// SAFETY: `entity_identifiers` is guaranteed to be of length `self.length`.
identifier: *unsafe { entity_identifiers.get_unchecked(i) },
components: Components {
pointers: component_pointers,
// SAFETY: This `IdentifierRef` will not outlive its referenced
// `Identifier`.
identifier: unsafe { self.identifier.as_ref() },
},
},
);
}
debug_map.finish()
}
}