@@ -165,6 +165,16 @@ impl<T: Resource> Command for InsertResource<T> {
165
165
}
166
166
}
167
167
168
+ pub struct RemoveResource < T : Resource > {
169
+ phantom : PhantomData < T > ,
170
+ }
171
+
172
+ impl < T : Resource > Command for RemoveResource < T > {
173
+ fn write ( self : Box < Self > , _world : & mut World , resources : & mut Resources ) {
174
+ resources. remove :: < T > ( ) ;
175
+ }
176
+ }
177
+
168
178
#[ derive( Debug ) ]
169
179
pub ( crate ) struct InsertLocalResource < T : Resource > {
170
180
resource : T ,
@@ -304,6 +314,12 @@ impl Commands {
304
314
} )
305
315
}
306
316
317
+ pub fn remove_resource < T : Resource > ( & mut self ) -> & mut Self {
318
+ self . add_command ( RemoveResource :: < T > {
319
+ phantom : PhantomData ,
320
+ } )
321
+ }
322
+
307
323
/// Adds a bundle of components to the current entity.
308
324
///
309
325
/// See [`Self::with`], [`Self::current_entity`].
@@ -411,6 +427,7 @@ impl Commands {
411
427
#[ cfg( test) ]
412
428
mod tests {
413
429
use crate :: { resource:: Resources , Commands , World } ;
430
+ use core:: any:: TypeId ;
414
431
415
432
#[ test]
416
433
fn command_buffer ( ) {
@@ -466,4 +483,34 @@ mod tests {
466
483
let results_after_u64 = world. query :: < & u64 > ( ) . map ( |a| * a) . collect :: < Vec < _ > > ( ) ;
467
484
assert_eq ! ( results_after_u64, vec![ ] ) ;
468
485
}
486
+
487
+ #[ test]
488
+ fn remove_resources ( ) {
489
+ let mut world = World :: default ( ) ;
490
+ let mut resources = Resources :: default ( ) ;
491
+ let mut command_buffer = Commands :: default ( ) ;
492
+ command_buffer. insert_resource ( 123 ) ;
493
+ command_buffer. insert_resource ( 456.0 ) ;
494
+ command_buffer. apply ( & mut world, & mut resources) ;
495
+ assert_eq ! (
496
+ resources. resource_data. contains_key( & TypeId :: of:: <i32 >( ) ) ,
497
+ true
498
+ ) ;
499
+ assert_eq ! (
500
+ resources. resource_data. contains_key( & TypeId :: of:: <f64 >( ) ) ,
501
+ true
502
+ ) ;
503
+
504
+ // test resource removal
505
+ command_buffer. remove_resource :: < i32 > ( ) ;
506
+ command_buffer. apply ( & mut world, & mut resources) ;
507
+ assert_eq ! (
508
+ resources. resource_data. contains_key( & TypeId :: of:: <i32 >( ) ) ,
509
+ false
510
+ ) ;
511
+ assert_eq ! (
512
+ resources. resource_data. contains_key( & TypeId :: of:: <f64 >( ) ) ,
513
+ true
514
+ ) ;
515
+ }
469
516
}
0 commit comments