Skip to content

Commit baddb7e

Browse files
authored
Remove uses of #[cfg(feature = "track_location")] outside of the implementation of MaybeLocation (#21399)
# Objective Remove uses of `#[cfg(feature = "track_location")]` outside of the implementation of `MaybeLocation`. Reducing the amount of conditional compilation makes the code less brittle, since compilation errors will be found regardless of whether the feature is enabled. We also want to ensure that uses of `MaybeLocation` outside of `bevy_ecs` don't need their *own* feature flags. Avoiding the flags even within `bevy_ecs` helps ensure that all use cases are covered, and makes it easier to move or copy implementations from `bevy_ecs` into other crates. ## Solution Remove `#[cfg(feature = "track_location")]` annotations. Use `.into_option()` in the cases where we want the behavior to depend on whether the feature is enabled.
1 parent fa40e69 commit baddb7e

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

crates/bevy_ecs/src/world/command_queue.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#[cfg(feature = "track_location")]
2-
use crate::change_detection::MaybeLocation;
31
use crate::{
2+
change_detection::MaybeLocation,
43
system::{Command, SystemBuffer, SystemMeta},
54
world::{DeferredWorld, World},
65
};
@@ -41,7 +40,6 @@ pub struct CommandQueue {
4140
pub(crate) bytes: Vec<MaybeUninit<u8>>,
4241
pub(crate) cursor: usize,
4342
pub(crate) panic_recovery: Vec<MaybeUninit<u8>>,
44-
#[cfg(feature = "track_location")]
4543
pub(crate) caller: MaybeLocation,
4644
}
4745

@@ -52,7 +50,6 @@ impl Default for CommandQueue {
5250
bytes: Default::default(),
5351
cursor: Default::default(),
5452
panic_recovery: Default::default(),
55-
#[cfg(feature = "track_location")]
5653
caller: MaybeLocation::caller(),
5754
}
5855
}
@@ -74,13 +71,10 @@ pub(crate) struct RawCommandQueue {
7471
// So instead, the manual impl just prints the length of vec.
7572
impl Debug for CommandQueue {
7673
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77-
let mut binding = f.debug_struct("CommandQueue");
78-
binding.field("len_bytes", &self.bytes.len());
79-
80-
#[cfg(feature = "track_location")]
81-
binding.field("caller", &self.caller.into_option());
82-
83-
binding.finish_non_exhaustive()
74+
f.debug_struct("CommandQueue")
75+
.field("len_bytes", &self.bytes.len())
76+
.field("caller", &self.caller)
77+
.finish_non_exhaustive()
8478
}
8579
}
8680

@@ -332,10 +326,11 @@ impl RawCommandQueue {
332326
impl Drop for CommandQueue {
333327
fn drop(&mut self) {
334328
if !self.bytes.is_empty() {
335-
#[cfg(feature = "track_location")]
336-
warn!("CommandQueue has un-applied commands being dropped. Did you forget to call SystemState::apply? caller:{:?}",self.caller.into_option());
337-
#[cfg(not(feature = "track_location"))]
338-
warn!("CommandQueue has un-applied commands being dropped. Did you forget to call SystemState::apply?");
329+
if let Some(caller) = self.caller.into_option() {
330+
warn!("CommandQueue has un-applied commands being dropped. Did you forget to call SystemState::apply? caller:{caller:?}");
331+
} else {
332+
warn!("CommandQueue has un-applied commands being dropped. Did you forget to call SystemState::apply?");
333+
}
339334
}
340335
// SAFETY: A reference is always a valid pointer
341336
unsafe { self.get_raw().apply_or_drop_queued(None) };

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6753,15 +6753,19 @@ mod tests {
67536753
let _id2 = world.spawn(Marker).id();
67546754
let id3 = world.spawn(Marker).id();
67556755

6756-
#[cfg(feature = "track_location")]
67576756
let e1_spawned = world.entity(id1).spawned_by();
67586757

67596758
let spawn = world.entity(id3).spawned_by();
67606759
world.entity_mut(id1).despawn();
6761-
#[cfg(feature = "track_location")]
67626760
let e1_despawned = world.entities().entity_get_spawned_or_despawned_by(id1);
6763-
#[cfg(feature = "track_location")]
6764-
assert_ne!(e1_spawned.map(Some), e1_despawned);
6761+
6762+
// These assertions are only possible if the `track_location` feature is enabled
6763+
if let (Some(e1_spawned), Some(e1_despawned)) =
6764+
(e1_spawned.into_option(), e1_despawned.into_option())
6765+
{
6766+
assert!(e1_despawned.is_some());
6767+
assert_ne!(Some(e1_spawned), e1_despawned);
6768+
}
67656769

67666770
let spawn_after = world.entity(id3).spawned_by();
67676771
assert_eq!(spawn, spawn_after);

0 commit comments

Comments
 (0)