Repository: bevyengine/bevy
File: crates/bevy_utils/src/debug_info.rs (Rust)

Code before:
/// * If the `debug` feature is enabled, the actual name will be used
/// * If it is disabled, a string mentioning the disabled feature will be used
#[derive(Clone, PartialEq, Eq)]
pub struct DebugName {
    #[cfg(feature = "debug")]
    name: Cow<'static, str>,
}

cfg::alloc! {
    impl fmt::Display for DebugName {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            #[cfg(feature = "debug")]
            f.write_str(self.name.as_ref())?;
            #[cfg(not(feature = "debug"))]
            f.write_str(FEATURE_DISABLED)?;

            Ok(())
        }
    }

    impl fmt::Debug for DebugName {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            #[cfg(feature = "debug")]
            write!(f, "{:?}", self.name.as_ref())?;
            #[cfg(not(feature = "debug"))]
            f.debug_struct("DebugName").finish()?;

            Ok(())
        }
    }
}

impl DebugName {
    /// Create a new `DebugName` from a `&str`
    ///
    /// The value will be ignored if the `debug` feature is not enabled
    #[cfg_attr(
        not(feature = "debug"),
        expect(
            unused_variables,
            reason = "The value will be ignored if the `debug` feature is not enabled"
        )
    )]
    pub const fn borrowed(value: &'static str) -> Self {
        DebugName {
            #[cfg(feature = "debug")]
            name: Cow::Borrowed(value),
        }
    }

    cfg::alloc! {

Review comment (bug):
Maybe tell the user they can enable the `debug` feature to read the actual name?

Code after:
/// * If the `debug` feature is enabled, the actual name will be used
/// * If it is disabled, a string mentioning the disabled feature will be used
#[derive(Clone, PartialEq, Eq)]
pub struct DebugName {
    #[cfg(feature = "debug")]
    name: Cow<'static, str>,
}

cfg::alloc! {
    impl fmt::Display for DebugName {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            // Deref to `str`, which will use `FEATURE_DISABLED` if necessary
            write!(f, "{}", &**self)
        }
    }

    impl fmt::Debug for DebugName {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            // Deref to `str`, which will use `FEATURE_DISABLED` if necessary
            write!(f, "{:?}", &**self)
        }
    }
}

impl DebugName {
    /// Create a new `DebugName` from a `&str`
    ///
    /// The value will be ignored if the `debug` feature is not enabled
    #[cfg_attr(
        not(feature = "debug"),
        expect(
            unused_variables,
            reason = "The value will be ignored if the `debug` feature is not enabled"
        )
    )]
    pub const fn borrowed(value: &'static str) -> Self {
        DebugName {
            #[cfg(feature = "debug")]
            name: Cow::Borrowed(value),
        }
    }

    cfg::alloc! {
        /// Create a new `DebugName` from a `String`
        ///
        /// The value will be ignored if the `debug` feature is not enabled
        #[cfg_attr(
            not(feature = "debug"),
            expect(
                unused_variables,
                reason = "The value will be ignored if the `debug` feature is not enabled"