bevy_ecs/system/
system_name.rs1use crate::{
2 change_detection::Tick,
3 prelude::World,
4 system::{
5 ReadOnlySystemParam, SystemAccess, SystemMeta, SystemParam, SystemParamValidationError,
6 },
7 world::unsafe_world_cell::UnsafeWorldCell,
8};
9use bevy_utils::prelude::DebugName;
10use derive_more::derive::{Display, Into};
11
12#[derive(Debug, Into, Display)]
39pub struct SystemName(DebugName);
40
41impl SystemName {
42 pub fn name(&self) -> DebugName {
44 self.0.clone()
45 }
46
47 pub fn as_str(&self) -> &str {
49 &self.0
50 }
51}
52
53unsafe impl SystemParam for SystemName {
55 type State = ();
56 type Item<'w, 's> = SystemName;
57
58 fn init_state(_world: &mut World) -> Self::State {}
59
60 fn init_access(
61 _state: &Self::State,
62 _system_meta: &mut SystemMeta,
63 _system_access: &mut SystemAccess,
64 _world: &mut World,
65 ) {
66 }
67
68 #[inline]
69 unsafe fn get_param<'w, 's>(
70 _state: &'s mut Self::State,
71 system_meta: &SystemMeta,
72 _world: UnsafeWorldCell<'w>,
73 _change_tick: Tick,
74 ) -> Result<Self::Item<'w, 's>, SystemParamValidationError> {
75 Ok(SystemName(system_meta.name.clone()))
76 }
77}
78
79unsafe impl ReadOnlySystemParam for SystemName {}
81
82#[cfg(test)]
83#[cfg(all(feature = "trace", feature = "debug"))]
84mod tests {
85 use crate::{
86 system::{IntoSystem, RunSystemOnce, SystemName},
87 world::World,
88 };
89 use alloc::{borrow::ToOwned, string::String};
90
91 #[test]
92 fn test_system_name_regular_param() {
93 fn testing(name: SystemName) -> String {
94 name.name().as_string()
95 }
96
97 let mut world = World::default();
98 let id = world.register_system(testing);
99 let name = world.run_system(id).unwrap();
100 assert!(name.ends_with("testing"));
101 }
102
103 #[test]
104 fn test_system_name_exclusive_param() {
105 fn testing(_world: &mut World, name: SystemName) -> String {
106 name.name().as_string()
107 }
108
109 let mut world = World::default();
110 let id = world.register_system(testing);
111 let name = world.run_system(id).unwrap();
112 assert!(name.ends_with("testing"));
113 }
114
115 #[test]
116 fn test_closure_system_name_regular_param() {
117 let mut world = World::default();
118 let system =
119 IntoSystem::into_system(|name: SystemName| name.name().to_owned()).with_name("testing");
120 let name = world.run_system_once(system).unwrap().as_string();
121 assert_eq!(name, "testing");
122 }
123
124 #[test]
125 fn test_exclusive_closure_system_name_regular_param() {
126 let mut world = World::default();
127 let system =
128 IntoSystem::into_system(|_world: &mut World, name: SystemName| name.name().to_owned())
129 .with_name("testing");
130 let name = world.run_system_once(system).unwrap().as_string();
131 assert_eq!(name, "testing");
132 }
133}