From 5f0abbfd157969d82b632552cd7a36d53b41c2cf Mon Sep 17 00:00:00 2001 From: Robert Walter <26892280+RobWalt@users.noreply.github.com> Date: Wed, 29 Mar 2023 20:27:48 +0200 Subject: [PATCH] Make standard commands more ergonomic (in niche cases) (#8249) # Objective I ran into a case where I need to create a `CommandQueue` and push standard `Command` actions like `Insert` or `Remove` to it manually. I saw that `Remove` looked as follows: ```rust struct Remove { entity: Entity, phantom: PhantomData } ``` so naturally, I tried to use `Remove::::from(entity)` but it didn't exist. We need to specify the `PhantomData` explicitly when creating this command action. The same goes for `RemoveResource` and `InitResource` ## Solution This PR implements the following: - `From` for `Remove` - `Default` for `RemoveResource` and `InitResource` - use these traits in the implementation of methods of `Commands` - rename `phantom` field on the structs above to `_phantom` to have a more uniform field naming scheme for the command actions --- ## Changelog > This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section. - Added: implemented `From` for `Remove` and `Default` for `RemoveResource` and `InitResource` for ergonomics --------- Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/system/commands/mod.rs | 41 ++++++++++++++++------ 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index fb0f1b388f..8a4a81dbc3 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -444,9 +444,7 @@ impl<'w, 's> Commands<'w, 's> { /// # bevy_ecs::system::assert_is_system(initialise_scoreboard); /// ``` pub fn init_resource(&mut self) { - self.queue.push(InitResource:: { - _phantom: PhantomData::::default(), - }); + self.queue.push(InitResource::::new()); } /// Pushes a [`Command`] to the queue for inserting a [`Resource`] in the [`World`] with a specific value. @@ -499,9 +497,7 @@ impl<'w, 's> Commands<'w, 's> { /// # bevy_ecs::system::assert_is_system(system); /// ``` pub fn remove_resource(&mut self) { - self.queue.push(RemoveResource:: { - phantom: PhantomData, - }); + self.queue.push(RemoveResource::::new()); } /// Pushes a generic [`Command`] to the command queue. @@ -748,10 +744,7 @@ impl<'w, 's, 'a> EntityCommands<'w, 's, 'a> { where T: Bundle, { - self.commands.add(Remove:: { - entity: self.entity, - phantom: PhantomData, - }); + self.commands.add(Remove::::new(self.entity)); self } @@ -956,6 +949,16 @@ where } } +impl Remove { + /// Creates a [`Command`] which will remove the specified [`Entity`] when flushed + pub const fn new(entity: Entity) -> Self { + Self { + entity, + phantom: PhantomData::, + } + } +} + pub struct InitResource { _phantom: PhantomData, } @@ -966,6 +969,15 @@ impl Command for InitResource { } } +impl InitResource { + /// Creates a [`Command`] which will insert a default created [`Resource`] into the [`World`] + pub const fn new() -> Self { + Self { + _phantom: PhantomData::, + } + } +} + pub struct InsertResource { pub resource: R, } @@ -986,6 +998,15 @@ impl Command for RemoveResource { } } +impl RemoveResource { + /// Creates a [`Command`] which will remove a [`Resource`] from the [`World`] + pub const fn new() -> Self { + Self { + phantom: PhantomData::, + } + } +} + /// [`Command`] to log the components of a given entity. See [`EntityCommands::log_components`]. pub struct LogComponents { entity: Entity,