bevy_utils/
debug_info.rs

1use crate::cfg;
2cfg::alloc! {
3    use alloc::{borrow::Cow, fmt, string::String};
4}
5#[cfg(feature = "debug")]
6use core::any::type_name;
7use core::ops::Deref;
8use disqualified::ShortName;
9
10#[cfg(not(feature = "debug"))]
11const FEATURE_DISABLED: &str = "Enable the debug feature to see the name";
12
13/// Wrapper to help debugging ECS issues. This is used to display the names of systems, components, ...
14///
15/// * If the `debug` feature is enabled, the actual name will be used
16/// * If it is disabled, a string mentioning the disabled feature will be used
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct DebugName {
19    #[cfg(feature = "debug")]
20    name: Cow<'static, str>,
21}
22
23cfg::alloc! {
24    impl fmt::Display for DebugName {
25        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26            #[cfg(feature = "debug")]
27            f.write_str(self.name.as_ref())?;
28            #[cfg(not(feature = "debug"))]
29            f.write_str(FEATURE_DISABLED)?;
30
31            Ok(())
32        }
33    }
34}
35
36impl DebugName {
37    /// Create a new `DebugName` from a `&str`
38    ///
39    /// The value will be ignored if the `debug` feature is not enabled
40    #[cfg_attr(
41        not(feature = "debug"),
42        expect(
43            unused_variables,
44            reason = "The value will be ignored if the `debug` feature is not enabled"
45        )
46    )]
47    pub const fn borrowed(value: &'static str) -> Self {
48        DebugName {
49            #[cfg(feature = "debug")]
50            name: Cow::Borrowed(value),
51        }
52    }
53
54    cfg::alloc! {
55        /// Create a new `DebugName` from a `String`
56        ///
57        /// The value will be ignored if the `debug` feature is not enabled
58        #[cfg_attr(
59            not(feature = "debug"),
60            expect(
61                unused_variables,
62                reason = "The value will be ignored if the `debug` feature is not enabled"
63            )
64        )]
65        pub fn owned(value: String) -> Self {
66            DebugName {
67                #[cfg(feature = "debug")]
68                name: Cow::Owned(value),
69            }
70        }
71    }
72
73    /// Create a new `DebugName` from a type by using its [`core::any::type_name`]
74    ///
75    /// The value will be ignored if the `debug` feature is not enabled
76    pub fn type_name<T>() -> Self {
77        DebugName {
78            #[cfg(feature = "debug")]
79            name: Cow::Borrowed(type_name::<T>()),
80        }
81    }
82
83    /// Get the [`ShortName`] corresponding to this debug name
84    ///
85    /// The value will be a static string if the `debug` feature is not enabled
86    pub fn shortname(&self) -> ShortName<'_> {
87        #[cfg(feature = "debug")]
88        return ShortName(self.name.as_ref());
89        #[cfg(not(feature = "debug"))]
90        return ShortName(FEATURE_DISABLED);
91    }
92
93    /// Return the string hold by this `DebugName`
94    ///
95    /// This is intended for debugging purpose, and only available if the `debug` feature is enabled
96    #[cfg(feature = "debug")]
97    pub fn as_string(&self) -> String {
98        self.name.clone().into_owned()
99    }
100}
101
102impl Deref for DebugName {
103    type Target = str;
104
105    fn deref(&self) -> &Self::Target {
106        #[cfg(feature = "debug")]
107        return &self.name;
108        #[cfg(not(feature = "debug"))]
109        return FEATURE_DISABLED;
110    }
111}
112
113cfg::alloc! {
114    impl From<Cow<'static, str>> for DebugName {
115        #[cfg_attr(
116            not(feature = "debug"),
117            expect(
118                unused_variables,
119                reason = "The value will be ignored if the `debug` feature is not enabled"
120            )
121        )]
122        fn from(value: Cow<'static, str>) -> Self {
123            Self {
124                #[cfg(feature = "debug")]
125                name: value,
126            }
127        }
128    }
129
130    impl From<String> for DebugName {
131        fn from(value: String) -> Self {
132            Self::owned(value)
133        }
134    }
135
136    impl From<DebugName> for Cow<'static, str> {
137        #[cfg_attr(
138            not(feature = "debug"),
139            expect(
140                unused_variables,
141                reason = "The value will be ignored if the `debug` feature is not enabled"
142            )
143        )]
144        fn from(value: DebugName) -> Self {
145            #[cfg(feature = "debug")]
146            {
147                value.name
148            }
149            #[cfg(not(feature = "debug"))]
150            {
151                Cow::Borrowed(FEATURE_DISABLED)
152            }
153        }
154    }
155}
156
157impl From<&'static str> for DebugName {
158    fn from(value: &'static str) -> Self {
159        Self::borrowed(value)
160    }
161}