Skip to content

Commit

Permalink
Rename Add to Queue for methods with deferred semantics (#15234)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #15106

## Solution

- Trivial refactor to rename the method. The duplicate method `push` was
removed as well. This will simpify the API and make the semantics more
clear. `Add` implies that the action happens immediately, whereas in
reality, the command is queued to be run eventually.
- `ChildBuilder::add_command` has similarly been renamed to
`queue_command`.

## Testing

Unit tests should suffice for this simple refactor.

---

## Migration Guide

- `Commands::add` and `Commands::push` have been replaced with
`Commnads::queue`.
- `ChildBuilder::add_command` has been renamed to
`ChildBuilder::queue_command`.
  • Loading branch information
BenjaminBrienen authored Sep 17, 2024
1 parent c2d54f5 commit b45d83e
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 118 deletions.
6 changes: 3 additions & 3 deletions benches/benches/bevy_ecs/world/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ pub fn fake_commands(criterion: &mut Criterion) {
let mut commands = Commands::new(&mut command_queue, &world);
for i in 0..command_count {
if black_box(i % 2 == 0) {
commands.add(FakeCommandA);
commands.queue(FakeCommandA);
} else {
commands.add(FakeCommandB(0));
commands.queue(FakeCommandB(0));
}
}
command_queue.apply(&mut world);
Expand Down Expand Up @@ -190,7 +190,7 @@ pub fn sized_commands_impl<T: Default + Command>(criterion: &mut Criterion) {
bencher.iter(|| {
let mut commands = Commands::new(&mut command_queue, &world);
for _ in 0..command_count {
commands.add(T::default());
commands.queue(T::default());
}
command_queue.apply(&mut world);
});
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/event/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use bevy_ecs::{
/// //
/// // NOTE: the event won't actually be sent until commands get applied during
/// // apply_deferred.
/// commands.add(|w: &mut World| {
/// commands.queue(|w: &mut World| {
/// w.send_event(MyEvent);
/// });
/// }
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ mod tests {
..Default::default()
});

world.commands().add(
world.commands().queue(
// SAFETY: we registered `event_a` above and it matches the type of TriggerA
unsafe { EmitDynamicTrigger::new_with_id(event_a, EventA, ()) },
);
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/observer/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Component for ObserverState {

fn register_component_hooks(hooks: &mut ComponentHooks) {
hooks.on_add(|mut world, entity, _| {
world.commands().add(move |world: &mut World| {
world.commands().queue(move |world: &mut World| {
world.register_observer(entity);
});
});
Expand All @@ -78,7 +78,7 @@ impl Component for ObserverState {
.as_mut()
.descriptor,
);
world.commands().add(move |world: &mut World| {
world.commands().queue(move |world: &mut World| {
world.unregister_observer(entity, descriptor);
});
});
Expand Down Expand Up @@ -398,7 +398,7 @@ fn hook_on_add<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
entity: Entity,
_: ComponentId,
) {
world.commands().add(move |world: &mut World| {
world.commands().queue(move |world: &mut World| {
let event_type = world.init_component::<E>();
let mut components = Vec::new();
B::component_ids(&mut world.components, &mut world.storages, &mut |id| {
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/reflect/entity_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub trait ReflectCommandExt {
///
/// // Or even with BundleA
/// prefab.data = boxed_reflect_bundle_a;
///
///
/// // No matter which component or bundle is in the resource and without knowing the exact type, you can
/// // use the insert_reflect entity command to insert that component/bundle into an entity.
/// commands
Expand Down Expand Up @@ -174,7 +174,7 @@ pub trait ReflectCommandExt {

impl ReflectCommandExt for EntityCommands<'_> {
fn insert_reflect(&mut self, component: Box<dyn PartialReflect>) -> &mut Self {
self.commands.add(InsertReflect {
self.commands.queue(InsertReflect {
entity: self.entity,
component,
});
Expand All @@ -185,7 +185,7 @@ impl ReflectCommandExt for EntityCommands<'_> {
&mut self,
component: Box<dyn PartialReflect>,
) -> &mut Self {
self.commands.add(InsertReflectWithRegistry::<T> {
self.commands.queue(InsertReflectWithRegistry::<T> {
entity: self.entity,
_t: PhantomData,
component,
Expand All @@ -194,7 +194,7 @@ impl ReflectCommandExt for EntityCommands<'_> {
}

fn remove_reflect(&mut self, component_type_path: impl Into<Cow<'static, str>>) -> &mut Self {
self.commands.add(RemoveReflect {
self.commands.queue(RemoveReflect {
entity: self.entity,
component_type_path: component_type_path.into(),
});
Expand All @@ -205,7 +205,7 @@ impl ReflectCommandExt for EntityCommands<'_> {
&mut self,
component_type_name: impl Into<Cow<'static, str>>,
) -> &mut Self {
self.commands.add(RemoveReflectWithRegistry::<T> {
self.commands.queue(RemoveReflectWithRegistry::<T> {
entity: self.entity,
_t: PhantomData,
component_type_name: component_type_name.into(),
Expand Down
Loading

0 comments on commit b45d83e

Please sign in to comment.