From 2f3746ed963f6ec0cdaeb24cddc748036b8a43e4 Mon Sep 17 00:00:00 2001 From: RobWalt Date: Wed, 29 Mar 2023 16:59:41 +0200 Subject: [PATCH 1/5] feat: make standard commands more ergonomic --- crates/bevy_ecs/src/system/commands/mod.rs | 42 +++++++++++++++------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index fb0f1b388fc18..165b09df988d2 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::::default()); } /// 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::::default()); } /// 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::::from(self.entity)); self } @@ -942,7 +935,7 @@ where #[derive(Debug)] pub struct Remove { pub entity: Entity, - pub phantom: PhantomData, + pub _phantom: PhantomData, } impl Command for Remove @@ -956,6 +949,15 @@ where } } +impl From for Remove { + fn from(entity: Entity) -> Self { + Self { + entity, + _phantom: PhantomData::, + } + } +} + pub struct InitResource { _phantom: PhantomData, } @@ -966,6 +968,14 @@ impl Command for InitResource { } } +impl Default for InitResource { + fn default() -> Self { + Self { + _phantom: PhantomData::, + } + } +} + pub struct InsertResource { pub resource: R, } @@ -977,7 +987,7 @@ impl Command for InsertResource { } pub struct RemoveResource { - pub phantom: PhantomData, + pub _phantom: PhantomData, } impl Command for RemoveResource { @@ -986,6 +996,14 @@ impl Command for RemoveResource { } } +impl Default for RemoveResource { + fn default() -> Self { + Self { + _phantom: PhantomData::, + } + } +} + /// [`Command`] to log the components of a given entity. See [`EntityCommands::log_components`]. pub struct LogComponents { entity: Entity, From 79a752000fd1eb75cf36266eabf5713f03091516 Mon Sep 17 00:00:00 2001 From: RobWalt Date: Wed, 29 Mar 2023 17:36:47 +0200 Subject: [PATCH 2/5] review: use constructors instead of trait impls --- crates/bevy_ecs/src/system/commands/mod.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 165b09df988d2..5bdb27fac8f99 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -444,7 +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::::default()); + self.queue.push(InitResource::::new()); } /// Pushes a [`Command`] to the queue for inserting a [`Resource`] in the [`World`] with a specific value. @@ -497,7 +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::::default()); + self.queue.push(RemoveResource::::new()); } /// Pushes a generic [`Command`] to the command queue. @@ -744,7 +744,7 @@ impl<'w, 's, 'a> EntityCommands<'w, 's, 'a> { where T: Bundle, { - self.commands.add(Remove::::from(self.entity)); + self.commands.add(Remove::::new(self.entity)); self } @@ -949,8 +949,8 @@ where } } -impl From for Remove { - fn from(entity: Entity) -> Self { +impl Remove { + fn new(entity: Entity) -> Self { Self { entity, _phantom: PhantomData::, @@ -968,8 +968,8 @@ impl Command for InitResource { } } -impl Default for InitResource { - fn default() -> Self { +impl InitResource { + fn new() -> Self { Self { _phantom: PhantomData::, } @@ -996,8 +996,8 @@ impl Command for RemoveResource { } } -impl Default for RemoveResource { - fn default() -> Self { +impl RemoveResource { + fn new() -> Self { Self { _phantom: PhantomData::, } From feb453b395f11d73c4fb5d86d7c55f650a3e9c4f Mon Sep 17 00:00:00 2001 From: RobWalt Date: Wed, 29 Mar 2023 18:43:43 +0200 Subject: [PATCH 3/5] review: make constructors `pub const` and add docs --- crates/bevy_ecs/src/system/commands/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 5bdb27fac8f99..5ee455d40deae 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -950,7 +950,8 @@ where } impl Remove { - fn new(entity: Entity) -> Self { + /// Creates a [`Command`] which will remove the specified [`Entity`] when flushed + pub const fn new(entity: Entity) -> Self { Self { entity, _phantom: PhantomData::, @@ -969,7 +970,8 @@ impl Command for InitResource { } impl InitResource { - fn new() -> Self { + /// Creates a [`Command`] which will insert a default created [`Resource`] into the [`World`] + pub const fn new() -> Self { Self { _phantom: PhantomData::, } @@ -997,6 +999,7 @@ impl Command for RemoveResource { } impl RemoveResource { + /// Creates a [`Command`] which will remove a [`Resource`] from the [`World`] fn new() -> Self { Self { _phantom: PhantomData::, From d81a0ea45f6f8ea0af4f88e02cd7ee323da75b0e Mon Sep 17 00:00:00 2001 From: Robert Walter <26892280+RobWalt@users.noreply.github.com> Date: Wed, 29 Mar 2023 18:52:56 +0200 Subject: [PATCH 4/5] Update crates/bevy_ecs/src/system/commands/mod.rs Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/system/commands/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 5ee455d40deae..9100694b5c826 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -1000,7 +1000,7 @@ impl Command for RemoveResource { impl RemoveResource { /// Creates a [`Command`] which will remove a [`Resource`] from the [`World`] - fn new() -> Self { + pub const fn new() -> Self { Self { _phantom: PhantomData::, } From 7f18383c129054e0d6f1de3b35aa3e7483884c8e Mon Sep 17 00:00:00 2001 From: RobWalt Date: Wed, 29 Mar 2023 18:58:30 +0200 Subject: [PATCH 5/5] review: revert breaking changes --- crates/bevy_ecs/src/system/commands/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 9100694b5c826..8a4a81dbc37cf 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -935,7 +935,7 @@ where #[derive(Debug)] pub struct Remove { pub entity: Entity, - pub _phantom: PhantomData, + pub phantom: PhantomData, } impl Command for Remove @@ -954,7 +954,7 @@ impl Remove { pub const fn new(entity: Entity) -> Self { Self { entity, - _phantom: PhantomData::, + phantom: PhantomData::, } } } @@ -989,7 +989,7 @@ impl Command for InsertResource { } pub struct RemoveResource { - pub _phantom: PhantomData, + pub phantom: PhantomData, } impl Command for RemoveResource { @@ -1002,7 +1002,7 @@ impl RemoveResource { /// Creates a [`Command`] which will remove a [`Resource`] from the [`World`] pub const fn new() -> Self { Self { - _phantom: PhantomData::, + phantom: PhantomData::, } } }