Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add remove resource to commands #1478

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/bevy_ecs/src/resource/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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]
Expand Down
47 changes: 47 additions & 0 deletions crates/bevy_ecs/src/system/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`].
Expand Down Expand Up @@ -411,6 +427,7 @@ impl Commands {
#[cfg(test)]
mod tests {
use crate::{resource::Resources, Commands, World};
use core::any::TypeId;

#[test]
fn command_buffer() {
Expand Down Expand Up @@ -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
);
}
}