From 314458efbf97971324fc6f773cacb94659e0d6f7 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Wed, 17 Aug 2022 21:16:41 -0700 Subject: [PATCH 1/6] SystemParam for the name of the system you are currently in --- crates/bevy_ecs/src/system/system_param.rs | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 2356d732a8dda..62cf8a66619bd 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -16,6 +16,7 @@ pub use bevy_ecs_macros::SystemParam; use bevy_ecs_macros::{all_tuples, impl_param_set}; use bevy_ptr::UnsafeCellDeref; use std::{ + borrow::Cow, fmt::Debug, marker::PhantomData, ops::{Deref, DerefMut}, @@ -1304,6 +1305,51 @@ impl<'w, 's> SystemParamFetch<'w, 's> for SystemChangeTickState { } } +#[derive(Debug)] +pub struct SystemName { + name: Cow<'static, str>, +} + +impl SystemName { + pub fn name(&self) -> &Cow<'static, str> { + &self.name + } +} + +impl<'a> SystemParam for SystemName { + type Fetch = SystemNameState; +} + +// SAFETY: Only reads World entities +unsafe impl ReadOnlySystemParamFetch for SystemNameState {} + +/// The [`SystemParamState`] of [`Entities`]. +#[doc(hidden)] +pub struct SystemNameState; + +// SAFETY: no component value access +unsafe impl SystemParamState for SystemNameState { + fn init(_world: &mut World, _system_meta: &mut SystemMeta) -> Self { + Self + } +} + +impl<'w, 's> SystemParamFetch<'w, 's> for SystemNameState { + type Item = SystemName; + + #[inline] + unsafe fn get_param( + _state: &'s mut Self, + system_meta: &SystemMeta, + _world: &'w World, + _change_tick: u32, + ) -> Self::Item { + SystemName { + name: system_meta.name.clone(), + } + } +} + macro_rules! impl_system_param_tuple { ($($param: ident),*) => { impl<$($param: SystemParam),*> SystemParam for ($($param,)*) { From 51b42bf5d8a160d68a753bf7394a39f8db3a1db6 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 18 Aug 2022 02:50:01 -0700 Subject: [PATCH 2/6] Documentation, convenience methods, better handling of references --- crates/bevy_ecs/src/system/system_param.rs | 59 ++++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 62cf8a66619bd..af1774521e9c5 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1305,47 +1305,74 @@ impl<'w, 's> SystemParamFetch<'w, 's> for SystemChangeTickState { } } +/// Name of the system that corresponds to this [`SystemState`]. +/// +/// This is not a reliable identifier, it is more so useful for debugging +/// purposes of finding where a system parameter is being used incorrectly. #[derive(Debug)] -pub struct SystemName { - name: Cow<'static, str>, +pub struct SystemName<'s> { + name: &'s str, +} + +impl<'s> SystemName<'s> { + pub fn name(&self) -> &str { + self.name + } +} + +impl<'s> Deref for SystemName<'s> { + type Target = str; + fn deref(&self) -> &Self::Target { + self.name() + } } -impl SystemName { - pub fn name(&self) -> &Cow<'static, str> { - &self.name +impl<'s> AsRef for SystemName<'s> { + fn as_ref(&self) -> &str { + self.name() } } -impl<'a> SystemParam for SystemName { +impl<'s> Into<&'s str> for SystemName<'s> { + fn into(self) -> &'s str { + self.name + } +} + +impl<'s> SystemParam for SystemName<'s> { type Fetch = SystemNameState; } -// SAFETY: Only reads World entities -unsafe impl ReadOnlySystemParamFetch for SystemNameState {} +// SAFETY: Only reads internal system state +unsafe impl<'a> ReadOnlySystemParamFetch for SystemNameState {} -/// The [`SystemParamState`] of [`Entities`]. +/// The [`SystemParamState`] of [`SystemName`]. #[doc(hidden)] -pub struct SystemNameState; +pub struct SystemNameState { + name: Cow<'static, str>, +} // SAFETY: no component value access unsafe impl SystemParamState for SystemNameState { - fn init(_world: &mut World, _system_meta: &mut SystemMeta) -> Self { - Self + fn init(_world: &mut World, system_meta: &mut SystemMeta) -> Self { + Self { + name: system_meta.name.clone(), + } } } impl<'w, 's> SystemParamFetch<'w, 's> for SystemNameState { - type Item = SystemName; + type Item = SystemName<'s>; #[inline] unsafe fn get_param( - _state: &'s mut Self, - system_meta: &SystemMeta, + state: &'s mut Self, + _system_meta: &SystemMeta, _world: &'w World, _change_tick: u32, ) -> Self::Item { SystemName { - name: system_meta.name.clone(), + name: state.name.as_ref(), } } } From 38243d1cb51951c59bf5d55876fc2a4fb80b0146 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 18 Aug 2022 02:57:10 -0700 Subject: [PATCH 3/6] Display/debug niceties --- crates/bevy_ecs/src/system/system_param.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index af1774521e9c5..37172d7f6a6c0 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1309,7 +1309,6 @@ impl<'w, 's> SystemParamFetch<'w, 's> for SystemChangeTickState { /// /// This is not a reliable identifier, it is more so useful for debugging /// purposes of finding where a system parameter is being used incorrectly. -#[derive(Debug)] pub struct SystemName<'s> { name: &'s str, } @@ -1339,6 +1338,20 @@ impl<'s> Into<&'s str> for SystemName<'s> { } } +impl<'s> std::fmt::Debug for SystemName<'s> { + #[inline(always)] + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_tuple("SystemName").field(&self.name()).finish() + } +} + +impl<'s> std::fmt::Display for SystemName<'s> { + #[inline(always)] + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + std::fmt::Display::fmt(&self.name(), f) + } +} + impl<'s> SystemParam for SystemName<'s> { type Fetch = SystemNameState; } From f2c3d4f2a537f5517a35a7c0ec29b34b17a204b5 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 18 Aug 2022 02:58:56 -0700 Subject: [PATCH 4/6] Add `SystemName` to the list of system params --- crates/bevy_ecs/src/system/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index ec3fd6ea78553..aef76c277a5ec 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -59,6 +59,7 @@ //! - [`NonSendMut`] and `Option` //! - [`&World`](crate::world::World) //! - [`RemovedComponents`] +//! - [`SystemName`] //! - [`SystemChangeTick`] //! - [`Archetypes`](crate::archetype::Archetypes) (Provides Archetype metadata) //! - [`Bundles`](crate::bundle::Bundles) (Provides Bundles metadata) From 839d40226d61811721118a0a12837066953c993f Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 18 Aug 2022 03:10:27 -0700 Subject: [PATCH 5/6] Fix some warnings --- crates/bevy_ecs/src/system/system_param.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 37172d7f6a6c0..692bd3c192e49 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1332,9 +1332,9 @@ impl<'s> AsRef for SystemName<'s> { } } -impl<'s> Into<&'s str> for SystemName<'s> { - fn into(self) -> &'s str { - self.name +impl<'s> From> for &'s str { + fn from(name: SystemName<'s>) -> &'s str { + name.name } } @@ -1357,7 +1357,7 @@ impl<'s> SystemParam for SystemName<'s> { } // SAFETY: Only reads internal system state -unsafe impl<'a> ReadOnlySystemParamFetch for SystemNameState {} +unsafe impl ReadOnlySystemParamFetch for SystemNameState {} /// The [`SystemParamState`] of [`SystemName`]. #[doc(hidden)] From 3be65dcb93c658013f05ea722e05ab57a213934c Mon Sep 17 00:00:00 2001 From: Aceeri Date: Thu, 18 Aug 2022 05:06:05 -0700 Subject: [PATCH 6/6] Fix doc reference to SystemState --- crates/bevy_ecs/src/system/system_param.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 692bd3c192e49..8762bd64c4255 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1305,7 +1305,7 @@ impl<'w, 's> SystemParamFetch<'w, 's> for SystemChangeTickState { } } -/// Name of the system that corresponds to this [`SystemState`]. +/// Name of the system that corresponds to this [`crate::system::SystemState`]. /// /// This is not a reliable identifier, it is more so useful for debugging /// purposes of finding where a system parameter is being used incorrectly.