From 1839ff7e2ab4f6fc77b3927e85653f80c1e08864 Mon Sep 17 00:00:00 2001 From: Zachary Harrold Date: Tue, 29 Aug 2023 04:08:53 +1000 Subject: [PATCH] Replaced `EntityCommand` Implementation for `FnOnce` (#9604) # Objective - Fixes #4917 - Replaces #9602 ## Solution - Replaced `EntityCommand` implementation for `FnOnce` to apply to `FnOnce(EntityMut)` instead of `FnOnce(Entity, &mut World)` --- ## Changelog - `FnOnce(Entity, &mut World)` no longer implements `EntityCommand`. This is a breaking change. ## Migration Guide ### 1. New-Type `FnOnce` Create an `EntityCommand` type which implements the method you previously wrote: ```rust pub struct ClassicEntityCommand(pub F); impl EntityCommand for ClassicEntityCommand where F: FnOnce(Entity, &mut World) + Send + 'static, { fn apply(self, id: Entity, world: &mut World) { (self.0)(id, world); } } commands.add(ClassicEntityCommand(|id: Entity, world: &mut World| { /* ... */ })); ``` ### 2. Extract `(Entity, &mut World)` from `EntityMut` The method `into_world_mut` can be used to gain access to the `World` from an `EntityMut`. ```rust let old = |id: Entity, world: &mut World| { /* ... */ }; let new = |mut entity: EntityMut| { let id = entity.id(); let world = entity.into_world_mut(); /* ... */ }; ``` --- crates/bevy_ecs/src/system/commands/mod.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index fac21c7c0f..ed6677ea9f 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -5,7 +5,7 @@ use crate::{ self as bevy_ecs, bundle::Bundle, entity::{Entities, Entity}, - world::{FromWorld, World}, + world::{EntityMut, FromWorld, World}, }; use bevy_ecs_macros::SystemParam; use bevy_utils::tracing::{error, info}; @@ -805,12 +805,13 @@ impl<'w, 's, 'a> EntityCommands<'w, 's, 'a> { /// /// ``` /// # use bevy_ecs::prelude::*; + /// # use bevy_ecs::world::EntityMut; /// # fn my_system(mut commands: Commands) { /// commands /// .spawn_empty() /// // Closures with this signature implement `EntityCommand`. - /// .add(|id: Entity, world: &mut World| { - /// println!("Executed an EntityCommand for {id:?}"); + /// .add(|entity: EntityMut| { + /// println!("Executed an EntityCommand for {:?}", entity.id()); /// }); /// # } /// # bevy_ecs::system::assert_is_system(my_system); @@ -848,10 +849,10 @@ where impl EntityCommand for F where - F: FnOnce(Entity, &mut World) + Send + 'static, + F: FnOnce(EntityMut) + Send + 'static, { fn apply(self, id: Entity, world: &mut World) { - self(id, world); + self(world.entity_mut(id)); } }