Skip to content

Commit 78c03e3

Browse files
chescockmockersf
authored andcommitted
Simplify Debug display of DebugName (bevyengine#21870)
# Objective As noted in bevyengine#21856, the `Debug` output of `DebugName` is too verbose. It is supposed to be a thin wrapper around a string, but it renders as a `struct`. ## Solution Manually `impl Debug for DebugName` and write the string directly. ## Showcase The following code ```rust #[derive(Debug)] struct TestStruct { debug_name: DebugName, } let test_struct = TestStruct { debug_name: DebugName::type_name::<TestStruct>(), }; println!("{test_struct:#?}"); ``` Prints the following before this change ``` TestStruct { debug_name: DebugName { name: "crate_name::TestStruct", }, } ``` And the following after it ``` TestStruct { debug_name: "crate_name::TestStruct", } ``` When the `debug` feature is disabled, it prints the following both before and after the change ``` TestStruct { debug_name: DebugName, } ``` --------- Co-authored-by: François Mockers <[email protected]>
1 parent e5c78c0 commit 78c03e3

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

crates/bevy_utils/src/debug_info.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const FEATURE_DISABLED: &str = "Enable the debug feature to see the name";
1414
///
1515
/// * If the `debug` feature is enabled, the actual name will be used
1616
/// * If it is disabled, a string mentioning the disabled feature will be used
17-
#[derive(Clone, Debug, PartialEq, Eq)]
17+
#[derive(Clone, PartialEq, Eq)]
1818
pub struct DebugName {
1919
#[cfg(feature = "debug")]
2020
name: Cow<'static, str>,
@@ -23,12 +23,15 @@ pub struct DebugName {
2323
cfg::alloc! {
2424
impl fmt::Display for DebugName {
2525
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)?;
26+
// Deref to `str`, which will use `FEATURE_DISABLED` if necessary
27+
write!(f, "{}", &**self)
28+
}
29+
}
3030

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

0 commit comments

Comments
 (0)