From 8a354c0268a3379fac6a7e3fd468d63b9e86b911 Mon Sep 17 00:00:00 2001 From: Stefan Seemayer Date: Sat, 23 Oct 2021 16:46:39 +0200 Subject: [PATCH 1/6] impl<'c> IntoIterator for &'c Components This allows iteration over all `Component`s registered in a world by implementing `IntoIterator` for the `Components` type. --- crates/bevy_ecs/src/component.rs | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index a38132fc4c435..677766b7793cc 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -344,6 +344,16 @@ impl Components { } } +impl<'c> IntoIterator for &'c Components { + type Item = &'c ComponentInfo; + + type IntoIter = std::slice::Iter<'c, ComponentInfo>; + + fn into_iter(self) -> Self::IntoIter { + self.components.iter() + } +} + #[derive(Clone, Debug)] pub struct ComponentTicks { pub(crate) added: u32, @@ -408,3 +418,37 @@ fn check_tick(last_change_tick: &mut u32, change_tick: u32) { *last_change_tick = change_tick.wrapping_sub(MAX_DELTA); } } + +#[cfg(test)] +mod tests { + use crate::{ + self as bevy_ecs, + component::{Component, ComponentInfo}, + world::World, + }; + + #[derive(Component)] + struct W(T); + + #[test] + fn components_iteration() { + let mut world = World::default(); + world.spawn().insert(W(42u32)).insert(W(12.3f32)); + world.spawn().insert(W(123u32)).insert(W(true)); + + let component_names: Vec<&str> = world + .components() + .into_iter() + .map(|ci: &ComponentInfo| ci.name()) + .collect(); + + assert_eq!( + component_names, + vec![ + "bevy_ecs::component::tests::W", + "bevy_ecs::component::tests::W", + "bevy_ecs::component::tests::W" + ] + ); + } +} From e48cd2679dd051f3029a5346224ec89716379e8a Mon Sep 17 00:00:00 2001 From: Stefan Seemayer Date: Sun, 24 Oct 2021 18:10:18 +0200 Subject: [PATCH 2/6] Rename `Components` to `WorldData` the Components struct was not just storing metadata on components, but also on resources. WorldData is a new umbrella term for components and resources. also rename the following: `ComponentId` -> `DataId` `ComponentInfo` -> `DataInfo` `ComponentDescriptor` -> `DataDescriptor` --- crates/bevy_core/src/time/fixed_timestep.rs | 4 +- crates/bevy_ecs/macros/src/lib.rs | 10 +- crates/bevy_ecs/src/archetype.rs | 41 +++--- crates/bevy_ecs/src/bundle.rs | 36 +++--- crates/bevy_ecs/src/component.rs | 108 ++++++++-------- crates/bevy_ecs/src/lib.rs | 24 ++-- crates/bevy_ecs/src/query/fetch.rs | 22 ++-- crates/bevy_ecs/src/query/filter.rs | 16 +-- crates/bevy_ecs/src/query/state.rs | 4 +- crates/bevy_ecs/src/schedule/run_criteria.rs | 6 +- crates/bevy_ecs/src/schedule/stage.rs | 8 +- .../bevy_ecs/src/schedule/system_container.rs | 8 +- crates/bevy_ecs/src/storage/sparse_set.rs | 12 +- crates/bevy_ecs/src/storage/table.rs | 24 ++-- crates/bevy_ecs/src/system/function_system.rs | 6 +- crates/bevy_ecs/src/system/mod.rs | 13 +- crates/bevy_ecs/src/system/query.rs | 4 +- crates/bevy_ecs/src/system/system.rs | 4 +- crates/bevy_ecs/src/system/system_chaining.rs | 6 +- crates/bevy_ecs/src/system/system_param.rs | 48 +++---- crates/bevy_ecs/src/world/entity_ref.rs | 38 +++--- crates/bevy_ecs/src/world/mod.rs | 117 +++++++++--------- crates/bevy_ecs/src/world/spawn_batch.rs | 4 +- crates/bevy_ecs/src/world/world_cell.rs | 21 ++-- crates/bevy_scene/src/dynamic_scene.rs | 2 +- crates/bevy_scene/src/scene_spawner.rs | 2 +- 26 files changed, 285 insertions(+), 303 deletions(-) diff --git a/crates/bevy_core/src/time/fixed_timestep.rs b/crates/bevy_core/src/time/fixed_timestep.rs index a22c874f92930..11d00f4f8aa8f 100644 --- a/crates/bevy_core/src/time/fixed_timestep.rs +++ b/crates/bevy_core/src/time/fixed_timestep.rs @@ -1,7 +1,7 @@ use crate::Time; use bevy_ecs::{ archetype::{Archetype, ArchetypeComponentId}, - component::ComponentId, + component::DataId, query::Access, schedule::ShouldRun, system::{ConfigurableSystem, IntoSystem, Local, Res, ResMut, System}, @@ -156,7 +156,7 @@ impl System for FixedTimestep { self.internal_system.archetype_component_access() } - fn component_access(&self) -> &Access { + fn component_access(&self) -> &Access { self.internal_system.component_access() } diff --git a/crates/bevy_ecs/macros/src/lib.rs b/crates/bevy_ecs/macros/src/lib.rs index e508c7ac2c6e5..46ecaf08f1f43 100644 --- a/crates/bevy_ecs/macros/src/lib.rs +++ b/crates/bevy_ecs/macros/src/lib.rs @@ -147,12 +147,12 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream { let struct_name = &ast.ident; TokenStream::from(quote! { - /// SAFE: ComponentId is returned in field-definition-order. [from_components] and [get_components] use field-definition-order + /// SAFE: DataId is returned in field-definition-order. [from_components] and [get_components] use field-definition-order unsafe impl #impl_generics #ecs_path::bundle::Bundle for #struct_name#ty_generics #where_clause { fn component_ids( - components: &mut #ecs_path::component::Components, + components: &mut #ecs_path::component::WorldData, storages: &mut #ecs_path::storage::Storages, - ) -> Vec<#ecs_path::component::ComponentId> { + ) -> Vec<#ecs_path::component::DataId> { let mut component_ids = Vec::with_capacity(#field_len); #(#field_component_ids)* component_ids @@ -195,7 +195,7 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream { pub fn #fn_name(&mut self) -> Query<'_, '_, #query, #filter> { // SAFE: systems run without conflicts with other systems. // Conflicting queries in QuerySet are not accessible at the same time - // QuerySets are guaranteed to not conflict with other SystemParams + // QuerySets are guaranteed to not onflict with other SystemParams unsafe { Query::new(self.world, &self.query_states.#index, self.last_change_tick, self.change_tick) } @@ -219,7 +219,7 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream { where #(#query::Fetch: ReadOnlyFetch,)* #(#filter::Fetch: FilterFetch,)* { } - // SAFE: Relevant query ComponentId and ArchetypeComponentId access is applied to SystemMeta. If any QueryState conflicts + // SAFE: Relevant query DataId and ArchetypeComponentId access is applied to SystemMeta. If any QueryState conflicts // with any prior access, a panic will occur. unsafe impl<#(#query: WorldQuery + 'static,)* #(#filter: WorldQuery + 'static,)*> SystemParamState for QuerySetState<(#(QueryState<#query, #filter>,)*)> where #(#filter::Fetch: FilterFetch,)* diff --git a/crates/bevy_ecs/src/archetype.rs b/crates/bevy_ecs/src/archetype.rs index 9d6ba80bd9dae..bfb353a9cd099 100644 --- a/crates/bevy_ecs/src/archetype.rs +++ b/crates/bevy_ecs/src/archetype.rs @@ -3,7 +3,7 @@ use crate::{ bundle::BundleId, - component::{ComponentId, StorageType}, + component::{DataId, StorageType}, entity::{Entity, EntityLocation}, storage::{Column, SparseArray, SparseSet, SparseSetIndex, TableId}, }; @@ -121,18 +121,18 @@ pub struct Archetype { entities: Vec, edges: Edges, table_info: TableInfo, - table_components: Cow<'static, [ComponentId]>, - sparse_set_components: Cow<'static, [ComponentId]>, - pub(crate) unique_components: SparseSet, - pub(crate) components: SparseSet, + table_components: Cow<'static, [DataId]>, + sparse_set_components: Cow<'static, [DataId]>, + pub(crate) unique_components: SparseSet, + pub(crate) components: SparseSet, } impl Archetype { pub fn new( id: ArchetypeId, table_id: TableId, - table_components: Cow<'static, [ComponentId]>, - sparse_set_components: Cow<'static, [ComponentId]>, + table_components: Cow<'static, [DataId]>, + sparse_set_components: Cow<'static, [DataId]>, table_archetype_components: Vec, sparse_set_archetype_components: Vec, ) -> Self { @@ -198,27 +198,27 @@ impl Archetype { } #[inline] - pub fn table_components(&self) -> &[ComponentId] { + pub fn table_components(&self) -> &[DataId] { &self.table_components } #[inline] - pub fn sparse_set_components(&self) -> &[ComponentId] { + pub fn sparse_set_components(&self) -> &[DataId] { &self.sparse_set_components } #[inline] - pub fn unique_components(&self) -> &SparseSet { + pub fn unique_components(&self) -> &SparseSet { &self.unique_components } #[inline] - pub fn unique_components_mut(&mut self) -> &mut SparseSet { + pub fn unique_components_mut(&mut self) -> &mut SparseSet { &mut self.unique_components } #[inline] - pub fn components(&self) -> impl Iterator + '_ { + pub fn components(&self) -> impl Iterator + '_ { self.components.indices() } @@ -286,22 +286,19 @@ impl Archetype { } #[inline] - pub fn contains(&self, component_id: ComponentId) -> bool { + pub fn contains(&self, component_id: DataId) -> bool { self.components.contains(component_id) } #[inline] - pub fn get_storage_type(&self, component_id: ComponentId) -> Option { + pub fn get_storage_type(&self, component_id: DataId) -> Option { self.components .get(component_id) .map(|info| info.storage_type) } #[inline] - pub fn get_archetype_component_id( - &self, - component_id: ComponentId, - ) -> Option { + pub fn get_archetype_component_id(&self, component_id: DataId) -> Option { self.components .get(component_id) .map(|info| info.archetype_component_id) @@ -331,8 +328,8 @@ impl ArchetypeGeneration { #[derive(Hash, PartialEq, Eq)] pub struct ArchetypeIdentity { - table_components: Cow<'static, [ComponentId]>, - sparse_set_components: Cow<'static, [ComponentId]>, + table_components: Cow<'static, [DataId]>, + sparse_set_components: Cow<'static, [DataId]>, } #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] @@ -474,8 +471,8 @@ impl Archetypes { pub(crate) fn get_id_or_insert( &mut self, table_id: TableId, - table_components: Vec, - sparse_set_components: Vec, + table_components: Vec, + sparse_set_components: Vec, ) -> ArchetypeId { let table_components = Cow::from(table_components); let sparse_set_components = Cow::from(sparse_set_components); diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index 463a0483cf5e4..8ddd9a40c735c 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -6,7 +6,7 @@ pub use bevy_ecs_macros::Bundle; use crate::{ archetype::{AddBundle, Archetype, ArchetypeId, Archetypes, ComponentStatus}, - component::{Component, ComponentId, ComponentTicks, Components, StorageType}, + component::{Component, ComponentTicks, DataId, StorageType, WorldData}, entity::{Entities, Entity, EntityLocation}, storage::{SparseSetIndex, SparseSets, Storages, Table}, }; @@ -71,25 +71,25 @@ use std::{any::TypeId, collections::HashMap}; /// /// # Safety /// -/// - [Bundle::component_ids] must return the ComponentId for each component type in the bundle, in the +/// - [Bundle::component_ids] must return the DataId for each component type in the bundle, in the /// _exact_ order that [Bundle::get_components] is called. -/// - [Bundle::from_components] must call `func` exactly once for each [ComponentId] returned by +/// - [Bundle::from_components] must call `func` exactly once for each [DataId] returned by /// [Bundle::component_ids]. pub unsafe trait Bundle: Send + Sync + 'static { - /// Gets this [Bundle]'s component ids, in the order of this bundle's Components - fn component_ids(components: &mut Components, storages: &mut Storages) -> Vec; + /// Gets this [Bundle]'s component ids, in the order of this bundle's WorldData + fn component_ids(components: &mut WorldData, storages: &mut Storages) -> Vec; /// Calls `func`, which should return data for each component in the bundle, in the order of - /// this bundle's Components + /// this bundle's WorldData /// /// # Safety /// Caller must return data for each component in the bundle, in the order of this bundle's - /// Components + /// WorldData unsafe fn from_components(func: impl FnMut() -> *mut u8) -> Self where Self: Sized; - /// Calls `func` on each value, in the order of this bundle's Components. This will + /// Calls `func` on each value, in the order of this bundle's WorldData. This will /// "mem::forget" the bundle fields, so callers are responsible for dropping the fields if /// that is desirable. fn get_components(self, func: impl FnMut(*mut u8)); @@ -100,7 +100,7 @@ macro_rules! tuple_impl { /// SAFE: Component is returned in tuple-order. [Bundle::from_components] and [Bundle::get_components] use tuple-order unsafe impl<$($name: Component),*> Bundle for ($($name,)*) { #[allow(unused_variables)] - fn component_ids(components: &mut Components, storages: &mut Storages) -> Vec { + fn component_ids(components: &mut WorldData, storages: &mut Storages) -> Vec { vec![$(components.init_component::<$name>(storages)),*] } @@ -152,7 +152,7 @@ impl SparseSetIndex for BundleId { pub struct BundleInfo { pub(crate) id: BundleId, - pub(crate) component_ids: Vec, + pub(crate) component_ids: Vec, pub(crate) storage_types: Vec, } @@ -163,7 +163,7 @@ impl BundleInfo { } #[inline] - pub fn components(&self) -> &[ComponentId] { + pub fn components(&self) -> &[DataId] { &self.component_ids } @@ -176,7 +176,7 @@ impl BundleInfo { &'b self, entities: &'a mut Entities, archetypes: &'a mut Archetypes, - components: &mut Components, + components: &mut WorldData, storages: &'a mut Storages, archetype_id: ArchetypeId, change_tick: u32, @@ -236,7 +236,7 @@ impl BundleInfo { &'b self, entities: &'a mut Entities, archetypes: &'a mut Archetypes, - components: &mut Components, + components: &mut WorldData, storages: &'a mut Storages, change_tick: u32, ) -> BundleSpawner<'a, 'b> { @@ -308,7 +308,7 @@ impl BundleInfo { &self, archetypes: &mut Archetypes, storages: &mut Storages, - components: &mut Components, + components: &mut WorldData, archetype_id: ArchetypeId, ) -> ArchetypeId { if let Some(add_bundle) = archetypes[archetype_id].edges().get_add_bundle(self.id) { @@ -591,7 +591,7 @@ impl Bundles { pub(crate) fn init_info<'a, T: Bundle>( &'a mut self, - components: &mut Components, + components: &mut WorldData, storages: &mut Storages, ) -> &'a BundleInfo { let bundle_infos = &mut self.bundle_infos; @@ -612,12 +612,12 @@ impl Bundles { /// # Safety /// -/// `component_id` must be valid [ComponentId]'s +/// `component_id` must be valid [DataId]'s unsafe fn initialize_bundle( bundle_type_name: &'static str, - component_ids: Vec, + component_ids: Vec, id: BundleId, - components: &mut Components, + components: &mut WorldData, ) -> BundleInfo { let mut storage_types = Vec::new(); diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 677766b7793cc..d4929cd9e2974 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -91,14 +91,14 @@ impl Default for StorageType { } #[derive(Debug)] -pub struct ComponentInfo { - id: ComponentId, - descriptor: ComponentDescriptor, +pub struct DataInfo { + id: DataId, + descriptor: DataDescriptor, } -impl ComponentInfo { +impl DataInfo { #[inline] - pub fn id(&self) -> ComponentId { + pub fn id(&self) -> DataId { self.id } @@ -132,18 +132,18 @@ impl ComponentInfo { self.descriptor.is_send_and_sync } - fn new(id: ComponentId, descriptor: ComponentDescriptor) -> Self { - ComponentInfo { id, descriptor } + fn new(id: DataId, descriptor: DataDescriptor) -> Self { + DataInfo { id, descriptor } } } #[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)] -pub struct ComponentId(usize); +pub struct DataId(usize); -impl ComponentId { +impl DataId { #[inline] - pub const fn new(index: usize) -> ComponentId { - ComponentId(index) + pub const fn new(index: usize) -> DataId { + DataId(index) } #[inline] @@ -152,7 +152,7 @@ impl ComponentId { } } -impl SparseSetIndex for ComponentId { +impl SparseSetIndex for DataId { #[inline] fn sparse_set_index(&self) -> usize { self.index() @@ -164,7 +164,7 @@ impl SparseSetIndex for ComponentId { } #[derive(Debug)] -pub struct ComponentDescriptor { +pub struct DataDescriptor { name: String, // SAFETY: This must remain private. It must match the statically known StorageType of the // associated rust component type if one exists. @@ -177,7 +177,7 @@ pub struct ComponentDescriptor { drop: unsafe fn(*mut u8), } -impl ComponentDescriptor { +impl DataDescriptor { // SAFETY: The pointer points to a valid value of type `T` and it is safe to drop this value. unsafe fn drop_ptr(x: *mut u8) { x.cast::().drop_in_place() @@ -233,8 +233,8 @@ impl ComponentDescriptor { } #[derive(Debug, Default)] -pub struct Components { - components: Vec, +pub struct WorldData { + data: Vec, indices: std::collections::HashMap, resource_indices: std::collections::HashMap, } @@ -245,112 +245,112 @@ pub enum ComponentsError { ComponentAlreadyExists { type_id: TypeId, name: String, - existing_id: ComponentId, + existing_id: DataId, }, } -impl Components { +impl WorldData { #[inline] - pub fn init_component(&mut self, storages: &mut Storages) -> ComponentId { + pub fn init_component(&mut self, storages: &mut Storages) -> DataId { let type_id = TypeId::of::(); - let components = &mut self.components; + let components = &mut self.data; let index = self.indices.entry(type_id).or_insert_with(|| { let index = components.len(); - let descriptor = ComponentDescriptor::new::(); - let info = ComponentInfo::new(ComponentId(index), descriptor); + let descriptor = DataDescriptor::new::(); + let info = DataInfo::new(DataId(index), descriptor); if T::Storage::STORAGE_TYPE == StorageType::SparseSet { storages.sparse_sets.get_or_insert(&info); } components.push(info); index }); - ComponentId(*index) + DataId(*index) } #[inline] pub fn len(&self) -> usize { - self.components.len() + self.data.len() } #[inline] pub fn is_empty(&self) -> bool { - self.components.len() == 0 + self.data.len() == 0 } #[inline] - pub fn get_info(&self, id: ComponentId) -> Option<&ComponentInfo> { - self.components.get(id.0) + pub fn get_info(&self, id: DataId) -> Option<&DataInfo> { + self.data.get(id.0) } /// # Safety /// - /// `id` must be a valid [ComponentId] + /// `id` must be a valid [DataId] #[inline] - pub unsafe fn get_info_unchecked(&self, id: ComponentId) -> &ComponentInfo { - debug_assert!(id.index() < self.components.len()); - self.components.get_unchecked(id.0) + pub unsafe fn get_info_unchecked(&self, id: DataId) -> &DataInfo { + debug_assert!(id.index() < self.data.len()); + self.data.get_unchecked(id.0) } #[inline] - pub fn get_id(&self, type_id: TypeId) -> Option { - self.indices.get(&type_id).map(|index| ComponentId(*index)) + pub fn get_id(&self, type_id: TypeId) -> Option { + self.indices.get(&type_id).map(|index| DataId(*index)) } #[inline] - pub fn get_resource_id(&self, type_id: TypeId) -> Option { + pub fn get_resource_id(&self, type_id: TypeId) -> Option { self.resource_indices .get(&type_id) - .map(|index| ComponentId(*index)) + .map(|index| DataId(*index)) } #[inline] - pub fn init_resource(&mut self) -> ComponentId { - // SAFE: The [`ComponentDescriptor`] matches the [`TypeId`] + pub fn init_resource(&mut self) -> DataId { + // SAFE: The [`DataDescriptor`] matches the [`TypeId`] unsafe { self.get_or_insert_resource_with(TypeId::of::(), || { - ComponentDescriptor::new_resource::(StorageType::default()) + DataDescriptor::new_resource::(StorageType::default()) }) } } #[inline] - pub fn init_non_send(&mut self) -> ComponentId { - // SAFE: The [`ComponentDescriptor`] matches the [`TypeId`] + pub fn init_non_send(&mut self) -> DataId { + // SAFE: The [`DataDescriptor`] matches the [`TypeId`] unsafe { self.get_or_insert_resource_with(TypeId::of::(), || { - ComponentDescriptor::new_non_send::(StorageType::default()) + DataDescriptor::new_non_send::(StorageType::default()) }) } } /// # Safety /// - /// The [`ComponentDescriptor`] must match the [`TypeId`] + /// The [`DataDescriptor`] must match the [`TypeId`] #[inline] unsafe fn get_or_insert_resource_with( &mut self, type_id: TypeId, - func: impl FnOnce() -> ComponentDescriptor, - ) -> ComponentId { - let components = &mut self.components; + func: impl FnOnce() -> DataDescriptor, + ) -> DataId { + let components = &mut self.data; let index = self.resource_indices.entry(type_id).or_insert_with(|| { let descriptor = func(); let index = components.len(); - components.push(ComponentInfo::new(ComponentId(index), descriptor)); + components.push(DataInfo::new(DataId(index), descriptor)); index }); - ComponentId(*index) + DataId(*index) } } -impl<'c> IntoIterator for &'c Components { - type Item = &'c ComponentInfo; +impl<'c> IntoIterator for &'c WorldData { + type Item = &'c DataInfo; - type IntoIter = std::slice::Iter<'c, ComponentInfo>; + type IntoIter = std::slice::Iter<'c, DataInfo>; fn into_iter(self) -> Self::IntoIter { - self.components.iter() + self.data.iter() } } @@ -423,7 +423,7 @@ fn check_tick(last_change_tick: &mut u32, change_tick: u32) { mod tests { use crate::{ self as bevy_ecs, - component::{Component, ComponentInfo}, + component::{Component, DataInfo}, world::World, }; @@ -437,9 +437,9 @@ mod tests { world.spawn().insert(W(123u32)).insert(W(true)); let component_names: Vec<&str> = world - .components() + .data() .into_iter() - .map(|ci: &ComponentInfo| ci.name()) + .map(|ci: &DataInfo| ci.name()) .collect(); assert_eq!( diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 6fb347a2a7e8f..005c58ddd81d3 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -43,7 +43,7 @@ mod tests { use crate as bevy_ecs; use crate::{ bundle::Bundle, - component::{Component, ComponentId}, + component::{Component, DataId}, entity::Entity, query::{ Added, ChangeTrackers, Changed, FilterFetch, FilteredAccess, With, Without, WorldQuery, @@ -130,7 +130,7 @@ mod tests { } assert_eq!( - ::component_ids(&mut world.components, &mut world.storages), + ::component_ids(&mut world.data, &mut world.storages), vec![ world.init_component::(), world.init_component::(), @@ -178,7 +178,7 @@ mod tests { } assert_eq!( - ::component_ids(&mut world.components, &mut world.storages), + ::component_ids(&mut world.data, &mut world.storages), vec![ world.init_component::(), world.init_component::(), @@ -996,10 +996,7 @@ mod tests { assert!(!world.is_resource_changed::()); world.insert_resource(123); - let resource_id = world - .components() - .get_resource_id(TypeId::of::()) - .unwrap(); + let resource_id = world.data().get_resource_id(TypeId::of::()).unwrap(); let archetype_component_id = world .archetypes() .resource() @@ -1061,10 +1058,7 @@ mod tests { "other resources are unaffected" ); - let current_resource_id = world - .components() - .get_resource_id(TypeId::of::()) - .unwrap(); + let current_resource_id = world.data().get_resource_id(TypeId::of::()).unwrap(); assert_eq!( resource_id, current_resource_id, "resource id does not change after removing / re-adding" @@ -1286,14 +1280,14 @@ mod tests { let mut world = World::new(); let query = world.query_filtered::<&mut A, Changed>(); - let mut expected = FilteredAccess::::default(); - let a_id = world.components.get_id(TypeId::of::()).unwrap(); - let b_id = world.components.get_id(TypeId::of::()).unwrap(); + let mut expected = FilteredAccess::::default(); + let a_id = world.data.get_id(TypeId::of::()).unwrap(); + let b_id = world.data.get_id(TypeId::of::()).unwrap(); expected.add_write(a_id); expected.add_read(b_id); assert!( query.component_access.eq(&expected), - "ComponentId access from query fetch and query filter should be combined" + "DataId access from query fetch and query filter should be combined" ); } diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index 9572a797f1f90..4b6e6b46ac9bf 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1,7 +1,7 @@ use crate::{ archetype::{Archetype, ArchetypeComponentId}, change_detection::Ticks, - component::{Component, ComponentId, ComponentStorage, ComponentTicks, StorageType}, + component::{Component, ComponentStorage, ComponentTicks, DataId, StorageType}, entity::Entity, query::{Access, FilteredAccess}, storage::{ComponentSparseSet, Table, Tables}, @@ -118,7 +118,7 @@ pub trait Fetch<'world, 'state>: Sized { /// [`Fetch::table_fetch`]. pub unsafe trait FetchState: Send + Sync + Sized { fn init(world: &mut World) -> Self; - fn update_component_access(&self, access: &mut FilteredAccess); + fn update_component_access(&self, access: &mut FilteredAccess); fn update_archetype_component_access( &self, archetype: &Archetype, @@ -154,7 +154,7 @@ unsafe impl FetchState for EntityState { Self } - fn update_component_access(&self, _access: &mut FilteredAccess) {} + fn update_component_access(&self, _access: &mut FilteredAccess) {} fn update_archetype_component_access( &self, @@ -224,7 +224,7 @@ impl WorldQuery for &T { /// The [`FetchState`] of `&T`. pub struct ReadState { - component_id: ComponentId, + component_id: DataId, marker: PhantomData, } @@ -239,7 +239,7 @@ unsafe impl FetchState for ReadState { } } - fn update_component_access(&self, access: &mut FilteredAccess) { + fn update_component_access(&self, access: &mut FilteredAccess) { if access.access().has_write(self.component_id) { panic!("&{} conflicts with a previous access in this query. Shared access cannot coincide with exclusive access.", std::any::type_name::()); @@ -403,7 +403,7 @@ impl Clone for WriteFetch { /// The [`FetchState`] of `&mut T`. pub struct WriteState { - component_id: ComponentId, + component_id: DataId, marker: PhantomData, } @@ -418,7 +418,7 @@ unsafe impl FetchState for WriteState { } } - fn update_component_access(&self, access: &mut FilteredAccess) { + fn update_component_access(&self, access: &mut FilteredAccess) { if access.access().has_read(self.component_id) { panic!("&mut {} conflicts with a previous access in this query. Mutable component access must be unique.", std::any::type_name::()); @@ -582,7 +582,7 @@ unsafe impl FetchState for OptionState { } } - fn update_component_access(&self, access: &mut FilteredAccess) { + fn update_component_access(&self, access: &mut FilteredAccess) { self.state.update_component_access(access); } @@ -735,7 +735,7 @@ impl WorldQuery for ChangeTrackers { /// The [`FetchState`] of [`ChangeTrackers`]. pub struct ChangeTrackersState { - component_id: ComponentId, + component_id: DataId, marker: PhantomData, } @@ -750,7 +750,7 @@ unsafe impl FetchState for ChangeTrackersState { } } - fn update_component_access(&self, access: &mut FilteredAccess) { + fn update_component_access(&self, access: &mut FilteredAccess) { if access.access().has_write(self.component_id) { panic!("ChangeTrackers<{}> conflicts with a previous access in this query. Shared access cannot coincide with exclusive access.", std::any::type_name::()); @@ -957,7 +957,7 @@ macro_rules! impl_tuple_fetch { ($($name::init(_world),)*) } - fn update_component_access(&self, _access: &mut FilteredAccess) { + fn update_component_access(&self, _access: &mut FilteredAccess) { let ($($name,)*) = self; $($name.update_component_access(_access);)* } diff --git a/crates/bevy_ecs/src/query/filter.rs b/crates/bevy_ecs/src/query/filter.rs index bbd8ec567a985..128c4555aa512 100644 --- a/crates/bevy_ecs/src/query/filter.rs +++ b/crates/bevy_ecs/src/query/filter.rs @@ -1,6 +1,6 @@ use crate::{ archetype::{Archetype, ArchetypeComponentId}, - component::{Component, ComponentId, ComponentStorage, ComponentTicks, StorageType}, + component::{Component, ComponentStorage, ComponentTicks, DataId, StorageType}, entity::Entity, query::{Access, Fetch, FetchState, FilteredAccess, WorldQuery}, storage::{ComponentSparseSet, Table, Tables}, @@ -81,7 +81,7 @@ pub struct WithFetch { /// The [`FetchState`] of [`With`]. pub struct WithState { - component_id: ComponentId, + component_id: DataId, marker: PhantomData, } @@ -96,7 +96,7 @@ unsafe impl FetchState for WithState { } #[inline] - fn update_component_access(&self, access: &mut FilteredAccess) { + fn update_component_access(&self, access: &mut FilteredAccess) { access.add_with(self.component_id); } @@ -200,7 +200,7 @@ pub struct WithoutFetch { /// The [`FetchState`] of [`Without`]. pub struct WithoutState { - component_id: ComponentId, + component_id: DataId, marker: PhantomData, } @@ -215,7 +215,7 @@ unsafe impl FetchState for WithoutState { } #[inline] - fn update_component_access(&self, access: &mut FilteredAccess) { + fn update_component_access(&self, access: &mut FilteredAccess) { access.add_without(self.component_id); } @@ -406,7 +406,7 @@ macro_rules! impl_query_filter_tuple { Or(($($filter::init(world),)*)) } - fn update_component_access(&self, access: &mut FilteredAccess) { + fn update_component_access(&self, access: &mut FilteredAccess) { let ($($filter,)*) = &self.0; $($filter.update_component_access(access);)* } @@ -457,7 +457,7 @@ macro_rules! impl_tick_filter { $(#[$state_meta])* pub struct $state_name { - component_id: ComponentId, + component_id: DataId, marker: PhantomData, } @@ -477,7 +477,7 @@ macro_rules! impl_tick_filter { } #[inline] - fn update_component_access(&self, access: &mut FilteredAccess) { + fn update_component_access(&self, access: &mut FilteredAccess) { if access.access().has_write(self.component_id) { panic!("$state_name<{}> conflicts with a previous access in this query. Shared access cannot coincide with exclusive access.", std::any::type_name::()); diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 6ecca3b870d72..6ccf6761fcce7 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -1,6 +1,6 @@ use crate::{ archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration, ArchetypeId}, - component::ComponentId, + component::DataId, entity::Entity, query::{ Access, Fetch, FetchState, FilterFetch, FilteredAccess, QueryCombinationIter, QueryIter, @@ -23,7 +23,7 @@ where pub(crate) matched_tables: FixedBitSet, pub(crate) matched_archetypes: FixedBitSet, pub(crate) archetype_component_access: Access, - pub(crate) component_access: FilteredAccess, + pub(crate) component_access: FilteredAccess, // NOTE: we maintain both a TableId bitset and a vec because iterating the vec is faster pub(crate) matched_table_ids: Vec, // NOTE: we maintain both a ArchetypeId bitset and a vec because iterating the vec is faster diff --git a/crates/bevy_ecs/src/schedule/run_criteria.rs b/crates/bevy_ecs/src/schedule/run_criteria.rs index 4104daab56d7c..dea4ee4b4ab9b 100644 --- a/crates/bevy_ecs/src/schedule/run_criteria.rs +++ b/crates/bevy_ecs/src/schedule/run_criteria.rs @@ -1,6 +1,6 @@ use crate::{ archetype::{Archetype, ArchetypeComponentId, ArchetypeGeneration}, - component::ComponentId, + component::DataId, query::Access, schedule::{BoxedRunCriteriaLabel, GraphNode, RunCriteriaLabel}, system::{BoxedSystem, IntoSystem, System}, @@ -399,7 +399,7 @@ where pub struct RunOnce { ran: bool, archetype_component_access: Access, - component_access: Access, + component_access: Access, } impl Default for RunOnce { @@ -422,7 +422,7 @@ impl System for RunOnce { fn new_archetype(&mut self, _archetype: &Archetype) {} - fn component_access(&self) -> &Access { + fn component_access(&self) -> &Access { &self.component_access } diff --git a/crates/bevy_ecs/src/schedule/stage.rs b/crates/bevy_ecs/src/schedule/stage.rs index 11cf3ea0c081f..c94595a4da8e6 100644 --- a/crates/bevy_ecs/src/schedule/stage.rs +++ b/crates/bevy_ecs/src/schedule/stage.rs @@ -1,5 +1,5 @@ use crate::{ - component::ComponentId, + component::DataId, prelude::IntoSystem, schedule::{ graph_utils::{self, DependencyGraphError}, @@ -477,7 +477,7 @@ impl SystemStage { fn write_display_names_of_pairs( string: &mut String, systems: &[impl SystemContainer], - mut ambiguities: Vec<(usize, usize, Vec)>, + mut ambiguities: Vec<(usize, usize, Vec)>, world: &World, ) { for (index_a, index_b, conflicts) in ambiguities.drain(..) { @@ -491,7 +491,7 @@ impl SystemStage { if !conflicts.is_empty() { let names = conflicts .iter() - .map(|id| world.components().get_info(*id).unwrap().name()) + .map(|id| world.data().get_info(*id).unwrap().name()) .collect::>(); writeln!(string, " conflicts: {:?}", names).unwrap(); } @@ -665,7 +665,7 @@ fn process_systems( /// Returns vector containing all pairs of indices of systems with ambiguous execution order, /// along with specific components that have triggered the warning. /// Systems must be topologically sorted beforehand. -fn find_ambiguities(systems: &[impl SystemContainer]) -> Vec<(usize, usize, Vec)> { +fn find_ambiguities(systems: &[impl SystemContainer]) -> Vec<(usize, usize, Vec)> { let mut ambiguity_set_labels = HashMap::default(); for set in systems.iter().flat_map(|c| c.ambiguity_sets()) { let len = ambiguity_set_labels.len(); diff --git a/crates/bevy_ecs/src/schedule/system_container.rs b/crates/bevy_ecs/src/schedule/system_container.rs index 68e493da714b5..c13fd78b322b5 100644 --- a/crates/bevy_ecs/src/schedule/system_container.rs +++ b/crates/bevy_ecs/src/schedule/system_container.rs @@ -1,5 +1,5 @@ use crate::{ - component::ComponentId, + component::DataId, query::Access, schedule::{ BoxedAmbiguitySetLabel, BoxedRunCriteriaLabel, BoxedSystemLabel, ExclusiveSystemDescriptor, @@ -21,7 +21,7 @@ pub trait SystemContainer: GraphNode