[PATCH v3 08/19] rust/qobject: add Display/Debug
Paolo Bonzini <[email protected]> Tue, 26 May 2026 19:56:07 +0200
| Newsgroups | org.nongnu.qemu-rust,org.nongnu.qemu-devel |
|---|---|
| Message-ID | <[email protected]> |
From: Marc-André Lureau <[email protected]> Signed-off-by: Marc-André Lureau <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> --- rust/util/src/qobject/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs index d2dde9ba0f1..3e22dc8bb3f 100644 --- a/rust/util/src/qobject/mod.rs +++ b/rust/util/src/qobject/mod.rs @@ -12,6 +12,7 @@ mod serialize; mod serializer; +use core::fmt::{self, Debug, Display}; use std::{ cell::UnsafeCell, ffi::{c_char, CString}, @@ -272,6 +273,33 @@ fn drop(&mut self) { } } +impl Display for QObject { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // replace with a plain serializer? + match_qobject! { (self) => + () => write!(f, "QNull"), + bool(b) => write!(f, "QBool({})", if b { "true" } else { "false" }), + i64(n) => write!(f, "QNumI64({})", n), + u64(n) => write!(f, "QNumU64({})", n), + f64(n) => write!(f, "QNumDouble({})", n), + CStr(s) => write!(f, "QString({})", s.to_str().unwrap_or("bad CStr")), + QList(_) => write!(f, "QList"), + QDict(_) => write!(f, "QDict"), + } + } +} + +impl Debug for QObject { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let val = self.to_string(); + f.debug_struct("QObject") + .field("ptr", &self.0.get()) + .field("refcnt()", &self.refcnt()) + .field("to_string()", &val) + .finish() + } +} + macro_rules! match_qobject { (@internal ($qobj:expr) => $(() => $unit:expr,)? -- 2.54.0