mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
Add remove resource to commands (#1478)
resolves #1468 Co-authored-by: Niklas Eicker <git@nikl.me>
This commit is contained in:
parent
f73c6d18ef
commit
2e3af84590
2 changed files with 53 additions and 0 deletions
|
@ -345,6 +345,10 @@ impl Resources {
|
|||
resource_data.storage.clear_trackers();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove<T: Resource>(&mut self) {
|
||||
self.resource_data.remove(&TypeId::of::<T>());
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for Resources {}
|
||||
|
@ -534,6 +538,8 @@ mod tests {
|
|||
222
|
||||
);
|
||||
assert_eq!(*resources.get::<i32>().expect("resource exists"), 123);
|
||||
resources.remove::<i32>();
|
||||
assert!(resources.get::<i32>().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -165,6 +165,16 @@ impl<T: Resource> Command for InsertResource<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct RemoveResource<T: Resource> {
|
||||
phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: Resource> Command for RemoveResource<T> {
|
||||
fn write(self: Box<Self>, _world: &mut World, resources: &mut Resources) {
|
||||
resources.remove::<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct InsertLocalResource<T: Resource> {
|
||||
resource: T,
|
||||
|
@ -304,6 +314,12 @@ impl Commands {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn remove_resource<T: Resource>(&mut self) -> &mut Self {
|
||||
self.add_command(RemoveResource::<T> {
|
||||
phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
/// Adds a bundle of components to the current entity.
|
||||
///
|
||||
/// See [`Self::with`], [`Self::current_entity`].
|
||||
|
@ -411,6 +427,7 @@ impl Commands {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{resource::Resources, Commands, World};
|
||||
use core::any::TypeId;
|
||||
|
||||
#[test]
|
||||
fn command_buffer() {
|
||||
|
@ -466,4 +483,34 @@ mod tests {
|
|||
let results_after_u64 = world.query::<&u64>().map(|a| *a).collect::<Vec<_>>();
|
||||
assert_eq!(results_after_u64, vec![]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_resources() {
|
||||
let mut world = World::default();
|
||||
let mut resources = Resources::default();
|
||||
let mut command_buffer = Commands::default();
|
||||
command_buffer.insert_resource(123);
|
||||
command_buffer.insert_resource(456.0);
|
||||
command_buffer.apply(&mut world, &mut resources);
|
||||
assert_eq!(
|
||||
resources.resource_data.contains_key(&TypeId::of::<i32>()),
|
||||
true
|
||||
);
|
||||
assert_eq!(
|
||||
resources.resource_data.contains_key(&TypeId::of::<f64>()),
|
||||
true
|
||||
);
|
||||
|
||||
// test resource removal
|
||||
command_buffer.remove_resource::<i32>();
|
||||
command_buffer.apply(&mut world, &mut resources);
|
||||
assert_eq!(
|
||||
resources.resource_data.contains_key(&TypeId::of::<i32>()),
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
resources.resource_data.contains_key(&TypeId::of::<f64>()),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue