From df063ab1ef1f84735d51e1896c4fa221edce5286 Mon Sep 17 00:00:00 2001 From: Pixelstorm Date: Mon, 22 Jan 2024 15:45:17 +0000 Subject: [PATCH] Implement `Debug` for `CommandQueue` (#11444) # Objective Allow users to impl Debug on types containing `CommandQueue`s ## Solution Derive Debug on `CommandQueue` --------- Co-authored-by: Alice Cecile --- .../bevy_ecs/src/system/commands/command_queue.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/commands/command_queue.rs b/crates/bevy_ecs/src/system/commands/command_queue.rs index 5130089cc8..0e372c6038 100644 --- a/crates/bevy_ecs/src/system/commands/command_queue.rs +++ b/crates/bevy_ecs/src/system/commands/command_queue.rs @@ -1,4 +1,4 @@ -use std::mem::MaybeUninit; +use std::{fmt::Debug, mem::MaybeUninit}; use bevy_ptr::{OwningPtr, Unaligned}; use bevy_utils::tracing::warn; @@ -34,6 +34,19 @@ pub struct CommandQueue { bytes: Vec>, } +// CommandQueue needs to implement Debug manually, rather than deriving it, because the derived impl just prints +// [core::mem::maybe_uninit::MaybeUninit, core::mem::maybe_uninit::MaybeUninit, ..] for every byte in the vec, +// which gets extremely verbose very quickly, while also providing no useful information. +// It is not possible to soundly print the values of the contained bytes, as some of them may be padding or uninitialized (#4863) +// So instead, the manual impl just prints the length of vec. +impl Debug for CommandQueue { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CommandQueue") + .field("len_bytes", &self.bytes.len()) + .finish_non_exhaustive() + } +} + // SAFETY: All commands [`Command`] implement [`Send`] unsafe impl Send for CommandQueue {}