Move commands module into bevy::ecs::world (#12234)

# Objective

Fixes https://github.com/bevyengine/bevy/issues/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.
This commit is contained in:
targrub 2024-03-02 18:13:45 -05:00 committed by GitHub
parent 04ec10552c
commit 13cbb9cf10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 81 additions and 80 deletions

View file

@ -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};

View file

@ -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;

View file

@ -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},

View file

@ -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;

View file

@ -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::*;

View file

@ -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.

View file

@ -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};

View file

@ -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.

View file

@ -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;

View file

@ -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;

View file

@ -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};

View file

@ -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};

View file

@ -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};

View file

@ -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;

View file

@ -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},
};