diff --git a/benches/benches/bevy_ecs/world/commands.rs b/benches/benches/bevy_ecs/world/commands.rs index 70cf1351ac..2b3d84195a 100644 --- a/benches/benches/bevy_ecs/world/commands.rs +++ b/benches/benches/bevy_ecs/world/commands.rs @@ -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}; diff --git a/crates/bevy_ecs/src/reflect/entity_commands.rs b/crates/bevy_ecs/src/reflect/entity_commands.rs index 7f69130d80..8545b346bd 100644 --- a/crates/bevy_ecs/src/reflect/entity_commands.rs +++ b/crates/bevy_ecs/src/reflect/entity_commands.rs @@ -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; diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 65fa06ed7c..a48be90868 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -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`, @@ -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`]. /// @@ -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); /// @@ -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}, diff --git a/crates/bevy_ecs/src/system/system_registry.rs b/crates/bevy_ecs/src/system/system_registry.rs index f4722f1511..ec65b0be84 100644 --- a/crates/bevy_ecs/src/system/system_registry.rs +++ b/crates/bevy_ecs/src/system/system_registry.rs @@ -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; diff --git a/crates/bevy_ecs/src/system/commands/command_queue.rs b/crates/bevy_ecs/src/world/command_queue.rs similarity index 97% rename from crates/bevy_ecs/src/system/commands/command_queue.rs rename to crates/bevy_ecs/src/world/command_queue.rs index a9dcd6f965..3103e3486c 100644 --- a/crates/bevy_ecs/src/system/commands/command_queue.rs +++ b/crates/bevy_ecs/src/world/command_queue.rs @@ -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`, @@ -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::*; diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 81691f6a6a..2b9e6e276a 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -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; @@ -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, @@ -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}; @@ -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. diff --git a/crates/bevy_ecs/src/world/unsafe_world_cell.rs b/crates/bevy_ecs/src/world/unsafe_world_cell.rs index b786a3cfb4..977158d87c 100644 --- a/crates/bevy_ecs/src/world/unsafe_world_cell.rs +++ b/crates/bevy_ecs/src/world/unsafe_world_cell.rs @@ -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, @@ -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}; diff --git a/crates/bevy_hierarchy/src/child_builder.rs b/crates/bevy_hierarchy/src/child_builder.rs index d119afe95e..b6ec13fe73 100644 --- a/crates/bevy_hierarchy/src/child_builder.rs +++ b/crates/bevy_hierarchy/src/child_builder.rs @@ -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}; @@ -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. diff --git a/crates/bevy_hierarchy/src/hierarchy.rs b/crates/bevy_hierarchy/src/hierarchy.rs index d707be839c..26f26233a0 100644 --- a/crates/bevy_hierarchy/src/hierarchy.rs +++ b/crates/bevy_hierarchy/src/hierarchy.rs @@ -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; @@ -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; diff --git a/crates/bevy_scene/src/dynamic_scene.rs b/crates/bevy_scene/src/dynamic_scene.rs index 2cf9a190e1..ac2f98ffae 100644 --- a/crates/bevy_scene/src/dynamic_scene.rs +++ b/crates/bevy_scene/src/dynamic_scene.rs @@ -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; diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index d8e0ea63ac..86c0c30d9a 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -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}; diff --git a/crates/bevy_transform/src/commands.rs b/crates/bevy_transform/src/commands.rs index 1a2f0d4c8a..8cbad487ba 100644 --- a/crates/bevy_transform/src/commands.rs +++ b/crates/bevy_transform/src/commands.rs @@ -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}; diff --git a/crates/bevy_transform/src/systems.rs b/crates/bevy_transform/src/systems.rs index 8f6bac916a..bdbce91e94 100644 --- a/crates/bevy_transform/src/systems.rs +++ b/crates/bevy_transform/src/systems.rs @@ -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}; diff --git a/crates/bevy_ui/src/stack.rs b/crates/bevy_ui/src/stack.rs index ec389daa59..6acc994b6f 100644 --- a/crates/bevy_ui/src/stack.rs +++ b/crates/bevy_ui/src/stack.rs @@ -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; diff --git a/examples/async_tasks/async_compute.rs b/examples/async_tasks/async_compute.rs index 0227c2841b..bf4b8a26ec 100644 --- a/examples/async_tasks/async_compute.rs +++ b/examples/async_tasks/async_compute.rs @@ -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}, };