Fix Entity Debug Format (#14539)

# Objective

Fixes #12139

## Solution

See this comment on original issue for my proposal:
https://github.com/bevyengine/bevy/issues/12139#issuecomment-2241915791

This PR is an implementation of this proposal.

I modified the implementation of `fmt::Debug` to instead display
`0v0#12345` to ensure entity index, generation, and raw bits are all
present in the output for debug purposes while still keeping log message
concise.

`fmt::Display` remains as is (`0v0`) to offer an even shorter output.

To me, this is the most non-intrusive fix for this issue.

## Testing

Add `fn entity_debug` test

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
This commit is contained in:
Zeenobit 2024-07-30 21:36:41 -04:00 committed by GitHub
parent 924f1cbc02
commit 023e0e5bde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -142,7 +142,7 @@ type IdCursor = isize;
/// [`Query::get`]: crate::system::Query::get
/// [`World`]: crate::world::World
/// [SemVer]: https://semver.org/
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect_value(Hash, PartialEq))]
#[cfg_attr(
@ -390,6 +390,42 @@ impl<'de> Deserialize<'de> for Entity {
}
}
/// Outputs the full entity identifier, including the index, generation, and the raw bits.
///
/// This takes the format: `{index}v{generation}#{bits}`.
///
/// # Usage
///
/// Prefer to use this format for debugging and logging purposes. Because the output contains
/// the raw bits, it is easy to check it against serialized scene data.
///
/// Example serialized scene data:
/// ```text
/// (
/// ...
/// entities: {
/// 4294967297: ( <--- Raw Bits
/// components: {
/// ...
/// ),
/// ...
/// )
/// ```
impl fmt::Debug for Entity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}v{}#{}",
self.index(),
self.generation(),
self.to_bits()
)
}
}
/// Outputs the short entity identifier, including the index and generation.
///
/// This takes the format: `{index}v{generation}`.
impl fmt::Display for Entity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}v{}", self.index(), self.generation())
@ -1152,6 +1188,15 @@ mod tests {
}
}
#[test]
fn entity_debug() {
let entity = Entity::from_raw(42);
let string = format!("{:?}", entity);
assert!(string.contains("42"));
assert!(string.contains("v1"));
assert!(string.contains(format!("#{}", entity.to_bits()).as_str()));
}
#[test]
fn entity_display() {
let entity = Entity::from_raw(42);