Skip to content

Commit

Permalink
Move commands module into bevy::ecs::world (bevyengine#12234)
Browse files Browse the repository at this point in the history
# Objective

Fixes bevyengine#11628

## Migration Guide

`Command` and `CommandQueue` have migrated from `bevy_ecs::system` to
`bevy_ecs::world`, so `use bevy_ecs::world::{Command, CommandQueue};`
when necessary.
  • Loading branch information
targrub authored Mar 2, 2024
1 parent 04ec105 commit 13cbb9c
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 80 deletions.
4 changes: 2 additions & 2 deletions benches/benches/bevy_ecs/world/commands.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy_ecs::{
component::Component,
entity::Entity,
system::{Command, CommandQueue, Commands},
world::World,
system::Commands,
world::{Command, CommandQueue, World},
};
use criterion::{black_box, Criterion};

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/reflect/entity_commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::Mut;
use crate::reflect::AppTypeRegistry;
use crate::system::{Command, EntityCommands, Resource};
use crate::system::{EntityCommands, Resource};
use crate::world::Command;
use crate::{entity::Entity, reflect::ReflectComponent, world::World};
use bevy_reflect::{Reflect, TypeRegistry};
use std::borrow::Cow;
Expand Down
58 changes: 5 additions & 53 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,18 @@
mod command_queue;
mod parallel_scope;

use super::{Deferred, Resource};
use crate::{
self as bevy_ecs,
bundle::Bundle,
entity::{Entities, Entity},
system::{RunSystemWithInput, SystemId},
world::{EntityWorldMut, FromWorld, World},
world::{Command, CommandQueue, EntityWorldMut, FromWorld, World},
};
use bevy_ecs_macros::SystemParam;
use bevy_utils::tracing::{error, info};
pub use command_queue::CommandQueue;
pub use parallel_scope::*;
use std::marker::PhantomData;

use super::{Deferred, Resource, SystemBuffer, SystemMeta};

/// A [`World`] mutation.
///
/// Should be used with [`Commands::add`].
///
/// # Usage
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::system::Command;
/// // Our world resource
/// #[derive(Resource, Default)]
/// struct Counter(u64);
///
/// // Our custom command
/// struct AddToCounter(u64);
///
/// impl Command for AddToCounter {
/// fn apply(self, world: &mut World) {
/// let mut counter = world.get_resource_or_insert_with(Counter::default);
/// counter.0 += self.0;
/// }
/// }
///
/// fn some_system(mut commands: Commands) {
/// commands.add(AddToCounter(42));
/// }
/// ```
pub trait Command: Send + 'static {
/// Applies this command, causing it to mutate the provided `world`.
///
/// This method is used to define what a command "does" when it is ultimately applied.
/// Because this method takes `self`, you can store data or settings on the type that implements this trait.
/// This data is set by the system or other source of the command, and then ultimately read in this method.
fn apply(self, world: &mut World);
}

/// A [`Command`] queue to perform structural changes to the [`World`].
///
/// Since each command requires exclusive access to the `World`,
Expand Down Expand Up @@ -115,15 +76,6 @@ pub struct Commands<'w, 's> {
entities: &'w Entities,
}

impl SystemBuffer for CommandQueue {
#[inline]
fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) {
#[cfg(feature = "trace")]
let _span_guard = _system_meta.commands_span.enter();
self.apply(world);
}
}

impl<'w, 's> Commands<'w, 's> {
/// Returns a new `Commands` instance from a [`CommandQueue`] and a [`World`].
///
Expand Down Expand Up @@ -581,7 +533,7 @@ impl<'w, 's> Commands<'w, 's> {
/// # Example
///
/// ```
/// # use bevy_ecs::{system::Command, prelude::*};
/// # use bevy_ecs::{world::Command, prelude::*};
/// #[derive(Resource, Default)]
/// struct Counter(u64);
///
Expand Down Expand Up @@ -1138,8 +1090,8 @@ mod tests {
use crate::{
self as bevy_ecs,
component::Component,
system::{CommandQueue, Commands, Resource},
world::World,
system::{Commands, Resource},
world::{CommandQueue, World},
};
use std::sync::{
atomic::{AtomicUsize, Ordering},
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/system/system_registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::entity::Entity;
use crate::system::{BoxedSystem, Command, IntoSystem};
use crate::world::World;
use crate::system::{BoxedSystem, IntoSystem};
use crate::world::{Command, World};
use crate::{self as bevy_ecs};
use bevy_ecs_macros::Component;
use thiserror::Error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::system::{SystemBuffer, SystemMeta};

use std::{fmt::Debug, mem::MaybeUninit};

use bevy_ptr::{OwningPtr, Unaligned};
use bevy_utils::tracing::warn;

use super::Command;
use crate::world::World;
use crate::world::{Command, World};

struct CommandMeta {
/// SAFETY: The `value` must point to a value of type `T: Command`,
Expand Down Expand Up @@ -234,6 +235,15 @@ impl Drop for CommandQueue {
}
}

impl SystemBuffer for CommandQueue {
#[inline]
fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) {
#[cfg(feature = "trace")]
let _span_guard = _system_meta.commands_span.enter();
self.apply(world);
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
41 changes: 39 additions & 2 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Defines the [`World`] and APIs for accessing it directly.
mod command_queue;
mod deferred_world;
mod entity_ref;
pub mod error;
Expand All @@ -8,6 +9,7 @@ pub mod unsafe_world_cell;
mod world_cell;

pub use crate::change_detection::{Mut, Ref, CHECK_TICK_THRESHOLD};
pub use crate::world::command_queue::CommandQueue;
pub use deferred_world::DeferredWorld;
pub use entity_ref::{
EntityMut, EntityRef, EntityWorldMut, Entry, FilteredEntityMut, FilteredEntityRef,
Expand All @@ -30,7 +32,7 @@ use crate::{
removal_detection::RemovedComponentEvents,
schedule::{Schedule, ScheduleLabel, Schedules},
storage::{ResourceData, Storages},
system::{CommandQueue, Commands, Res, Resource},
system::{Commands, Res, Resource},
world::error::TryRunScheduleError,
};
use bevy_ptr::{OwningPtr, Ptr};
Expand All @@ -43,9 +45,44 @@ use std::{
};
mod identifier;

use self::unsafe_world_cell::{UnsafeEntityCell, UnsafeWorldCell};
pub use identifier::WorldId;

use self::unsafe_world_cell::{UnsafeEntityCell, UnsafeWorldCell};
/// A [`World`] mutation.
///
/// Should be used with [`Commands::add`].
///
/// # Usage
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::world::Command;
/// // Our world resource
/// #[derive(Resource, Default)]
/// struct Counter(u64);
///
/// // Our custom command
/// struct AddToCounter(u64);
///
/// impl Command for AddToCounter {
/// fn apply(self, world: &mut World) {
/// let mut counter = world.get_resource_or_insert_with(Counter::default);
/// counter.0 += self.0;
/// }
/// }
///
/// fn some_system(mut commands: Commands) {
/// commands.add(AddToCounter(42));
/// }
/// ```
pub trait Command: Send + 'static {
/// Applies this command, causing it to mutate the provided `world`.
///
/// This method is used to define what a command "does" when it is ultimately applied.
/// Because this method takes `self`, you can store data or settings on the type that implements this trait.
/// This data is set by the system or other source of the command, and then ultimately read in this method.
fn apply(self, world: &mut World);
}

/// Stores and exposes operations on [entities](Entity), [components](Component), resources,
/// and their associated metadata.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/world/unsafe_world_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![warn(unsafe_op_in_unsafe_fn)]

use super::{Mut, Ref, World, WorldId};
use super::{command_queue::CommandQueue, Mut, Ref, World, WorldId};
use crate::{
archetype::{Archetype, ArchetypeComponentId, Archetypes},
bundle::Bundles,
Expand All @@ -14,7 +14,7 @@ use crate::{
prelude::Component,
removal_detection::RemovedComponentEvents,
storage::{Column, ComponentSparseSet, Storages},
system::{CommandQueue, Res, Resource},
system::{Res, Resource},
};
use bevy_ptr::Ptr;
use std::{any::TypeId, cell::UnsafeCell, fmt::Debug, marker::PhantomData, ptr, ptr::addr_of_mut};
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_hierarchy/src/child_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use bevy_ecs::{
bundle::Bundle,
entity::Entity,
prelude::Events,
system::{Command, Commands, EntityCommands},
world::{EntityWorldMut, World},
system::{Commands, EntityCommands},
world::{Command, EntityWorldMut, World},
};
use bevy_utils::smallvec::{smallvec, SmallVec};

Expand Down Expand Up @@ -702,8 +702,8 @@ mod tests {
component::Component,
entity::Entity,
event::Events,
system::{CommandQueue, Commands},
world::World,
system::Commands,
world::{CommandQueue, World},
};

/// Assert the (non)existence and state of the child's [`Parent`] component.
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_hierarchy/src/hierarchy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::components::{Children, Parent};
use bevy_ecs::{
entity::Entity,
system::{Command, EntityCommands},
world::{EntityWorldMut, World},
system::EntityCommands,
world::{Command, EntityWorldMut, World},
};
use bevy_utils::tracing::debug;

Expand Down Expand Up @@ -139,8 +139,8 @@ impl<'w> DespawnRecursiveExt for EntityWorldMut<'w> {
mod tests {
use bevy_ecs::{
component::Component,
system::{CommandQueue, Commands},
world::World,
system::Commands,
world::{CommandQueue, World},
};

use super::DespawnRecursiveExt;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_scene/src/dynamic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ where
#[cfg(test)]
mod tests {
use bevy_ecs::entity::EntityHashMap;
use bevy_ecs::{reflect::AppTypeRegistry, system::Command, world::World};
use bevy_ecs::{reflect::AppTypeRegistry, world::Command, world::World};
use bevy_hierarchy::{Parent, PushChild};

use crate::dynamic_scene_builder::DynamicSceneBuilder;
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use bevy_ecs::{
entity::Entity,
event::{Event, Events, ManualEventReader},
reflect::AppTypeRegistry,
system::{Command, Resource},
world::{Mut, World},
system::Resource,
world::{Command, Mut, World},
};
use bevy_hierarchy::{Parent, PushChild};
use bevy_utils::{tracing::error, HashMap, HashSet};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_transform/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Extension to [`EntityCommands`] to modify `bevy_hierarchy` hierarchies
//! while preserving [`GlobalTransform`].
use bevy_ecs::{prelude::Entity, system::Command, system::EntityCommands, world::World};
use bevy_ecs::{prelude::Entity, system::EntityCommands, world::Command, world::World};
use bevy_hierarchy::{PushChild, RemoveParent};

use crate::{GlobalTransform, Transform};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_transform/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ unsafe fn propagate_recursive(
mod test {
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_ecs::system::CommandQueue;
use bevy_ecs::world::CommandQueue;
use bevy_math::{vec3, Vec3};
use bevy_tasks::{ComputeTaskPool, TaskPool};

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ mod tests {
use bevy_ecs::{
component::Component,
schedule::Schedule,
system::{CommandQueue, Commands},
world::World,
system::Commands,
world::{CommandQueue, World},
};
use bevy_hierarchy::BuildChildren;

Expand Down
3 changes: 2 additions & 1 deletion examples/async_tasks/async_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//! to spawn, poll, and complete tasks across systems and system ticks.
use bevy::{
ecs::system::{CommandQueue, SystemState},
ecs::system::SystemState,
ecs::world::CommandQueue,
prelude::*,
tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task},
};
Expand Down

0 comments on commit 13cbb9c

Please sign in to comment.