Skip to content

Commit e8b3892

Browse files
authored
Improve various Debug implementations (#9588)
# Objective * `Local` and `SystemName` implement `Debug` manually, but they could derive it. * `QueryState` and `dyn System` have unconventional debug formatting.
1 parent a6991c3 commit e8b3892

File tree

3 files changed

+16
-41
lines changed

3 files changed

+16
-41
lines changed

crates/bevy_ecs/src/query/state.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ pub struct QueryState<Q: WorldQuery, F: ReadOnlyWorldQuery = ()> {
3939

4040
impl<Q: WorldQuery, F: ReadOnlyWorldQuery> std::fmt::Debug for QueryState<Q, F> {
4141
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42-
write!(
43-
f,
44-
"QueryState<Q, F> matched_table_ids: {} matched_archetype_ids: {}",
45-
self.matched_table_ids.len(),
46-
self.matched_archetype_ids.len()
47-
)
42+
f.debug_struct("QueryState")
43+
.field("world_id", &self.world_id)
44+
.field("matched_table_count", &self.matched_table_ids.len())
45+
.field("matched_archetype_count", &self.matched_archetype_ids.len())
46+
.finish_non_exhaustive()
4847
}
4948
}
5049

crates/bevy_ecs/src/system/system.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,11 @@ pub(crate) fn check_system_change_tick(last_run: &mut Tick, this_run: Tick, syst
151151

152152
impl<In: 'static, Out: 'static> Debug for dyn System<In = In, Out = Out> {
153153
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
154-
write!(f, "System {}: {{{}}}", self.name(), {
155-
if self.is_send() {
156-
if self.is_exclusive() {
157-
"is_send is_exclusive"
158-
} else {
159-
"is_send"
160-
}
161-
} else if self.is_exclusive() {
162-
"is_exclusive"
163-
} else {
164-
""
165-
}
166-
},)
154+
f.debug_struct("System")
155+
.field("name", &self.name())
156+
.field("is_exclusive", &self.is_exclusive())
157+
.field("is_send", &self.is_send())
158+
.finish_non_exhaustive()
167159
}
168160
}
169161

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -679,20 +679,12 @@ unsafe impl SystemParam for &'_ World {
679679
/// // .add_systems(reset_to_system(my_config))
680680
/// # assert_is_system(reset_to_system(Config(10)));
681681
/// ```
682+
#[derive(Debug)]
682683
pub struct Local<'s, T: FromWorld + Send + 'static>(pub(crate) &'s mut T);
683684

684685
// SAFETY: Local only accesses internal state
685686
unsafe impl<'s, T: FromWorld + Send + 'static> ReadOnlySystemParam for Local<'s, T> {}
686687

687-
impl<'s, T: FromWorld + Send + Sync + 'static> Debug for Local<'s, T>
688-
where
689-
T: Debug,
690-
{
691-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
692-
f.debug_tuple("Local").field(&self.0).finish()
693-
}
694-
}
695-
696688
impl<'s, T: FromWorld + Send + Sync + 'static> Deref for Local<'s, T> {
697689
type Target = T;
698690

@@ -1300,14 +1292,13 @@ unsafe impl SystemParam for SystemChangeTick {
13001292
///
13011293
/// This is not a reliable identifier, it is more so useful for debugging
13021294
/// purposes of finding where a system parameter is being used incorrectly.
1303-
pub struct SystemName<'s> {
1304-
name: &'s str,
1305-
}
1295+
#[derive(Debug)]
1296+
pub struct SystemName<'s>(&'s str);
13061297

13071298
impl<'s> SystemName<'s> {
13081299
/// Gets the name of the system.
13091300
pub fn name(&self) -> &str {
1310-
self.name
1301+
self.0
13111302
}
13121303
}
13131304

@@ -1326,14 +1317,7 @@ impl<'s> AsRef<str> for SystemName<'s> {
13261317

13271318
impl<'s> From<SystemName<'s>> for &'s str {
13281319
fn from(name: SystemName<'s>) -> &'s str {
1329-
name.name
1330-
}
1331-
}
1332-
1333-
impl<'s> std::fmt::Debug for SystemName<'s> {
1334-
#[inline(always)]
1335-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1336-
f.debug_tuple("SystemName").field(&self.name()).finish()
1320+
name.0
13371321
}
13381322
}
13391323

@@ -1360,7 +1344,7 @@ unsafe impl SystemParam for SystemName<'_> {
13601344
_world: UnsafeWorldCell<'w>,
13611345
_change_tick: Tick,
13621346
) -> Self::Item<'w, 's> {
1363-
SystemName { name }
1347+
SystemName(name)
13641348
}
13651349
}
13661350

0 commit comments

Comments
 (0)