@@ -8,20 +8,28 @@ use crate::{
8
8
world:: World ,
9
9
} ;
10
10
use fallible:: * ;
11
- use std:: { fmt:: Debug , marker:: PhantomData } ;
11
+ use std:: { borrow :: Cow , fmt:: Debug , marker:: PhantomData } ;
12
12
13
13
pub use config:: * ;
14
14
pub use fallible:: { FallibleCommand , InfallibleCommand } ;
15
15
16
+ pub struct CommandArgs < ' a > {
17
+ pub world : & ' a mut World ,
18
+ pub system_name : & ' a Cow < ' static , str > ,
19
+ }
20
+
21
+ // IMPL DerefMut<Target = World> for CommandArgs to help with breaking backwards compatilibilty?
22
+
16
23
/// A [`World`] mutation.
17
24
pub trait Command : Send + Sync + ' static {
18
- fn write ( self : Box < Self > , world : & mut World ) ;
25
+ fn write ( self : Box < Self > , args : CommandArgs ) ;
19
26
}
20
27
21
28
/// A queue of [`Command`]s.
22
29
#[ derive( Default ) ]
23
30
pub struct CommandQueue {
24
- commands : Vec < Box < dyn Command > > ,
31
+ pub ( crate ) commands : Vec < Box < dyn Command > > ,
32
+ pub ( crate ) system_name : Cow < ' static , str > ,
25
33
}
26
34
27
35
impl CommandQueue {
@@ -30,7 +38,10 @@ impl CommandQueue {
30
38
pub fn apply ( & mut self , world : & mut World ) {
31
39
world. flush ( ) ;
32
40
for command in self . commands . drain ( ..) {
33
- command. write ( world) ;
41
+ command. write ( CommandArgs {
42
+ world,
43
+ system_name : & self . system_name ,
44
+ } ) ;
34
45
}
35
46
}
36
47
@@ -336,8 +347,8 @@ impl<T> InfallibleCommand for Spawn<T>
336
347
where
337
348
T : Bundle ,
338
349
{
339
- fn write ( self , world : & mut World ) {
340
- world. spawn ( ) . insert_bundle ( self . bundle ) ;
350
+ fn write ( self , args : CommandArgs ) {
351
+ args . world . spawn ( ) . insert_bundle ( self . bundle ) ;
341
352
}
342
353
}
343
354
@@ -354,8 +365,8 @@ where
354
365
I : IntoIterator + Send + Sync + ' static ,
355
366
I :: Item : Bundle ,
356
367
{
357
- fn write ( self , world : & mut World ) {
358
- world. spawn_batch ( self . bundles_iter ) ;
368
+ fn write ( self , args : CommandArgs ) {
369
+ args . world . spawn_batch ( self . bundles_iter ) ;
359
370
}
360
371
}
361
372
@@ -372,8 +383,8 @@ pub struct DespawnError {
372
383
impl FallibleCommand for Despawn {
373
384
type Error = DespawnError ;
374
385
375
- fn try_write ( self , world : & mut World ) -> Result < ( ) , Self :: Error > {
376
- if world. despawn ( self . entity ) {
386
+ fn try_write ( self , args : CommandArgs ) -> Result < ( ) , Self :: Error > {
387
+ if args . world . despawn ( self . entity ) {
377
388
Ok ( ( ) )
378
389
} else {
379
390
Err ( DespawnError {
@@ -408,8 +419,8 @@ where
408
419
{
409
420
type Error = InsertBundleError < T > ;
410
421
411
- fn try_write ( self , world : & mut World ) -> Result < ( ) , Self :: Error > {
412
- if let Some ( mut entity_mut) = world. get_entity_mut ( self . entity ) {
422
+ fn try_write ( self , args : CommandArgs ) -> Result < ( ) , Self :: Error > {
423
+ if let Some ( mut entity_mut) = args . world . get_entity_mut ( self . entity ) {
413
424
entity_mut. insert_bundle ( self . bundle ) ;
414
425
Ok ( ( ) )
415
426
} else {
@@ -447,8 +458,8 @@ where
447
458
{
448
459
type Error = InsertError < T > ;
449
460
450
- fn try_write ( self , world : & mut World ) -> Result < ( ) , Self :: Error > {
451
- match world. get_entity_mut ( self . entity ) {
461
+ fn try_write ( self , args : CommandArgs ) -> Result < ( ) , Self :: Error > {
462
+ match args . world . get_entity_mut ( self . entity ) {
452
463
Some ( mut entity) => {
453
464
entity. insert ( self . component ) ;
454
465
Ok ( ( ) )
@@ -487,8 +498,8 @@ where
487
498
{
488
499
type Error = RemoveError < T > ;
489
500
490
- fn try_write ( self , world : & mut World ) -> Result < ( ) , Self :: Error > {
491
- if let Some ( mut entity_mut) = world. get_entity_mut ( self . entity ) {
501
+ fn try_write ( self , args : CommandArgs ) -> Result < ( ) , Self :: Error > {
502
+ if let Some ( mut entity_mut) = args . world . get_entity_mut ( self . entity ) {
492
503
entity_mut. remove :: < T > ( ) ;
493
504
Ok ( ( ) )
494
505
} else {
@@ -526,8 +537,8 @@ where
526
537
{
527
538
type Error = RemoveBundleError < T > ;
528
539
529
- fn try_write ( self , world : & mut World ) -> Result < ( ) , Self :: Error > {
530
- if let Some ( mut entity_mut) = world. get_entity_mut ( self . entity ) {
540
+ fn try_write ( self , args : CommandArgs ) -> Result < ( ) , Self :: Error > {
541
+ if let Some ( mut entity_mut) = args . world . get_entity_mut ( self . entity ) {
531
542
// remove intersection to gracefully handle components that were removed before running
532
543
// this command
533
544
entity_mut. remove_bundle_intersection :: < T > ( ) ;
@@ -546,8 +557,8 @@ pub struct InsertResource<T: Component> {
546
557
}
547
558
548
559
impl < T : Component > InfallibleCommand for InsertResource < T > {
549
- fn write ( self , world : & mut World ) {
550
- world. insert_resource ( self . resource ) ;
560
+ fn write ( self , args : CommandArgs ) {
561
+ args . world . insert_resource ( self . resource ) ;
551
562
}
552
563
}
553
564
@@ -570,8 +581,8 @@ impl<T> Debug for RemoveResourceError<T> {
570
581
impl < T : Component > FallibleCommand for RemoveResource < T > {
571
582
type Error = RemoveResourceError < T > ;
572
583
573
- fn try_write ( self , world : & mut World ) -> Result < ( ) , Self :: Error > {
574
- if world. remove_resource :: < T > ( ) . is_some ( ) {
584
+ fn try_write ( self , args : CommandArgs ) -> Result < ( ) , Self :: Error > {
585
+ if args . world . remove_resource :: < T > ( ) . is_some ( ) {
575
586
Ok ( ( ) )
576
587
} else {
577
588
Err ( RemoveResourceError {
@@ -584,10 +595,11 @@ impl<T: Component> FallibleCommand for RemoveResource<T> {
584
595
#[ cfg( test) ]
585
596
#[ allow( clippy:: float_cmp, clippy:: approx_constant) ]
586
597
mod tests {
598
+ use super :: { CommandError , Despawn } ;
587
599
use crate :: {
588
600
component:: { ComponentDescriptor , StorageType } ,
589
601
entity:: Entity ,
590
- system:: { CommandQueue , Commands } ,
602
+ system:: { CommandQueue , Commands , In , IntoSystem } ,
591
603
world:: World ,
592
604
} ;
593
605
use std:: sync:: {
@@ -731,9 +743,12 @@ mod tests {
731
743
732
744
let mut try_despawn = |e| {
733
745
let invoked_clone = invoked. clone ( ) ;
734
- commands. entity ( e) . despawn ( ) . on_failure ( move |_| {
735
- invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
736
- } ) ;
746
+ commands. entity ( e) . despawn ( ) . on_failure (
747
+ Box :: new ( move |_: In < CommandError < Despawn > > | {
748
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
749
+ } )
750
+ . system ( ) ,
751
+ ) ;
737
752
} ;
738
753
739
754
try_despawn ( invalid_entity) ;
0 commit comments