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:
parent
04ec10552c
commit
13cbb9cf10
@ -1,8 +1,8 @@
|
|||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
component::Component,
|
component::Component,
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
system::{Command, CommandQueue, Commands},
|
system::Commands,
|
||||||
world::World,
|
world::{Command, CommandQueue, World},
|
||||||
};
|
};
|
||||||
use criterion::{black_box, Criterion};
|
use criterion::{black_box, Criterion};
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use crate::prelude::Mut;
|
use crate::prelude::Mut;
|
||||||
use crate::reflect::AppTypeRegistry;
|
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 crate::{entity::Entity, reflect::ReflectComponent, world::World};
|
||||||
use bevy_reflect::{Reflect, TypeRegistry};
|
use bevy_reflect::{Reflect, TypeRegistry};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
@ -1,57 +1,18 @@
|
|||||||
mod command_queue;
|
|
||||||
mod parallel_scope;
|
mod parallel_scope;
|
||||||
|
|
||||||
|
use super::{Deferred, Resource};
|
||||||
use crate::{
|
use crate::{
|
||||||
self as bevy_ecs,
|
self as bevy_ecs,
|
||||||
bundle::Bundle,
|
bundle::Bundle,
|
||||||
entity::{Entities, Entity},
|
entity::{Entities, Entity},
|
||||||
system::{RunSystemWithInput, SystemId},
|
system::{RunSystemWithInput, SystemId},
|
||||||
world::{EntityWorldMut, FromWorld, World},
|
world::{Command, CommandQueue, EntityWorldMut, FromWorld, World},
|
||||||
};
|
};
|
||||||
use bevy_ecs_macros::SystemParam;
|
use bevy_ecs_macros::SystemParam;
|
||||||
use bevy_utils::tracing::{error, info};
|
use bevy_utils::tracing::{error, info};
|
||||||
pub use command_queue::CommandQueue;
|
|
||||||
pub use parallel_scope::*;
|
pub use parallel_scope::*;
|
||||||
use std::marker::PhantomData;
|
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`].
|
/// A [`Command`] queue to perform structural changes to the [`World`].
|
||||||
///
|
///
|
||||||
/// Since each command requires exclusive access to the `World`,
|
/// Since each command requires exclusive access to the `World`,
|
||||||
@ -115,15 +76,6 @@ pub struct Commands<'w, 's> {
|
|||||||
entities: &'w Entities,
|
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> {
|
impl<'w, 's> Commands<'w, 's> {
|
||||||
/// Returns a new `Commands` instance from a [`CommandQueue`] and a [`World`].
|
/// Returns a new `Commands` instance from a [`CommandQueue`] and a [`World`].
|
||||||
///
|
///
|
||||||
@ -581,7 +533,7 @@ impl<'w, 's> Commands<'w, 's> {
|
|||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use bevy_ecs::{system::Command, prelude::*};
|
/// # use bevy_ecs::{world::Command, prelude::*};
|
||||||
/// #[derive(Resource, Default)]
|
/// #[derive(Resource, Default)]
|
||||||
/// struct Counter(u64);
|
/// struct Counter(u64);
|
||||||
///
|
///
|
||||||
@ -1138,8 +1090,8 @@ mod tests {
|
|||||||
use crate::{
|
use crate::{
|
||||||
self as bevy_ecs,
|
self as bevy_ecs,
|
||||||
component::Component,
|
component::Component,
|
||||||
system::{CommandQueue, Commands, Resource},
|
system::{Commands, Resource},
|
||||||
world::World,
|
world::{CommandQueue, World},
|
||||||
};
|
};
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
atomic::{AtomicUsize, Ordering},
|
atomic::{AtomicUsize, Ordering},
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::entity::Entity;
|
use crate::entity::Entity;
|
||||||
use crate::system::{BoxedSystem, Command, IntoSystem};
|
use crate::system::{BoxedSystem, IntoSystem};
|
||||||
use crate::world::World;
|
use crate::world::{Command, World};
|
||||||
use crate::{self as bevy_ecs};
|
use crate::{self as bevy_ecs};
|
||||||
use bevy_ecs_macros::Component;
|
use bevy_ecs_macros::Component;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
|
use crate::system::{SystemBuffer, SystemMeta};
|
||||||
|
|
||||||
use std::{fmt::Debug, mem::MaybeUninit};
|
use std::{fmt::Debug, mem::MaybeUninit};
|
||||||
|
|
||||||
use bevy_ptr::{OwningPtr, Unaligned};
|
use bevy_ptr::{OwningPtr, Unaligned};
|
||||||
use bevy_utils::tracing::warn;
|
use bevy_utils::tracing::warn;
|
||||||
|
|
||||||
use super::Command;
|
use crate::world::{Command, World};
|
||||||
use crate::world::World;
|
|
||||||
|
|
||||||
struct CommandMeta {
|
struct CommandMeta {
|
||||||
/// SAFETY: The `value` must point to a value of type `T: Command`,
|
/// 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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
@ -1,5 +1,6 @@
|
|||||||
//! Defines the [`World`] and APIs for accessing it directly.
|
//! Defines the [`World`] and APIs for accessing it directly.
|
||||||
|
|
||||||
|
mod command_queue;
|
||||||
mod deferred_world;
|
mod deferred_world;
|
||||||
mod entity_ref;
|
mod entity_ref;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
@ -8,6 +9,7 @@ pub mod unsafe_world_cell;
|
|||||||
mod world_cell;
|
mod world_cell;
|
||||||
|
|
||||||
pub use crate::change_detection::{Mut, Ref, CHECK_TICK_THRESHOLD};
|
pub use crate::change_detection::{Mut, Ref, CHECK_TICK_THRESHOLD};
|
||||||
|
pub use crate::world::command_queue::CommandQueue;
|
||||||
pub use deferred_world::DeferredWorld;
|
pub use deferred_world::DeferredWorld;
|
||||||
pub use entity_ref::{
|
pub use entity_ref::{
|
||||||
EntityMut, EntityRef, EntityWorldMut, Entry, FilteredEntityMut, FilteredEntityRef,
|
EntityMut, EntityRef, EntityWorldMut, Entry, FilteredEntityMut, FilteredEntityRef,
|
||||||
@ -30,7 +32,7 @@ use crate::{
|
|||||||
removal_detection::RemovedComponentEvents,
|
removal_detection::RemovedComponentEvents,
|
||||||
schedule::{Schedule, ScheduleLabel, Schedules},
|
schedule::{Schedule, ScheduleLabel, Schedules},
|
||||||
storage::{ResourceData, Storages},
|
storage::{ResourceData, Storages},
|
||||||
system::{CommandQueue, Commands, Res, Resource},
|
system::{Commands, Res, Resource},
|
||||||
world::error::TryRunScheduleError,
|
world::error::TryRunScheduleError,
|
||||||
};
|
};
|
||||||
use bevy_ptr::{OwningPtr, Ptr};
|
use bevy_ptr::{OwningPtr, Ptr};
|
||||||
@ -43,9 +45,44 @@ use std::{
|
|||||||
};
|
};
|
||||||
mod identifier;
|
mod identifier;
|
||||||
|
|
||||||
|
use self::unsafe_world_cell::{UnsafeEntityCell, UnsafeWorldCell};
|
||||||
pub use identifier::WorldId;
|
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,
|
/// Stores and exposes operations on [entities](Entity), [components](Component), resources,
|
||||||
/// and their associated metadata.
|
/// and their associated metadata.
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#![warn(unsafe_op_in_unsafe_fn)]
|
#![warn(unsafe_op_in_unsafe_fn)]
|
||||||
|
|
||||||
use super::{Mut, Ref, World, WorldId};
|
use super::{command_queue::CommandQueue, Mut, Ref, World, WorldId};
|
||||||
use crate::{
|
use crate::{
|
||||||
archetype::{Archetype, ArchetypeComponentId, Archetypes},
|
archetype::{Archetype, ArchetypeComponentId, Archetypes},
|
||||||
bundle::Bundles,
|
bundle::Bundles,
|
||||||
@ -14,7 +14,7 @@ use crate::{
|
|||||||
prelude::Component,
|
prelude::Component,
|
||||||
removal_detection::RemovedComponentEvents,
|
removal_detection::RemovedComponentEvents,
|
||||||
storage::{Column, ComponentSparseSet, Storages},
|
storage::{Column, ComponentSparseSet, Storages},
|
||||||
system::{CommandQueue, Res, Resource},
|
system::{Res, Resource},
|
||||||
};
|
};
|
||||||
use bevy_ptr::Ptr;
|
use bevy_ptr::Ptr;
|
||||||
use std::{any::TypeId, cell::UnsafeCell, fmt::Debug, marker::PhantomData, ptr, ptr::addr_of_mut};
|
use std::{any::TypeId, cell::UnsafeCell, fmt::Debug, marker::PhantomData, ptr, ptr::addr_of_mut};
|
||||||
|
@ -3,8 +3,8 @@ use bevy_ecs::{
|
|||||||
bundle::Bundle,
|
bundle::Bundle,
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
prelude::Events,
|
prelude::Events,
|
||||||
system::{Command, Commands, EntityCommands},
|
system::{Commands, EntityCommands},
|
||||||
world::{EntityWorldMut, World},
|
world::{Command, EntityWorldMut, World},
|
||||||
};
|
};
|
||||||
use bevy_utils::smallvec::{smallvec, SmallVec};
|
use bevy_utils::smallvec::{smallvec, SmallVec};
|
||||||
|
|
||||||
@ -702,8 +702,8 @@ mod tests {
|
|||||||
component::Component,
|
component::Component,
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
event::Events,
|
event::Events,
|
||||||
system::{CommandQueue, Commands},
|
system::Commands,
|
||||||
world::World,
|
world::{CommandQueue, World},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Assert the (non)existence and state of the child's [`Parent`] component.
|
/// Assert the (non)existence and state of the child's [`Parent`] component.
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use crate::components::{Children, Parent};
|
use crate::components::{Children, Parent};
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
system::{Command, EntityCommands},
|
system::EntityCommands,
|
||||||
world::{EntityWorldMut, World},
|
world::{Command, EntityWorldMut, World},
|
||||||
};
|
};
|
||||||
use bevy_utils::tracing::debug;
|
use bevy_utils::tracing::debug;
|
||||||
|
|
||||||
@ -139,8 +139,8 @@ impl<'w> DespawnRecursiveExt for EntityWorldMut<'w> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
component::Component,
|
component::Component,
|
||||||
system::{CommandQueue, Commands},
|
system::Commands,
|
||||||
world::World,
|
world::{CommandQueue, World},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::DespawnRecursiveExt;
|
use super::DespawnRecursiveExt;
|
||||||
|
@ -193,7 +193,7 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use bevy_ecs::entity::EntityHashMap;
|
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 bevy_hierarchy::{Parent, PushChild};
|
||||||
|
|
||||||
use crate::dynamic_scene_builder::DynamicSceneBuilder;
|
use crate::dynamic_scene_builder::DynamicSceneBuilder;
|
||||||
|
@ -5,8 +5,8 @@ use bevy_ecs::{
|
|||||||
entity::Entity,
|
entity::Entity,
|
||||||
event::{Event, Events, ManualEventReader},
|
event::{Event, Events, ManualEventReader},
|
||||||
reflect::AppTypeRegistry,
|
reflect::AppTypeRegistry,
|
||||||
system::{Command, Resource},
|
system::Resource,
|
||||||
world::{Mut, World},
|
world::{Command, Mut, World},
|
||||||
};
|
};
|
||||||
use bevy_hierarchy::{Parent, PushChild};
|
use bevy_hierarchy::{Parent, PushChild};
|
||||||
use bevy_utils::{tracing::error, HashMap, HashSet};
|
use bevy_utils::{tracing::error, HashMap, HashSet};
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Extension to [`EntityCommands`] to modify `bevy_hierarchy` hierarchies
|
//! Extension to [`EntityCommands`] to modify `bevy_hierarchy` hierarchies
|
||||||
//! while preserving [`GlobalTransform`].
|
//! 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 bevy_hierarchy::{PushChild, RemoveParent};
|
||||||
|
|
||||||
use crate::{GlobalTransform, Transform};
|
use crate::{GlobalTransform, Transform};
|
||||||
|
@ -182,7 +182,7 @@ unsafe fn propagate_recursive(
|
|||||||
mod test {
|
mod test {
|
||||||
use bevy_app::prelude::*;
|
use bevy_app::prelude::*;
|
||||||
use bevy_ecs::prelude::*;
|
use bevy_ecs::prelude::*;
|
||||||
use bevy_ecs::system::CommandQueue;
|
use bevy_ecs::world::CommandQueue;
|
||||||
use bevy_math::{vec3, Vec3};
|
use bevy_math::{vec3, Vec3};
|
||||||
use bevy_tasks::{ComputeTaskPool, TaskPool};
|
use bevy_tasks::{ComputeTaskPool, TaskPool};
|
||||||
|
|
||||||
|
@ -126,8 +126,8 @@ mod tests {
|
|||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
component::Component,
|
component::Component,
|
||||||
schedule::Schedule,
|
schedule::Schedule,
|
||||||
system::{CommandQueue, Commands},
|
system::Commands,
|
||||||
world::World,
|
world::{CommandQueue, World},
|
||||||
};
|
};
|
||||||
use bevy_hierarchy::BuildChildren;
|
use bevy_hierarchy::BuildChildren;
|
||||||
|
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
//! to spawn, poll, and complete tasks across systems and system ticks.
|
//! to spawn, poll, and complete tasks across systems and system ticks.
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
ecs::system::{CommandQueue, SystemState},
|
ecs::system::SystemState,
|
||||||
|
ecs::world::CommandQueue,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task},
|
tasks::{block_on, futures_lite::future, AsyncComputeTaskPool, Task},
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user