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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
use std::{
cell::UnsafeCell,
marker::PhantomData,
ops::{Deref, DerefMut},
};
use crate::{
change_detection::ChangeTicks,
query::{Query, QueryState, ReadOnlyWorldQuery, WorldQuery},
storage::Resource,
world::{ComponentId, FromWorld, World},
};
use super::{CommandQueue, Commands, FilteredAccess, SystemMeta};
pub use shiv_macro::SystemParam;
pub unsafe trait ReadOnlySystemParamFetch: for<'w, 's> SystemParamFetch<'w, 's> {}
pub trait SystemParam: Sized {
type Fetch: for<'w, 's> SystemParamFetch<'w, 's>;
}
pub type SystemParamItem<'w, 's, P> = <<P as SystemParam>::Fetch as SystemParamFetch<'w, 's>>::Item;
pub unsafe trait SystemParamState: Send + Sync + 'static {
fn init(world: &mut World, meta: &mut SystemMeta) -> Self;
#[inline]
fn apply(&mut self, _world: &mut World) {}
}
pub trait SystemParamFetch<'w, 's>: SystemParamState {
type Item: SystemParam<Fetch = Self>;
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item;
}
#[doc(hidden)]
#[derive(Debug)]
pub struct WorldFetch;
impl SystemParam for &World {
type Fetch = WorldFetch;
}
unsafe impl SystemParamState for WorldFetch {
fn init(_world: &mut World, meta: &mut SystemMeta) -> Self {
if meta.access.write_any() {
panic!("&World cannot be used as a parameter to a system that writes to any component or resource");
}
meta.access.read_all();
WorldFetch
}
}
impl<'w, 's> SystemParamFetch<'w, 's> for WorldFetch {
type Item = &'w World;
unsafe fn get_param(
&'s mut self,
_meta: &SystemMeta,
world: &'w World,
_change_tick: u32,
) -> Self::Item {
world
}
}
unsafe impl SystemParamState for CommandQueue {
fn init(_world: &mut World, _meta: &mut SystemMeta) -> Self {
Self::default()
}
fn apply(&mut self, world: &mut World) {
self.apply(world);
}
}
impl<'w, 's> SystemParamFetch<'w, 's> for CommandQueue {
type Item = Commands<'w, 's>;
unsafe fn get_param(
&'s mut self,
_meta: &SystemMeta,
world: &'w World,
_change_tick: u32,
) -> Self::Item {
Commands::new(self, world)
}
}
unsafe impl ReadOnlySystemParamFetch for CommandQueue {}
impl<'w, 's> SystemParam for Commands<'w, 's> {
type Fetch = CommandQueue;
}
unsafe impl<Q, F> SystemParamState for QueryState<Q, F>
where
Q: WorldQuery + 'static,
F: ReadOnlyWorldQuery + 'static,
{
#[inline]
fn init(world: &mut World, meta: &mut SystemMeta) -> Self {
let state = QueryState::new(world);
assert_access_compatibility(
std::any::type_name::<Q>(),
std::any::type_name::<F>(),
&state.filtered_access,
meta,
world,
);
meta.access.extend(&state.filtered_access);
state
}
}
impl<'w, 's, Q, F> SystemParamFetch<'w, 's> for QueryState<Q, F>
where
Q: WorldQuery + 'static,
F: ReadOnlyWorldQuery + 'static,
{
type Item = Query<'w, 's, Q, F>;
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item {
unsafe { Query::new(world, self, meta.last_change_tick, change_tick) }
}
}
impl<'w, 's, Q, F> SystemParam for Query<'w, 's, Q, F>
where
Q: WorldQuery + 'static,
F: ReadOnlyWorldQuery + 'static,
{
type Fetch = QueryState<Q, F>;
}
unsafe impl<Q, F> ReadOnlySystemParamFetch for QueryState<Q, F>
where
Q: ReadOnlyWorldQuery + 'static,
F: ReadOnlyWorldQuery + 'static,
{
}
fn assert_access_compatibility(
query_type: &'static str,
filter_type: &'static str,
access: &FilteredAccess<ComponentId>,
meta: &SystemMeta,
world: &World,
) {
let conflicts = access.get_conflicts(&meta.access);
if conflicts.is_empty() {
return;
}
let messages = conflicts
.into_iter()
.map(|id| {
let info = world.components.get(id).unwrap();
info.name()
})
.collect::<Vec<_>>();
let message = messages.join(", ");
panic!(
"Query<{}, {}> in system {} access component(s) {} that conflict with previous system parameters.",
query_type,
filter_type,
meta.name(),
message,
);
}
#[derive(Debug)]
pub struct Res<'w, T> {
value: &'w T,
ticks: &'w ChangeTicks,
last_change_tick: u32,
change_tick: u32,
}
impl<'w, T> Res<'w, T> {
#[inline]
pub fn is_added(&self) -> bool {
self.ticks.is_added(self.last_change_tick, self.change_tick)
}
#[inline]
pub fn is_changed(&self) -> bool {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
#[inline]
pub fn into_inner(self) -> &'w T {
self.value
}
pub fn ticks(&self) -> &ChangeTicks {
self.ticks
}
}
impl<'w, T> Deref for Res<'w, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<'w, T> AsRef<T> for Res<'w, T> {
#[inline]
fn as_ref(&self) -> &T {
self.value
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct ResState<T> {
component_id: ComponentId,
marker: PhantomData<T>,
}
unsafe impl<T: Resource> SystemParamState for ResState<T> {
fn init(world: &mut World, meta: &mut SystemMeta) -> Self {
let component_id = world.components.init_resource::<T>();
assert!(
!meta.access.has_write(component_id),
"Res<{}> in system {} conflicts with previous access in this query. Shared access cannot coexist with exclusive access.",
std::any::type_name::<T>(),
meta.name(),
);
meta.access.add_read(component_id);
Self {
component_id,
marker: PhantomData,
}
}
}
impl<'w, 's, T: Resource> SystemParamFetch<'w, 's> for ResState<T> {
type Item = Res<'w, T>;
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item {
let (ptr, ticks) = world
.storage
.resources
.get_with_ticks(self.component_id)
.unwrap_or_else(|| {
panic!(
"Resource requested by system {} does not exist: {}.",
meta.name(),
std::any::type_name::<T>()
)
});
Res {
value: unsafe { &*ptr.cast::<T>() },
ticks: unsafe { &*ticks },
last_change_tick: meta.last_change_tick,
change_tick,
}
}
}
impl<'w, T: Resource> SystemParam for Res<'w, T> {
type Fetch = ResState<T>;
}
unsafe impl<T: Resource> ReadOnlySystemParamFetch for ResState<T> {}
#[derive(Debug)]
pub struct ResMut<'w, T> {
value: &'w mut T,
ticks: &'w mut ChangeTicks,
last_change_tick: u32,
change_tick: u32,
}
impl<'w, T> ResMut<'w, T> {
#[inline]
pub fn is_added(&self) -> bool {
self.ticks.is_added(self.last_change_tick, self.change_tick)
}
#[inline]
pub fn is_changed(&self) -> bool {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
#[inline]
pub fn into_inner(self) -> &'w mut T {
self.value
}
}
impl<'w, T> Deref for ResMut<'w, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<'w, T> DerefMut for ResMut<'w, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.ticks.set_changed(self.change_tick);
self.value
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct ResMutState<T> {
component_id: ComponentId,
marker: PhantomData<T>,
}
unsafe impl<T: Resource> SystemParamState for ResMutState<T> {
fn init(world: &mut World, meta: &mut SystemMeta) -> Self {
let component_id = world.components.init_resource::<T>();
assert!(
!meta.access.has_read(component_id),
"ResMut<{}> in system {} conflicts with previous system parameters. Mutable resource access must be unique.",
std::any::type_name::<T>(),
meta.name(),
);
meta.access.add_write(component_id);
Self {
component_id,
marker: PhantomData,
}
}
}
impl<'w, 's, T: Resource> SystemParamFetch<'w, 's> for ResMutState<T> {
type Item = ResMut<'w, T>;
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item {
let (ptr, ticks) = world
.storage
.resources
.get_with_ticks(self.component_id)
.unwrap_or_else(|| {
panic!(
"Resource requested by system {} does not exist: {}.",
meta.name(),
std::any::type_name::<T>()
)
});
ResMut {
value: unsafe { &mut *ptr.cast::<T>() },
ticks: unsafe { &mut *ticks },
last_change_tick: meta.last_change_tick,
change_tick,
}
}
}
impl<'w, T: Resource> SystemParam for ResMut<'w, T> {
type Fetch = ResMutState<T>;
}
#[derive(Debug)]
pub struct ResInit<'w, T> {
value: &'w T,
ticks: &'w ChangeTicks,
last_change_tick: u32,
change_tick: u32,
}
impl<'w, T> ResInit<'w, T> {
#[inline]
pub fn is_added(&self) -> bool {
self.ticks.is_added(self.last_change_tick, self.change_tick)
}
#[inline]
pub fn is_changed(&self) -> bool {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
#[inline]
pub fn into_inner(self) -> &'w T {
self.value
}
}
impl<'w, T> Deref for ResInit<'w, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<'w, T> AsRef<T> for ResInit<'w, T> {
#[inline]
fn as_ref(&self) -> &T {
self.value
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct ResInitState<T> {
component_id: ComponentId,
marker: PhantomData<T>,
}
unsafe impl<T: Resource + FromWorld> SystemParamState for ResInitState<T> {
fn init(world: &mut World, meta: &mut SystemMeta) -> Self {
world.init_resource::<T>();
let component_id = world.components.init_resource::<T>();
assert!(
!meta.access.has_write(component_id),
"Res<{}> in system {} conflicts with previous access in this query. Shared access cannot coexist with exclusive access.",
std::any::type_name::<T>(),
meta.name(),
);
meta.access.add_read(component_id);
Self {
component_id,
marker: PhantomData,
}
}
}
impl<'w, 's, T: Resource + FromWorld> SystemParamFetch<'w, 's> for ResInitState<T> {
type Item = ResInit<'w, T>;
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item {
let (ptr, ticks) = world
.storage
.resources
.get_with_ticks(self.component_id)
.unwrap_or_else(|| {
panic!(
"Resource requested by system {} does not exist: {}.",
meta.name(),
std::any::type_name::<T>()
)
});
ResInit {
value: unsafe { &*ptr.cast::<T>() },
ticks: unsafe { &*ticks },
last_change_tick: meta.last_change_tick,
change_tick,
}
}
}
impl<'w, T: Resource + FromWorld> SystemParam for ResInit<'w, T> {
type Fetch = ResInitState<T>;
}
unsafe impl<T: Resource + FromWorld> ReadOnlySystemParamFetch for ResInitState<T> {}
#[derive(Debug)]
pub struct ResMutInit<'w, T> {
value: &'w mut T,
ticks: &'w mut ChangeTicks,
last_change_tick: u32,
change_tick: u32,
}
impl<'w, T> ResMutInit<'w, T> {
#[inline]
pub fn is_added(&self) -> bool {
self.ticks.is_added(self.last_change_tick, self.change_tick)
}
#[inline]
pub fn is_changed(&self) -> bool {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
#[inline]
pub fn into_inner(self) -> &'w mut T {
self.value
}
}
impl<'w, T> Deref for ResMutInit<'w, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<'w, T> DerefMut for ResMutInit<'w, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.ticks.set_changed(self.change_tick);
self.value
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct ResMutInitState<T> {
component_id: ComponentId,
marker: PhantomData<T>,
}
unsafe impl<T: Resource + FromWorld> SystemParamState for ResMutInitState<T> {
fn init(world: &mut World, meta: &mut SystemMeta) -> Self {
world.init_resource::<T>();
let component_id = world.components.init_resource::<T>();
assert!(
!meta.access.has_read(component_id),
"ResMut<{}> in system {} conflicts with previous system parameters. Mutable resource access must be unique.",
std::any::type_name::<T>(),
meta.name(),
);
meta.access.add_write(component_id);
Self {
component_id,
marker: PhantomData,
}
}
}
impl<'w, 's, T: Resource + FromWorld> SystemParamFetch<'w, 's> for ResMutInitState<T> {
type Item = ResMutInit<'w, T>;
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item {
let (ptr, ticks) = world
.storage
.resources
.get_with_ticks(self.component_id)
.unwrap_or_else(|| {
panic!(
"Resource requested by system {} does not exist: {}.",
meta.name(),
std::any::type_name::<T>()
)
});
ResMutInit {
value: unsafe { &mut *ptr.cast::<T>() },
ticks: unsafe { &mut *ticks },
last_change_tick: meta.last_change_tick,
change_tick,
}
}
}
impl<'w, T: Resource + FromWorld> SystemParam for ResMutInit<'w, T> {
type Fetch = ResMutInitState<T>;
}
#[doc(hidden)]
#[derive(Debug)]
pub struct OptionResState<T>(ResState<T>);
unsafe impl<T: Resource> ReadOnlySystemParamFetch for OptionResState<T> {}
unsafe impl<T: Resource> SystemParamState for OptionResState<T> {
#[inline]
fn init(world: &mut World, meta: &mut SystemMeta) -> Self {
Self(ResState::init(world, meta))
}
}
impl<'w, 's, T: Resource> SystemParamFetch<'w, 's> for OptionResState<T> {
type Item = Option<Res<'w, T>>;
#[inline]
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item {
if world.storage.resources.contains(self.0.component_id) {
Some(unsafe { ResState::get_param(&mut self.0, meta, world, change_tick) })
} else {
None
}
}
}
impl<'w, T: Resource> SystemParam for Option<Res<'w, T>> {
type Fetch = OptionResState<T>;
}
#[doc(hidden)]
#[derive(Debug)]
pub struct OptionResMutState<T>(ResMutState<T>);
unsafe impl<T: Resource> SystemParamState for OptionResMutState<T> {
#[inline]
fn init(world: &mut World, meta: &mut SystemMeta) -> Self {
Self(ResMutState::init(world, meta))
}
}
impl<'w, 's, T: Resource> SystemParamFetch<'w, 's> for OptionResMutState<T> {
type Item = Option<ResMut<'w, T>>;
#[inline]
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item {
if world.storage.resources.contains(self.0.component_id) {
Some(unsafe { ResMutState::get_param(&mut self.0, meta, world, change_tick) })
} else {
None
}
}
}
impl<'w, T: Resource> SystemParam for Option<ResMut<'w, T>> {
type Fetch = OptionResMutState<T>;
}
#[derive(Debug)]
pub struct Local<'s, T: FromWorld + Send + 'static> {
pub(crate) value: &'s mut T,
}
impl<'s, T> Deref for Local<'s, T>
where
T: FromWorld + Send + 'static,
{
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
self.value
}
}
impl<'s, T> DerefMut for Local<'s, T>
where
T: FromWorld + Send + 'static,
{
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.value
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct LocalState<T: Send + 'static> {
value: UnsafeCell<T>,
}
unsafe impl<T: Send> Sync for LocalState<T> {}
unsafe impl<T: FromWorld + Send + 'static> ReadOnlySystemParamFetch for LocalState<T> {}
unsafe impl<T: FromWorld + Send + 'static> SystemParamState for LocalState<T> {
#[inline]
fn init(world: &mut World, _meta: &mut SystemMeta) -> Self {
Self {
value: UnsafeCell::new(T::from_world(world)),
}
}
}
impl<'w, 's, T: FromWorld + Send + 'static> SystemParamFetch<'w, 's> for LocalState<T> {
type Item = Local<'s, T>;
#[inline]
unsafe fn get_param(
&'s mut self,
_meta: &SystemMeta,
_world: &'w World,
_change_tick: u32,
) -> Self::Item {
Local {
value: unsafe { &mut *self.value.get() },
}
}
}
impl<'w, T: FromWorld + Send + 'static> SystemParam for Local<'w, T> {
type Fetch = LocalState<T>;
}
macro_rules! impl_system_param {
(@ $($param:ident),*) => {
impl<$($param: SystemParam),*> SystemParam for ($($param,)*) {
type Fetch = ($($param::Fetch,)*);
}
#[allow(non_snake_case, unused)]
unsafe impl<$($param: SystemParamState),*> SystemParamState for ($($param,)*) {
#[inline]
fn init(world: &mut World, meta: &mut SystemMeta) -> Self {
($($param::init(world, meta),)*)
}
#[inline]
fn apply(&mut self, world: &mut World) {
let ($($param,)*) = self;
$($param.apply(world);)*
}
}
#[allow(non_snake_case, unused)]
impl<'w, 's, $($param: SystemParamFetch<'w, 's>),*> SystemParamFetch<'w, 's> for ($($param,)*) {
type Item = ($($param::Item,)*);
unsafe fn get_param(
&'s mut self,
meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item {
let ($($param,)*) = self;
unsafe { ($($param.get_param(meta, world, change_tick),)*) }
}
}
};
($start:ident $(,$ident:ident)*) => {
impl_system_param!(@ $start $(,$ident)*);
impl_system_param!($($ident),*);
};
() => {
impl_system_param!(@);
};
}
impl_system_param!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);