From 09049676a58aea42fc710bbd3137b5f5d9f0644b Mon Sep 17 00:00:00 2001 From: Aceeri Date: Fri, 13 Jan 2023 22:44:25 -0800 Subject: [PATCH 1/7] Debug name for entities --- crates/bevy_core/src/name.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs index f7fad0ca18e26..143366c9cc32c 100644 --- a/crates/bevy_core/src/name.rs +++ b/crates/bevy_core/src/name.rs @@ -1,4 +1,10 @@ -use bevy_ecs::{component::Component, reflect::ReflectComponent}; +use bevy_ecs::{ + component::Component, + entity::Entity, + prelude::Query, + query::{ReadOnlyWorldQuery, WorldQuery}, + reflect::ReflectComponent, +}; use bevy_reflect::Reflect; use bevy_reflect::{std_traits::ReflectDefault, FromReflect}; use bevy_utils::AHasher; @@ -76,6 +82,30 @@ impl std::fmt::Display for Name { } } +/// An extension trait for [`Query`] that attempts to give a user friendly +/// name for an entity if available. +pub trait DebugNameQueryExt<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> { + /// Give the name of an entity if it has one, otherwise use the entity id + /// as the name. + fn debug_name(&'w self, entity: Entity) -> Box + where + Q::ReadOnly: WorldQuery = &'w Name>; +} + +impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> DebugNameQueryExt<'w, 's, Q, F> + for Query<'w, 's, Q, F> +{ + fn debug_name(&'w self, entity: Entity) -> Box + where + Q::ReadOnly: WorldQuery = &'w Name>, + { + match self.get(entity).ok() { + Some(name) => Box::new(name.as_str()), + None => Box::new(entity), + } + } +} + /* Conversions from strings */ impl From<&str> for Name { From 8278ab178f7824a147dd107fb25f5fd5c92edd27 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Fri, 13 Jan 2023 22:48:38 -0800 Subject: [PATCH 2/7] Add to prelude --- crates/bevy_core/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index a3b91ea2d9fc7..86c8489bcbe2c 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -15,7 +15,8 @@ pub mod prelude { //! The Bevy Core Prelude. #[doc(hidden)] pub use crate::{ - FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin, + DebugNameQueryExt, FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, + TypeRegistrationPlugin, }; } From d205e1edf1f37835010c930fd1e6405185cd6ed6 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Fri, 13 Jan 2023 22:54:56 -0800 Subject: [PATCH 3/7] Better docs --- crates/bevy_core/src/name.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs index 143366c9cc32c..b057c0b70062c 100644 --- a/crates/bevy_core/src/name.rs +++ b/crates/bevy_core/src/name.rs @@ -82,11 +82,9 @@ impl std::fmt::Display for Name { } } -/// An extension trait for [`Query`] that attempts to give a user friendly -/// name for an entity if available. +/// An extension trait for [`Query`] for more user friendly debug information about an entity. pub trait DebugNameQueryExt<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> { - /// Give the name of an entity if it has one, otherwise use the entity id - /// as the name. + /// Give the [`Name`] of an entity if it has one, otherwise return the [`Entity`]. fn debug_name(&'w self, entity: Entity) -> Box where Q::ReadOnly: WorldQuery = &'w Name>; From df94739b6d452ad39408a80e0ebc9577b37edca1 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Wed, 18 Jan 2023 03:10:55 -0800 Subject: [PATCH 4/7] Refactor into worldquery --- crates/bevy_core/src/lib.rs | 3 +-- crates/bevy_core/src/name.rs | 35 ++++++++++++++--------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 86c8489bcbe2c..1b92d00d8911c 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -15,8 +15,7 @@ pub mod prelude { //! The Bevy Core Prelude. #[doc(hidden)] pub use crate::{ - DebugNameQueryExt, FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, - TypeRegistrationPlugin, + DebugName, FrameCountPlugin, Name, TaskPoolOptions, TaskPoolPlugin, TypeRegistrationPlugin, }; } diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs index b057c0b70062c..1681f82be4d20 100644 --- a/crates/bevy_core/src/name.rs +++ b/crates/bevy_core/src/name.rs @@ -1,9 +1,5 @@ use bevy_ecs::{ - component::Component, - entity::Entity, - prelude::Query, - query::{ReadOnlyWorldQuery, WorldQuery}, - reflect::ReflectComponent, + component::Component, entity::Entity, query::WorldQuery, reflect::ReflectComponent, }; use bevy_reflect::Reflect; use bevy_reflect::{std_traits::ReflectDefault, FromReflect}; @@ -82,24 +78,21 @@ impl std::fmt::Display for Name { } } -/// An extension trait for [`Query`] for more user friendly debug information about an entity. -pub trait DebugNameQueryExt<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> { - /// Give the [`Name`] of an entity if it has one, otherwise return the [`Entity`]. - fn debug_name(&'w self, entity: Entity) -> Box - where - Q::ReadOnly: WorldQuery = &'w Name>; +/// Convenient query for giving a human friendly name to an entity. +#[derive(WorldQuery)] +pub struct DebugName { + /// A [`Name`] that the entity might have that is displayed if available. + pub name: Option<&'static Name>, + /// The unique identifier of the entity as a fallback. + pub entity: Entity, } -impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> DebugNameQueryExt<'w, 's, Q, F> - for Query<'w, 's, Q, F> -{ - fn debug_name(&'w self, entity: Entity) -> Box - where - Q::ReadOnly: WorldQuery = &'w Name>, - { - match self.get(entity).ok() { - Some(name) => Box::new(name.as_str()), - None => Box::new(entity), +impl std::fmt::Debug for DebugName { + #[inline(always)] + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self.name { + Some(name) => std::fmt::Debug::fmt(&name, f), + None => std::fmt::Debug::fmt(&self.entity, f), } } } From 8e29254631d814f1c98cc631377d176730a38286 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Wed, 25 Jan 2023 20:15:21 -0800 Subject: [PATCH 5/7] Print entity with the name --- crates/bevy_core/src/name.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs index 1681f82be4d20..e51200489b5be 100644 --- a/crates/bevy_core/src/name.rs +++ b/crates/bevy_core/src/name.rs @@ -91,7 +91,7 @@ impl std::fmt::Debug for DebugName { #[inline(always)] fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self.name { - Some(name) => std::fmt::Debug::fmt(&name, f), + Some(name) => write!(f, "{:?} ({:?})", &name, &self.entity), None => std::fmt::Debug::fmt(&self.entity, f), } } From d81d8cc75fe735089bf304d09a620d8756faee42 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 26 Jan 2023 11:26:19 -0800 Subject: [PATCH 6/7] Doctest for DebugName --- crates/bevy_core/src/name.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs index e51200489b5be..2fabc680709f1 100644 --- a/crates/bevy_core/src/name.rs +++ b/crates/bevy_core/src/name.rs @@ -79,6 +79,21 @@ impl std::fmt::Display for Name { } /// Convenient query for giving a human friendly name to an entity. +/// +/// ```rust +/// # use bevy_core::prelude::*; +/// # use bevy_ecs::prelude::*; +/// # #[derive(Component)] pub struct Score(f32); +/// fn increment_score(mut scores: Query<(DebugName, &mut Score)>) { +/// for (name, mut score) in &mut scores { +/// score.0 += 1.0; +/// if score.0.is_nan() { +/// bevy_utils::tracing::error!("Score for {:?} is invalid", name); +/// } +/// } +/// } +/// # bevy_ecs::system::assert_is_system(increment_score); +/// ``` #[derive(WorldQuery)] pub struct DebugName { /// A [`Name`] that the entity might have that is displayed if available. @@ -87,7 +102,7 @@ pub struct DebugName { pub entity: Entity, } -impl std::fmt::Debug for DebugName { +impl<'a> std::fmt::Debug for DebugNameItem<'a> { #[inline(always)] fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self.name { From aaa5677054324ffd23f0ec00628065fd373a18bf Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 26 Jan 2023 11:27:57 -0800 Subject: [PATCH 7/7] formatting --- crates/bevy_core/src/name.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs index 2fabc680709f1..145c6e8060e93 100644 --- a/crates/bevy_core/src/name.rs +++ b/crates/bevy_core/src/name.rs @@ -79,7 +79,7 @@ impl std::fmt::Display for Name { } /// Convenient query for giving a human friendly name to an entity. -/// +/// /// ```rust /// # use bevy_core::prelude::*; /// # use bevy_ecs::prelude::*;