Path: blob/main/crates/bevy_ecs/src/system/system_name.rs
6604 views
use crate::{1component::Tick,2prelude::World,3query::FilteredAccessSet,4system::{ExclusiveSystemParam, ReadOnlySystemParam, SystemMeta, SystemParam},5world::unsafe_world_cell::UnsafeWorldCell,6};7use bevy_utils::prelude::DebugName;8use derive_more::derive::{Display, Into};910/// [`SystemParam`] that returns the name of the system which it is used in.11///12/// This is not a reliable identifier, it is more so useful for debugging or logging.13///14/// # Examples15///16/// ```17/// # use bevy_ecs::system::SystemName;18/// # use bevy_ecs::system::SystemParam;19///20/// #[derive(SystemParam)]21/// struct Logger {22/// system_name: SystemName,23/// }24///25/// impl Logger {26/// fn log(&mut self, message: &str) {27/// eprintln!("{}: {}", self.system_name, message);28/// }29/// }30///31/// fn system1(mut logger: Logger) {32/// // Prints: "crate_name::mod_name::system1: Hello".33/// logger.log("Hello");34/// }35/// ```36#[derive(Debug, Into, Display)]37pub struct SystemName(DebugName);3839impl SystemName {40/// Gets the name of the system.41pub fn name(&self) -> DebugName {42self.0.clone()43}44}4546// SAFETY: no component value access47unsafe impl SystemParam for SystemName {48type State = ();49type Item<'w, 's> = SystemName;5051fn init_state(_world: &mut World) -> Self::State {}5253fn init_access(54_state: &Self::State,55_system_meta: &mut SystemMeta,56_component_access_set: &mut FilteredAccessSet,57_world: &mut World,58) {59}6061#[inline]62unsafe fn get_param<'w, 's>(63_state: &'s mut Self::State,64system_meta: &SystemMeta,65_world: UnsafeWorldCell<'w>,66_change_tick: Tick,67) -> Self::Item<'w, 's> {68SystemName(system_meta.name.clone())69}70}7172// SAFETY: Only reads internal system state73unsafe impl ReadOnlySystemParam for SystemName {}7475impl ExclusiveSystemParam for SystemName {76type State = ();77type Item<'s> = SystemName;7879fn init(_world: &mut World, _system_meta: &mut SystemMeta) -> Self::State {}8081fn get_param<'s>(_state: &'s mut Self::State, system_meta: &SystemMeta) -> Self::Item<'s> {82SystemName(system_meta.name.clone())83}84}8586#[cfg(test)]87#[cfg(feature = "trace")]88mod tests {89use crate::{90system::{IntoSystem, RunSystemOnce, SystemName},91world::World,92};93use alloc::{borrow::ToOwned, string::String};9495#[test]96fn test_system_name_regular_param() {97fn testing(name: SystemName) -> String {98name.name().as_string()99}100101let mut world = World::default();102let id = world.register_system(testing);103let name = world.run_system(id).unwrap();104assert!(name.ends_with("testing"));105}106107#[test]108fn test_system_name_exclusive_param() {109fn testing(_world: &mut World, name: SystemName) -> String {110name.name().as_string()111}112113let mut world = World::default();114let id = world.register_system(testing);115let name = world.run_system(id).unwrap();116assert!(name.ends_with("testing"));117}118119#[test]120fn test_closure_system_name_regular_param() {121let mut world = World::default();122let system =123IntoSystem::into_system(|name: SystemName| name.name().to_owned()).with_name("testing");124let name = world.run_system_once(system).unwrap().as_string();125assert_eq!(name, "testing");126}127128#[test]129fn test_exclusive_closure_system_name_regular_param() {130let mut world = World::default();131let system =132IntoSystem::into_system(|_world: &mut World, name: SystemName| name.name().to_owned())133.with_name("testing");134let name = world.run_system_once(system).unwrap().as_string();135assert_eq!(name, "testing");136}137}138139140