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
use crate::alloc::alloc::{alloc, dealloc, Layout};
use crate::alloc::vec::Vec;
use core::any::TypeId;
use core::ptr::{self, NonNull};
use hashbrown::hash_map::Entry;
use crate::archetype::{TypeIdMap, TypeInfo};
use crate::{align, Component, DynamicBundle};
pub struct EntityBuilder {
storage: NonNull<u8>,
layout: Layout,
cursor: usize,
info: Vec<(TypeInfo, usize)>,
ids: Vec<TypeId>,
indices: TypeIdMap<usize>,
}
impl EntityBuilder {
pub fn new() -> Self {
Self {
storage: NonNull::dangling(),
layout: Layout::from_size_align(0, 8).unwrap(),
cursor: 0,
info: Vec::new(),
ids: Vec::new(),
indices: Default::default(),
}
}
pub fn add<T: Component>(&mut self, component: T) -> &mut Self {
self.add_bundle((component,))
}
pub fn add_bundle(&mut self, bundle: impl DynamicBundle) -> &mut Self {
unsafe {
bundle.put(|ptr, ty| {
match self.indices.entry(ty.id()) {
Entry::Occupied(occupied) => {
let index = *occupied.get();
let (ty, offset) = self.info[index];
let storage = self.storage.as_ptr().add(offset);
ty.drop(storage);
ptr::copy_nonoverlapping(ptr, storage, ty.layout().size());
}
Entry::Vacant(vacant) => {
let offset = align(self.cursor, ty.layout().align());
let end = offset + ty.layout().size();
if end > self.layout.size() || ty.layout().align() > self.layout.align() {
let new_align = self.layout.align().max(ty.layout().align());
let (new_storage, new_layout) =
Self::grow(end, self.cursor, new_align, self.storage);
if self.layout.size() != 0 {
dealloc(self.storage.as_ptr(), self.layout);
}
self.storage = new_storage;
self.layout = new_layout;
}
let addr = self.storage.as_ptr().add(offset);
ptr::copy_nonoverlapping(ptr, addr, ty.layout().size());
vacant.insert(self.info.len());
self.info.push((ty, offset));
self.cursor = end;
}
}
});
}
self
}
unsafe fn grow(
min_size: usize,
cursor: usize,
align: usize,
storage: NonNull<u8>,
) -> (NonNull<u8>, Layout) {
let layout = Layout::from_size_align(min_size.next_power_of_two().max(64), align).unwrap();
let new_storage = NonNull::new_unchecked(alloc(layout));
ptr::copy_nonoverlapping(storage.as_ptr(), new_storage.as_ptr(), cursor);
(new_storage, layout)
}
pub fn build(&mut self) -> BuiltEntity<'_> {
self.info.sort_unstable_by_key(|x| x.0);
self.ids.extend(self.info.iter().map(|x| x.0.id()));
BuiltEntity { builder: self }
}
pub fn clear(&mut self) {
self.ids.clear();
self.indices.clear();
self.cursor = 0;
unsafe {
for (ty, offset) in self.info.drain(..) {
ty.drop(self.storage.as_ptr().add(offset));
}
}
}
}
unsafe impl Send for EntityBuilder {}
unsafe impl Sync for EntityBuilder {}
impl Drop for EntityBuilder {
fn drop(&mut self) {
self.clear();
if self.layout.size() != 0 {
unsafe {
dealloc(self.storage.as_ptr(), self.layout);
}
}
}
}
impl Default for EntityBuilder {
fn default() -> Self {
Self::new()
}
}
pub struct BuiltEntity<'a> {
builder: &'a mut EntityBuilder,
}
unsafe impl DynamicBundle for BuiltEntity<'_> {
fn with_ids<T>(&self, f: impl FnOnce(&[TypeId]) -> T) -> T {
f(&self.builder.ids)
}
#[doc(hidden)]
fn type_info(&self) -> Vec<TypeInfo> {
self.builder.info.iter().map(|x| x.0).collect()
}
unsafe fn put(self, mut f: impl FnMut(*mut u8, TypeInfo)) {
for (ty, offset) in self.builder.info.drain(..) {
let ptr = self.builder.storage.as_ptr().add(offset);
f(ptr, ty);
}
}
}
impl Drop for BuiltEntity<'_> {
fn drop(&mut self) {
self.builder.clear();
}
}