[PATCH v2 08/16] rust/qobject: add Display/Debug

Paolo Bonzini
2026-01-08T13:12:12Z

From: Marc-André Lureau <marcandre.lureau@redhat.com>

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
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 95d41cec4f1..8033b5fafa0 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},
@@ -270,6 +271,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.52.0