@@ -82,7 +82,7 @@ impl<'a> Commands<'a> {
82
82
/// }
83
83
/// # example_system.system();
84
84
/// ```
85
- pub fn spawn < ' b > ( & ' b mut self ) -> EntityCommands < ' a , ' b > {
85
+ pub fn spawn < ' this > ( & ' this mut self ) -> EntityCommands < ' a , ' this > {
86
86
let entity = self . entities . reserve_entity ( ) ;
87
87
EntityCommands {
88
88
entity,
@@ -130,7 +130,7 @@ impl<'a> Commands<'a> {
130
130
/// }
131
131
/// # example_system.system();
132
132
/// ```
133
- pub fn spawn_bundle < ' b , T : Bundle > ( & ' b mut self , bundle : T ) -> EntityCommands < ' a , ' b > {
133
+ pub fn spawn_bundle < ' this , T : Bundle > ( & ' this mut self , bundle : T ) -> EntityCommands < ' a , ' this > {
134
134
let mut e = self . spawn ( ) ;
135
135
e. insert_bundle ( bundle) ;
136
136
e
@@ -155,7 +155,7 @@ impl<'a> Commands<'a> {
155
155
/// }
156
156
/// # example_system.system();
157
157
/// ```
158
- pub fn entity < ' b > ( & ' b mut self , entity : Entity ) -> EntityCommands < ' a , ' b > {
158
+ pub fn entity < ' this > ( & ' this mut self , entity : Entity ) -> EntityCommands < ' a , ' this > {
159
159
EntityCommands {
160
160
entity,
161
161
commands : self ,
@@ -589,7 +589,12 @@ impl<T: Component> FallibleCommand for RemoveResource<T> {
589
589
#[ cfg( test) ]
590
590
#[ allow( clippy:: float_cmp, clippy:: approx_constant) ]
591
591
mod tests {
592
+ use super :: {
593
+ fallible:: FallibleCommand , InsertBundleError , InsertError , RemoveBundleError , RemoveError ,
594
+ } ;
595
+ use crate as bevy_ecs;
592
596
use crate :: {
597
+ bundle:: Bundle ,
593
598
component:: { ComponentDescriptor , StorageType } ,
594
599
entity:: Entity ,
595
600
system:: { CommandQueue , Commands } ,
@@ -721,8 +726,71 @@ mod tests {
721
726
assert ! ( world. contains_resource:: <f64 >( ) ) ;
722
727
}
723
728
729
+ struct FailingCommand ;
730
+ impl FallibleCommand for FailingCommand {
731
+ type Error = ( ) ;
732
+
733
+ fn try_write ( self , _: & mut World ) -> Result < ( ) , Self :: Error > {
734
+ Err ( ( ) )
735
+ }
736
+ }
737
+
738
+ struct SuccessfulCommand ;
739
+ impl FallibleCommand for SuccessfulCommand {
740
+ type Error = ( ) ;
741
+
742
+ fn try_write ( self , _: & mut World ) -> Result < ( ) , Self :: Error > {
743
+ Ok ( ( ) )
744
+ }
745
+ }
746
+
747
+ #[ test]
748
+ fn test_commands_error_handler ( ) {
749
+ let invoked = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
750
+ let mut world = World :: default ( ) ;
751
+ let mut queue = CommandQueue :: default ( ) ;
752
+ {
753
+ let mut commands = Commands :: new ( & mut queue, & world) ;
754
+
755
+ commands. insert_resource ( 42u32 ) ;
756
+ let invoked_clone = invoked. clone ( ) ;
757
+ // should succeed
758
+ commands. remove_resource :: < u32 > ( ) . on_failure ( move |_| {
759
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
760
+ } ) ;
761
+
762
+ let invoked_clone = invoked. clone ( ) ;
763
+ // should fail
764
+ commands. remove_resource :: < u32 > ( ) . on_failure ( move |_| {
765
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
766
+ } ) ;
767
+
768
+ let invoked_clone = invoked. clone ( ) ;
769
+ // should fail
770
+ commands. add_fallible ( FailingCommand ) . on_failure ( move |_| {
771
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
772
+ } ) ;
773
+
774
+ let invoked_clone = invoked. clone ( ) ;
775
+ // should succeed
776
+ commands
777
+ . add_fallible ( SuccessfulCommand )
778
+ . on_failure ( move |_| {
779
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
780
+ } ) ;
781
+ }
782
+ queue. apply ( & mut world) ;
783
+
784
+ assert_eq ! ( invoked. load( Ordering :: Relaxed ) , 2 ) ;
785
+ }
786
+
724
787
#[ test]
725
- fn test_error_handler ( ) {
788
+ fn test_entity_commands_error_handler ( ) {
789
+ #[ derive( Bundle ) ]
790
+ struct TestBundle {
791
+ value : u32 ,
792
+ }
793
+
726
794
let invoked = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
727
795
728
796
let mut world = World :: default ( ) ;
@@ -734,19 +802,67 @@ mod tests {
734
802
{
735
803
let mut commands = Commands :: new ( & mut queue, & world) ;
736
804
805
+ // EntityCommands::despawn
737
806
let mut try_despawn = |e| {
738
807
let invoked_clone = invoked. clone ( ) ;
739
- commands. entity ( e) . despawn ( ) . on_failure ( move |_| {
808
+ commands. entity ( e) . despawn ( ) . on_failure ( move |error| {
809
+ assert_eq ! ( error. error. entity, e) ;
740
810
invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
741
811
} ) ;
742
812
} ;
743
813
744
814
try_despawn ( invalid_entity) ;
745
815
try_despawn ( valid_entity) ;
816
+
817
+ // EntityCommands::insert
818
+ let invoked_clone = invoked. clone ( ) ;
819
+ commands
820
+ . entity ( invalid_entity)
821
+ . insert ( 42 )
822
+ . on_failure ( move |error| {
823
+ let InsertError { entity, component } = error. error ;
824
+ assert_eq ! ( entity, invalid_entity) ;
825
+ assert_eq ! ( component, 42 ) ;
826
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
827
+ } ) ;
828
+
829
+ // EntityCommands::insert_bundle
830
+ let invoked_clone = invoked. clone ( ) ;
831
+ commands
832
+ . entity ( invalid_entity)
833
+ . insert_bundle ( TestBundle { value : 42 } )
834
+ . on_failure ( move |error| {
835
+ let InsertBundleError { entity, bundle } = error. error ;
836
+ assert_eq ! ( entity, invalid_entity) ;
837
+ assert_eq ! ( bundle. value, 42 ) ;
838
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
839
+ } ) ;
840
+
841
+ // EntityCommands::remove
842
+ let invoked_clone = invoked. clone ( ) ;
843
+ commands
844
+ . entity ( invalid_entity)
845
+ . remove :: < u32 > ( )
846
+ . on_failure ( move |error| {
847
+ let RemoveError { entity, .. } = error. error ;
848
+ assert_eq ! ( entity, invalid_entity) ;
849
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
850
+ } ) ;
851
+
852
+ // EntityCommands::remove_resource
853
+ let invoked_clone = invoked. clone ( ) ;
854
+ commands
855
+ . entity ( invalid_entity)
856
+ . remove_bundle :: < TestBundle > ( )
857
+ . on_failure ( move |error| {
858
+ let RemoveBundleError { entity, .. } = error. error ;
859
+ assert_eq ! ( entity, invalid_entity) ;
860
+ invoked_clone. fetch_add ( 1 , Ordering :: Relaxed ) ;
861
+ } ) ;
746
862
}
747
863
queue. apply ( & mut world) ;
748
864
749
- assert_eq ! ( invoked. load( Ordering :: Relaxed ) , 1 ) ;
865
+ assert_eq ! ( invoked. load( Ordering :: Relaxed ) , 5 ) ;
750
866
}
751
867
752
868
#[ test]
0 commit comments