Skip to content

Commit

Permalink
Revert "Have EntityCommands methods consume self for easier chaining" (
Browse files Browse the repository at this point in the history
…#15523)

As discussed in #15521

- Partial revert of #14897, reverting the change to the methods to
consume `self`
- The `insert_if` method is kept

The migration guide of #14897 should be removed
Closes #15521

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
  • Loading branch information
tim-blackbird and alice-i-cecile authored Oct 2, 2024
1 parent 23b0dd6 commit 461305b
Show file tree
Hide file tree
Showing 22 changed files with 104 additions and 106 deletions.
4 changes: 2 additions & 2 deletions benches/benches/bevy_ecs/world/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub fn spawn_commands(criterion: &mut Criterion) {
bencher.iter(|| {
let mut commands = Commands::new(&mut command_queue, &world);
for i in 0..entity_count {
let entity = commands
.spawn_empty()
let mut entity = commands.spawn_empty();
entity
.insert_if(A, || black_box(i % 2 == 0))
.insert_if(B, || black_box(i % 3 == 0))
.insert_if(C, || black_box(i % 4 == 0));
Expand Down
78 changes: 39 additions & 39 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ impl<'w, 's> Commands<'w, 's> {
/// - [`spawn_batch`](Self::spawn_batch) to spawn entities with a bundle each.
#[track_caller]
pub fn spawn<T: Bundle>(&mut self, bundle: T) -> EntityCommands {
self.spawn_empty().insert(bundle)
let mut entity = self.spawn_empty();
entity.insert(bundle);
entity
}

/// Returns the [`EntityCommands`] for the requested [`Entity`].
Expand Down Expand Up @@ -1043,7 +1045,7 @@ impl EntityCommands<'_> {
/// # bevy_ecs::system::assert_is_system(add_combat_stats_system);
/// ```
#[track_caller]
pub fn insert(self, bundle: impl Bundle) -> Self {
pub fn insert(&mut self, bundle: impl Bundle) -> &mut Self {
self.queue(insert(bundle, InsertMode::Replace))
}

Expand Down Expand Up @@ -1077,7 +1079,7 @@ impl EntityCommands<'_> {
/// # bevy_ecs::system::assert_is_system(add_health_system);
/// ```
#[track_caller]
pub fn insert_if<F>(self, bundle: impl Bundle, condition: F) -> Self
pub fn insert_if<F>(&mut self, bundle: impl Bundle, condition: F) -> &mut Self
where
F: FnOnce() -> bool,
{
Expand All @@ -1102,7 +1104,7 @@ impl EntityCommands<'_> {
/// The command will panic when applied if the associated entity does not exist.
///
/// To avoid a panic in this case, use the command [`Self::try_insert_if_new`] instead.
pub fn insert_if_new(self, bundle: impl Bundle) -> Self {
pub fn insert_if_new(&mut self, bundle: impl Bundle) -> &mut Self {
self.queue(insert(bundle, InsertMode::Keep))
}

Expand All @@ -1120,7 +1122,7 @@ impl EntityCommands<'_> {
///
/// To avoid a panic in this case, use the command [`Self::try_insert_if_new`]
/// instead.
pub fn insert_if_new_and<F>(self, bundle: impl Bundle, condition: F) -> Self
pub fn insert_if_new_and<F>(&mut self, bundle: impl Bundle, condition: F) -> &mut Self
where
F: FnOnce() -> bool,
{
Expand All @@ -1147,10 +1149,10 @@ impl EntityCommands<'_> {
/// - `T` must have the same layout as the one passed during `component_id` creation.
#[track_caller]
pub unsafe fn insert_by_id<T: Send + 'static>(
self,
&mut self,
component_id: ComponentId,
value: T,
) -> Self {
) -> &mut Self {
let caller = Location::caller();
// SAFETY: same invariants as parent call
self.queue(unsafe {insert_by_id(component_id, value, move |entity| {
Expand All @@ -1167,10 +1169,10 @@ impl EntityCommands<'_> {
/// - [`ComponentId`] must be from the same world as `self`.
/// - `T` must have the same layout as the one passed during `component_id` creation.
pub unsafe fn try_insert_by_id<T: Send + 'static>(
self,
&mut self,
component_id: ComponentId,
value: T,
) -> Self {
) -> &mut Self {
// SAFETY: same invariants as parent call
self.queue(unsafe { insert_by_id(component_id, value, |_| {}) })
}
Expand Down Expand Up @@ -1224,7 +1226,7 @@ impl EntityCommands<'_> {
/// # bevy_ecs::system::assert_is_system(add_combat_stats_system);
/// ```
#[track_caller]
pub fn try_insert(self, bundle: impl Bundle) -> Self {
pub fn try_insert(&mut self, bundle: impl Bundle) -> &mut Self {
self.queue(try_insert(bundle, InsertMode::Replace))
}

Expand Down Expand Up @@ -1255,7 +1257,7 @@ impl EntityCommands<'_> {
/// # bevy_ecs::system::assert_is_system(add_health_system);
/// ```
#[track_caller]
pub fn try_insert_if<F>(self, bundle: impl Bundle, condition: F) -> Self
pub fn try_insert_if<F>(&mut self, bundle: impl Bundle, condition: F) -> &mut Self
where
F: FnOnce() -> bool,
{
Expand Down Expand Up @@ -1301,7 +1303,7 @@ impl EntityCommands<'_> {
/// }
/// # bevy_ecs::system::assert_is_system(add_health_system);
/// ```
pub fn try_insert_if_new_and<F>(self, bundle: impl Bundle, condition: F) -> Self
pub fn try_insert_if_new_and<F>(&mut self, bundle: impl Bundle, condition: F) -> &mut Self
where
F: FnOnce() -> bool,
{
Expand All @@ -1321,7 +1323,7 @@ impl EntityCommands<'_> {
/// # Note
///
/// Unlike [`Self::insert_if_new`], this will not panic if the associated entity does not exist.
pub fn try_insert_if_new(self, bundle: impl Bundle) -> Self {
pub fn try_insert_if_new(&mut self, bundle: impl Bundle) -> &mut Self {
self.queue(try_insert(bundle, InsertMode::Keep))
}

Expand Down Expand Up @@ -1360,20 +1362,20 @@ impl EntityCommands<'_> {
/// }
/// # bevy_ecs::system::assert_is_system(remove_combat_stats_system);
/// ```
pub fn remove<T>(self) -> Self
pub fn remove<T>(&mut self) -> &mut Self
where
T: Bundle,
{
self.queue(remove::<T>)
}

/// Removes a component from the entity.
pub fn remove_by_id(self, component_id: ComponentId) -> Self {
pub fn remove_by_id(&mut self, component_id: ComponentId) -> &mut Self {
self.queue(remove_by_id(component_id))
}

/// Removes all components associated with the entity.
pub fn clear(self) -> Self {
pub fn clear(&mut self) -> &mut Self {
self.queue(clear())
}

Expand Down Expand Up @@ -1405,8 +1407,8 @@ impl EntityCommands<'_> {
/// # bevy_ecs::system::assert_is_system(remove_character_system);
/// ```
#[track_caller]
pub fn despawn(self) -> Self {
self.queue(despawn())
pub fn despawn(&mut self) {
self.queue(despawn());
}

/// Pushes an [`EntityCommand`] to the queue, which will get executed for the current [`Entity`].
Expand All @@ -1425,8 +1427,7 @@ impl EntityCommands<'_> {
/// # }
/// # bevy_ecs::system::assert_is_system(my_system);
/// ```
#[allow(clippy::should_implement_trait)]
pub fn queue<M: 'static>(mut self, command: impl EntityCommand<M>) -> Self {
pub fn queue<M: 'static>(&mut self, command: impl EntityCommand<M>) -> &mut Self {
self.commands.queue(command.with_entity(self.entity));
self
}
Expand Down Expand Up @@ -1468,7 +1469,7 @@ impl EntityCommands<'_> {
/// }
/// # bevy_ecs::system::assert_is_system(remove_combat_stats_system);
/// ```
pub fn retain<T>(self) -> Self
pub fn retain<T>(&mut self) -> &mut Self
where
T: Bundle,
{
Expand All @@ -1480,7 +1481,7 @@ impl EntityCommands<'_> {
/// # Panics
///
/// The command will panic when applied if the associated entity does not exist.
pub fn log_components(self) -> Self {
pub fn log_components(&mut self) -> &mut Self {
self.queue(log_components)
}

Expand All @@ -1493,13 +1494,16 @@ impl EntityCommands<'_> {
/// watches this entity.
///
/// [`Trigger`]: crate::observer::Trigger
pub fn trigger(mut self, event: impl Event) -> Self {
pub fn trigger(&mut self, event: impl Event) -> &mut Self {
self.commands.trigger_targets(event, self.entity);
self
}

/// Creates an [`Observer`] listening for a trigger of type `T` that targets this entity.
pub fn observe<E: Event, B: Bundle, M>(self, system: impl IntoObserverSystem<E, B, M>) -> Self {
pub fn observe<E: Event, B: Bundle, M>(
&mut self,
system: impl IntoObserverSystem<E, B, M>,
) -> &mut Self {
self.queue(observe(system))
}
}
Expand All @@ -1512,9 +1516,8 @@ pub struct EntityEntryCommands<'a, T> {

impl<'a, T: Component> EntityEntryCommands<'a, T> {
/// Modify the component `T` if it exists, using the the function `modify`.
pub fn and_modify(mut self, modify: impl FnOnce(Mut<T>) + Send + Sync + 'static) -> Self {
self.entity_commands = self
.entity_commands
pub fn and_modify(&mut self, modify: impl FnOnce(Mut<T>) + Send + Sync + 'static) -> &mut Self {
self.entity_commands
.queue(move |mut entity: EntityWorldMut| {
if let Some(value) = entity.get_mut() {
modify(value);
Expand All @@ -1532,9 +1535,8 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
/// Panics if the entity does not exist.
/// See [`or_try_insert`](Self::or_try_insert) for a non-panicking version.
#[track_caller]
pub fn or_insert(mut self, default: T) -> Self {
self.entity_commands = self
.entity_commands
pub fn or_insert(&mut self, default: T) -> &mut Self {
self.entity_commands
.queue(insert(default, InsertMode::Keep));
self
}
Expand All @@ -1545,9 +1547,8 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
///
/// See also [`or_insert_with`](Self::or_insert_with).
#[track_caller]
pub fn or_try_insert(mut self, default: T) -> Self {
self.entity_commands = self
.entity_commands
pub fn or_try_insert(&mut self, default: T) -> &mut Self {
self.entity_commands
.queue(try_insert(default, InsertMode::Keep));
self
}
Expand All @@ -1561,7 +1562,7 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
/// Panics if the entity does not exist.
/// See [`or_try_insert_with`](Self::or_try_insert_with) for a non-panicking version.
#[track_caller]
pub fn or_insert_with(self, default: impl Fn() -> T) -> Self {
pub fn or_insert_with(&mut self, default: impl Fn() -> T) -> &mut Self {
self.or_insert(default())
}

Expand All @@ -1571,7 +1572,7 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
///
/// See also [`or_insert`](Self::or_insert) and [`or_try_insert`](Self::or_try_insert).
#[track_caller]
pub fn or_try_insert_with(self, default: impl Fn() -> T) -> Self {
pub fn or_try_insert_with(&mut self, default: impl Fn() -> T) -> &mut Self {
self.or_try_insert(default())
}

Expand All @@ -1583,7 +1584,7 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
///
/// Panics if the entity does not exist.
#[track_caller]
pub fn or_default(self) -> Self
pub fn or_default(&mut self) -> &mut Self
where
T: Default,
{
Expand All @@ -1600,12 +1601,11 @@ impl<'a, T: Component> EntityEntryCommands<'a, T> {
///
/// Panics if the entity does not exist.
#[track_caller]
pub fn or_from_world(mut self) -> Self
pub fn or_from_world(&mut self) -> &mut Self
where
T: FromWorld,
{
self.entity_commands = self
.entity_commands
self.entity_commands
.queue(insert_from_world::<T>(InsertMode::Keep));
self
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_input/src/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,15 +1345,15 @@ pub fn gamepad_connection_system(
let id = connection_event.gamepad;
match &connection_event.connection {
GamepadConnection::Connected(info) => {
let Some(gamepad) = commands.get_entity(id) else {
let Some(mut gamepad) = commands.get_entity(id) else {
warn!("Gamepad {:} removed before handling connection event.", id);
continue;
};
gamepad.insert(Gamepad::new(info.clone()));
info!("Gamepad {:?} connected.", id);
}
GamepadConnection::Disconnected => {
let Some(gamepad) = commands.get_entity(id) else {
let Some(mut gamepad) = commands.get_entity(id) else {
warn!("Gamepad {:} removed before handling disconnection event. You can ignore this if you manually removed it.", id);
continue;
};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ pub fn extract_camera_previous_view_data(
for (entity, camera, maybe_previous_view_data) in cameras_3d.iter() {
if camera.is_active {
let entity = entity.id();
let entity = commands.get_or_spawn(entity);
let mut entity = commands.get_or_spawn(entity);

if let Some(previous_view_data) = maybe_previous_view_data {
entity.insert(previous_view_data.clone());
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ pub(crate) fn add_light_view_entities(
trigger: Trigger<OnAdd, (ExtractedDirectionalLight, ExtractedPointLight)>,
mut commands: Commands,
) {
commands
.get_entity(trigger.entity())
.map(|v| v.insert(LightViewEntities::default()));
if let Some(mut v) = commands.get_entity(trigger.entity()) {
v.insert(LightViewEntities::default());
}
}

pub(crate) fn remove_light_view_entities(
Expand All @@ -447,7 +447,7 @@ pub(crate) fn remove_light_view_entities(
) {
if let Ok(entities) = query.get(trigger.entity()) {
for e in entities.0.iter().copied() {
if let Some(v) = commands.get_entity(e) {
if let Some(mut v) = commands.get_entity(e) {
v.despawn();
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn apply_wireframe_material(
global_material: Res<GlobalWireframeMaterial>,
) {
for e in removed_wireframes.read().chain(no_wireframes.iter()) {
if let Some(commands) = commands.get_entity(e) {
if let Some(mut commands) = commands.get_entity(e) {
commands.remove::<MeshMaterial3d<WireframeMaterial>>();
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_picking/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub fn update_interactions(
for (hovered_entity, new_interaction) in new_interaction_state.drain() {
if let Ok(mut interaction) = interact.get_mut(hovered_entity) {
*interaction = new_interaction;
} else if let Some(entity_commands) = commands.get_entity(hovered_entity) {
} else if let Some(mut entity_commands) = commands.get_entity(hovered_entity) {
entity_commands.try_insert(new_interaction);
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ pub fn extract_cameras(
}

let mut commands = commands.entity(render_entity.id());
commands = commands.insert((
commands.insert((
ExtractedCamera {
target: camera.target.normalize(primary_window),
viewport: camera.viewport.clone(),
Expand Down Expand Up @@ -1087,15 +1087,15 @@ pub fn extract_cameras(
));

if let Some(temporal_jitter) = temporal_jitter {
commands = commands.insert(temporal_jitter.clone());
commands.insert(temporal_jitter.clone());
}

if let Some(render_layers) = render_layers {
commands = commands.insert(render_layers.clone());
commands.insert(render_layers.clone());
}

if let Some(perspective) = projection {
commands = commands.insert(perspective.clone());
commands.insert(perspective.clone());
}
if gpu_culling {
if *gpu_preprocessing_support == GpuPreprocessingSupport::Culling {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/mesh2d/wireframe2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn apply_wireframe_material(
global_material: Res<GlobalWireframe2dMaterial>,
) {
for e in removed_wireframes.read().chain(no_wireframes.iter()) {
if let Some(commands) = commands.get_entity(e) {
if let Some(mut commands) = commands.get_entity(e) {
commands.remove::<MeshMaterial2d<Wireframe2dMaterial>>();
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,8 @@ pub fn extract_default_ui_camera_view(
TemporaryRenderEntity,
))
.id();
let entity_commands = commands
.get_or_spawn(entity)
.insert(DefaultCameraView(default_camera_view));
let mut entity_commands = commands.get_or_spawn(entity);
entity_commands.insert(DefaultCameraView(default_camera_view));
if let Some(ui_anti_alias) = ui_anti_alias {
entity_commands.insert(*ui_anti_alias);
}
Expand Down
Loading

0 comments on commit 461305b

Please sign in to comment.