-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
system_param.rs
324 lines (296 loc) · 11.4 KB
/
system_param.rs
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
323
324
use crate::{
ArchetypeComponent, ChangedRes, Commands, Fetch, FromResources, Local, Or, Query, QueryAccess,
QueryFilter, QuerySet, QueryTuple, Res, ResMut, Resource, ResourceIndex, Resources,
SystemState, TypeAccess, World, WorldQuery,
};
use parking_lot::Mutex;
use std::{any::TypeId, sync::Arc};
pub struct In<Input>(pub Input);
impl<Input> SystemParam<Input> for In<Input> {
#[inline]
unsafe fn get_param(
input: &mut Option<Input>,
_system_state: &mut SystemState,
_world: &World,
_resources: &Resources,
) -> Option<Self> {
Some(In(input.take().unwrap()))
}
fn init(_system_state: &mut SystemState, _world: &World, _resources: &mut Resources) {}
}
pub trait SystemParam<Input>: Sized {
fn init(system_state: &mut SystemState, world: &World, resources: &mut Resources);
/// # Safety
/// This call might access any of the input parameters in an unsafe way. Make sure the data access is safe in
/// the context of the system scheduler
unsafe fn get_param(
input: &mut Option<Input>,
system_state: &mut SystemState,
world: &World,
resources: &Resources,
) -> Option<Self>;
}
impl<'a, Q: WorldQuery, F: QueryFilter, Input> SystemParam<Input> for Query<'a, Q, F> {
#[inline]
unsafe fn get_param(
_input: &mut Option<Input>,
system_state: &mut SystemState,
world: &World,
_resources: &Resources,
) -> Option<Self> {
let query_index = system_state.current_query_index;
let world: &'a World = std::mem::transmute(world);
let archetype_component_access: &'a TypeAccess<ArchetypeComponent> =
std::mem::transmute(&system_state.query_archetype_component_accesses[query_index]);
system_state.current_query_index += 1;
Some(Query::new(world, archetype_component_access))
}
fn init(system_state: &mut SystemState, _world: &World, _resources: &mut Resources) {
system_state
.query_archetype_component_accesses
.push(TypeAccess::default());
let access = QueryAccess::union(vec![Q::Fetch::access(), F::access()]);
system_state.query_accesses.push(vec![access]);
system_state
.query_type_names
.push(std::any::type_name::<Q>());
}
}
impl<T: QueryTuple, Input> SystemParam<Input> for QuerySet<T> {
#[inline]
unsafe fn get_param(
_input: &mut Option<Input>,
system_state: &mut SystemState,
world: &World,
_resources: &Resources,
) -> Option<Self> {
let query_index = system_state.current_query_index;
system_state.current_query_index += 1;
Some(QuerySet::new(
world,
&system_state.query_archetype_component_accesses[query_index],
))
}
fn init(system_state: &mut SystemState, _world: &World, _resources: &mut Resources) {
system_state
.query_archetype_component_accesses
.push(TypeAccess::default());
system_state.query_accesses.push(T::get_accesses());
system_state
.query_type_names
.push(std::any::type_name::<T>());
}
}
impl<'a, Input> SystemParam<Input> for &'a mut Commands {
fn init(system_state: &mut SystemState, world: &World, _resources: &mut Resources) {
system_state
.commands
.set_entity_reserver(world.get_entity_reserver())
}
#[inline]
unsafe fn get_param(
_input: &mut Option<Input>,
system_state: &mut SystemState,
_world: &World,
_resources: &Resources,
) -> Option<Self> {
let commands: &'a mut Commands = std::mem::transmute(&mut system_state.commands);
Some(commands)
}
}
impl<Input> SystemParam<Input> for Arc<Mutex<Commands>> {
fn init(system_state: &mut SystemState, world: &World, _resources: &mut Resources) {
system_state.arc_commands.get_or_insert_with(|| {
let mut commands = Commands::default();
commands.set_entity_reserver(world.get_entity_reserver());
Arc::new(Mutex::new(commands))
});
}
#[inline]
unsafe fn get_param(
_input: &mut Option<Input>,
system_state: &mut SystemState,
_world: &World,
_resources: &Resources,
) -> Option<Self> {
Some(system_state.arc_commands.as_ref().unwrap().clone())
}
}
impl<'a, T: Resource, Input> SystemParam<Input> for Res<'a, T> {
fn init(system_state: &mut SystemState, _world: &World, _resources: &mut Resources) {
if system_state.resource_access.is_write(&TypeId::of::<T>()) {
panic!(
"System `{}` has a `Res<{res}>` parameter that conflicts with \
another parameter with mutable access to the same `{res}` resource.",
system_state.name,
res = std::any::type_name::<T>()
);
}
system_state.resource_access.add_read(TypeId::of::<T>());
}
#[inline]
unsafe fn get_param(
_input: &mut Option<Input>,
_system_state: &mut SystemState,
_world: &World,
resources: &Resources,
) -> Option<Self> {
Some(Res::new(
resources.get_unsafe_ref::<T>(ResourceIndex::Global),
))
}
}
impl<'a, T: Resource, Input> SystemParam<Input> for ResMut<'a, T> {
fn init(system_state: &mut SystemState, _world: &World, _resources: &mut Resources) {
// If a system already has access to the resource in another parameter, then we fail early.
// e.g. `fn(Res<Foo>, ResMut<Foo>)` or `fn(ResMut<Foo>, ResMut<Foo>)` must not be allowed.
if system_state
.resource_access
.is_read_or_write(&TypeId::of::<T>())
{
panic!(
"System `{}` has a `ResMut<{res}>` parameter that conflicts with \
another parameter to the same `{res}` resource. `ResMut` must have unique access.",
system_state.name,
res = std::any::type_name::<T>()
);
}
system_state.resource_access.add_write(TypeId::of::<T>());
}
#[inline]
unsafe fn get_param(
_input: &mut Option<Input>,
_system_state: &mut SystemState,
_world: &World,
resources: &Resources,
) -> Option<Self> {
let (value, _added, mutated) =
resources.get_unsafe_ref_with_added_and_mutated::<T>(ResourceIndex::Global);
Some(ResMut::new(value, mutated))
}
}
impl<'a, T: Resource, Input> SystemParam<Input> for ChangedRes<'a, T> {
fn init(system_state: &mut SystemState, _world: &World, _resources: &mut Resources) {
if system_state.resource_access.is_write(&TypeId::of::<T>()) {
panic!(
"System `{}` has a `ChangedRes<{res}>` parameter that conflicts with \
another parameter with mutable access to the same `{res}` resource.",
system_state.name,
res = std::any::type_name::<T>()
);
}
system_state.resource_access.add_read(TypeId::of::<T>());
}
#[inline]
unsafe fn get_param(
_input: &mut Option<Input>,
_system_state: &mut SystemState,
_world: &World,
resources: &Resources,
) -> Option<Self> {
let (value, added, mutated) =
resources.get_unsafe_ref_with_added_and_mutated::<T>(ResourceIndex::Global);
if *added.as_ptr() || *mutated.as_ptr() {
Some(ChangedRes::new(value))
} else {
None
}
}
}
impl<'a, T: Resource + FromResources, Input> SystemParam<Input> for Local<'a, T> {
fn init(system_state: &mut SystemState, _world: &World, resources: &mut Resources) {
if system_state
.local_resource_access
.is_read_or_write(&TypeId::of::<T>())
{
panic!(
"System `{}` has multiple parameters requesting access to a local resource of type `{}`. \
There may be at most one `Local` parameter per resource type.",
system_state.name,
std::any::type_name::<T>()
);
}
// A resource could have been already initialized by another system with
// `Commands::insert_local_resource` or `Resources::insert_local`
if resources.get_local::<T>(system_state.id).is_none() {
let value = T::from_resources(resources);
resources.insert_local(system_state.id, value);
}
system_state
.local_resource_access
.add_write(TypeId::of::<T>());
}
#[inline]
unsafe fn get_param(
_input: &mut Option<Input>,
system_state: &mut SystemState,
_world: &World,
resources: &Resources,
) -> Option<Self> {
Some(Local::new(resources, system_state.id))
}
}
macro_rules! impl_system_param_tuple {
($($param: ident),*) => {
#[allow(unused_variables)]
impl<Input, $($param: SystemParam<Input>),*> SystemParam<Input> for ($($param,)*) {
fn init(system_state: &mut SystemState, world: &World, resources: &mut Resources) {
$($param::init(system_state, world, resources);)*
}
#[inline]
unsafe fn get_param(
input: &mut Option<Input>,
system_state: &mut SystemState,
world: &World,
resources: &Resources,
) -> Option<Self> {
Some(($($param::get_param(input, system_state, world, resources)?,)*))
}
}
#[allow(unused_variables)]
#[allow(unused_mut)]
#[allow(non_snake_case)]
impl<Input, $($param: SystemParam<Input>),*> SystemParam<Input> for Or<($(Option<$param>,)*)> {
fn init(system_state: &mut SystemState, world: &World, resources: &mut Resources) {
$($param::init(system_state, world, resources);)*
}
#[inline]
unsafe fn get_param(
input: &mut Option<Input>,
system_state: &mut SystemState,
world: &World,
resources: &Resources,
) -> Option<Self> {
let mut has_some = false;
$(
let $param = $param::get_param(input, system_state, world, resources);
if $param.is_some() {
has_some = true;
}
)*
if has_some {
Some(Or(($($param,)*)))
} else {
None
}
}
}
};
}
impl_system_param_tuple!();
impl_system_param_tuple!(A);
impl_system_param_tuple!(A, B);
impl_system_param_tuple!(A, B, C);
impl_system_param_tuple!(A, B, C, D);
impl_system_param_tuple!(A, B, C, D, E);
impl_system_param_tuple!(A, B, C, D, E, F);
impl_system_param_tuple!(A, B, C, D, E, F, G);
impl_system_param_tuple!(A, B, C, D, E, F, G, H);
impl_system_param_tuple!(A, B, C, D, E, F, G, H, I);
impl_system_param_tuple!(A, B, C, D, E, F, G, H, I, J);
impl_system_param_tuple!(A, B, C, D, E, F, G, H, I, J, K);
impl_system_param_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);
impl_system_param_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M);
impl_system_param_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
impl_system_param_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
impl_system_param_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);