Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate cfg(feature = "track_location") in a type. #17602

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
85 changes: 16 additions & 69 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
Archetype, ArchetypeAfterBundleInsert, ArchetypeId, Archetypes, BundleComponentStatus,
ComponentStatus, SpawnBundleStatus,
},
change_detection::MaybeLocation,
component::{
Component, ComponentId, Components, RequiredComponentConstructor, RequiredComponents,
StorageType, Tick,
Expand All @@ -24,8 +25,6 @@ use alloc::{boxed::Box, vec, vec::Vec};
use bevy_platform_support::collections::{HashMap, HashSet};
use bevy_ptr::{ConstNonNull, OwningPtr};
use bevy_utils::TypeIdMap;
#[cfg(feature = "track_location")]
use core::panic::Location;
use core::{any::TypeId, ptr::NonNull};
use variadics_please::all_tuples;

Expand Down Expand Up @@ -534,7 +533,7 @@ impl BundleInfo {
change_tick: Tick,
bundle: T,
insert_mode: InsertMode,
#[cfg(feature = "track_location")] caller: &'static Location<'static>,
caller: MaybeLocation,
) {
// NOTE: get_components calls this closure on each component in "bundle order".
// bundle_info.component_ids are also in "bundle order"
Expand All @@ -549,20 +548,12 @@ impl BundleInfo {
// the target table contains the component.
let column = table.get_column_mut(component_id).debug_checked_unwrap();
match (status, insert_mode) {
(ComponentStatus::Added, _) => column.initialize(
table_row,
component_ptr,
change_tick,
#[cfg(feature = "track_location")]
caller,
),
(ComponentStatus::Existing, InsertMode::Replace) => column.replace(
table_row,
component_ptr,
change_tick,
#[cfg(feature = "track_location")]
caller,
),
(ComponentStatus::Added, _) => {
column.initialize(table_row, component_ptr, change_tick, caller);
}
(ComponentStatus::Existing, InsertMode::Replace) => {
column.replace(table_row, component_ptr, change_tick, caller);
}
(ComponentStatus::Existing, InsertMode::Keep) => {
if let Some(drop_fn) = table.get_drop_for(component_id) {
drop_fn(component_ptr);
Expand All @@ -575,13 +566,7 @@ impl BundleInfo {
// SAFETY: If component_id is in self.component_ids, BundleInfo::new ensures that
// a sparse set exists for the component.
unsafe { sparse_sets.get_mut(component_id).debug_checked_unwrap() };
sparse_set.insert(
entity,
component_ptr,
change_tick,
#[cfg(feature = "track_location")]
caller,
);
sparse_set.insert(entity, component_ptr, change_tick, caller);
}
}
bundle_component += 1;
Expand All @@ -594,7 +579,6 @@ impl BundleInfo {
change_tick,
table_row,
entity,
#[cfg(feature = "track_location")]
caller,
);
}
Expand All @@ -621,7 +605,7 @@ impl BundleInfo {
component_id: ComponentId,
storage_type: StorageType,
component_ptr: OwningPtr,
#[cfg(feature = "track_location")] caller: &'static Location<'static>,
caller: MaybeLocation,
) {
{
match storage_type {
Expand All @@ -630,26 +614,14 @@ impl BundleInfo {
// SAFETY: If component_id is in required_components, BundleInfo::new requires that
// the target table contains the component.
unsafe { table.get_column_mut(component_id).debug_checked_unwrap() };
column.initialize(
table_row,
component_ptr,
change_tick,
#[cfg(feature = "track_location")]
caller,
);
column.initialize(table_row, component_ptr, change_tick, caller);
}
StorageType::SparseSet => {
let sparse_set =
// SAFETY: If component_id is in required_components, BundleInfo::new requires that
// a sparse set exists for the component.
unsafe { sparse_sets.get_mut(component_id).debug_checked_unwrap() };
sparse_set.insert(
entity,
component_ptr,
change_tick,
#[cfg(feature = "track_location")]
caller,
);
sparse_set.insert(entity, component_ptr, change_tick, caller);
}
}
}
Expand Down Expand Up @@ -1036,7 +1008,7 @@ impl<'w> BundleInserter<'w> {
location: EntityLocation,
bundle: T,
insert_mode: InsertMode,
#[cfg(feature = "track_location")] caller: &'static Location<'static>,
caller: MaybeLocation,
) -> EntityLocation {
let bundle_info = self.bundle_info.as_ref();
let archetype_after_insert = self.archetype_after_insert.as_ref();
Expand All @@ -1054,15 +1026,13 @@ impl<'w> BundleInserter<'w> {
ON_REPLACE,
entity,
archetype_after_insert.iter_existing(),
#[cfg(feature = "track_location")]
caller,
);
}
deferred_world.trigger_on_replace(
archetype,
entity,
archetype_after_insert.iter_existing(),
#[cfg(feature = "track_location")]
caller,
);
}
Expand Down Expand Up @@ -1092,7 +1062,6 @@ impl<'w> BundleInserter<'w> {
self.change_tick,
bundle,
insert_mode,
#[cfg(feature = "track_location")]
caller,
);

Expand Down Expand Up @@ -1134,7 +1103,6 @@ impl<'w> BundleInserter<'w> {
self.change_tick,
bundle,
insert_mode,
#[cfg(feature = "track_location")]
caller,
);

Expand Down Expand Up @@ -1217,7 +1185,6 @@ impl<'w> BundleInserter<'w> {
self.change_tick,
bundle,
insert_mode,
#[cfg(feature = "track_location")]
caller,
);

Expand All @@ -1236,15 +1203,13 @@ impl<'w> BundleInserter<'w> {
new_archetype,
entity,
archetype_after_insert.iter_added(),
#[cfg(feature = "track_location")]
caller,
);
if new_archetype.has_add_observer() {
deferred_world.trigger_observers(
ON_ADD,
entity,
archetype_after_insert.iter_added(),
#[cfg(feature = "track_location")]
caller,
);
}
Expand All @@ -1255,15 +1220,13 @@ impl<'w> BundleInserter<'w> {
new_archetype,
entity,
archetype_after_insert.iter_inserted(),
#[cfg(feature = "track_location")]
caller,
);
if new_archetype.has_insert_observer() {
deferred_world.trigger_observers(
ON_INSERT,
entity,
archetype_after_insert.iter_inserted(),
#[cfg(feature = "track_location")]
caller,
);
}
Expand All @@ -1275,15 +1238,13 @@ impl<'w> BundleInserter<'w> {
new_archetype,
entity,
archetype_after_insert.iter_added(),
#[cfg(feature = "track_location")]
caller,
);
if new_archetype.has_insert_observer() {
deferred_world.trigger_observers(
ON_INSERT,
entity,
archetype_after_insert.iter_added(),
#[cfg(feature = "track_location")]
caller,
);
}
Expand Down Expand Up @@ -1365,7 +1326,7 @@ impl<'w> BundleSpawner<'w> {
&mut self,
entity: Entity,
bundle: T,
#[cfg(feature = "track_location")] caller: &'static Location<'static>,
caller: MaybeLocation,
) -> EntityLocation {
// SAFETY: We do not make any structural changes to the archetype graph through self.world so these pointers always remain valid
let bundle_info = self.bundle_info.as_ref();
Expand All @@ -1390,7 +1351,6 @@ impl<'w> BundleSpawner<'w> {
self.change_tick,
bundle,
InsertMode::Replace,
#[cfg(feature = "track_location")]
caller,
);
entities.set(entity.index(), location);
Expand All @@ -1408,31 +1368,27 @@ impl<'w> BundleSpawner<'w> {
archetype,
entity,
bundle_info.iter_contributed_components(),
#[cfg(feature = "track_location")]
caller,
);
if archetype.has_add_observer() {
deferred_world.trigger_observers(
ON_ADD,
entity,
bundle_info.iter_contributed_components(),
#[cfg(feature = "track_location")]
caller,
);
}
deferred_world.trigger_on_insert(
archetype,
entity,
bundle_info.iter_contributed_components(),
#[cfg(feature = "track_location")]
caller,
);
if archetype.has_insert_observer() {
deferred_world.trigger_observers(
ON_INSERT,
entity,
bundle_info.iter_contributed_components(),
#[cfg(feature = "track_location")]
caller,
);
}
Expand All @@ -1444,20 +1400,11 @@ impl<'w> BundleSpawner<'w> {
/// # Safety
/// `T` must match this [`BundleInfo`]'s type
#[inline]
pub unsafe fn spawn<T: Bundle>(
&mut self,
bundle: T,
#[cfg(feature = "track_location")] caller: &'static Location<'static>,
) -> Entity {
pub unsafe fn spawn<T: Bundle>(&mut self, bundle: T, caller: MaybeLocation) -> Entity {
let entity = self.entities().alloc();
// SAFETY: entity is allocated (but non-existent), `T` matches this BundleInfo's type
unsafe {
self.spawn_non_existent(
entity,
bundle,
#[cfg(feature = "track_location")]
caller,
);
self.spawn_non_existent(entity, bundle, caller);
}
entity
}
Expand Down
Loading