From: Markus Armbruster <armbru@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
qemu-rust@nongnu.org, "Zhao Liu" <zhao1.liu@intel.com>
Subject: Re: [PATCH v2 03/16] rust/qobject: add Serialize implementation
Date: Tue, 24 Feb 2026 11:29:45 +0100 [thread overview]
Message-ID: <87ecma4ep2.fsf@pond.sub.org> (raw)
In-Reply-To: <20260108131043.490084-4-pbonzini@redhat.com> (Paolo Bonzini's message of "Thu, 8 Jan 2026 14:10:30 +0100")
Paolo Bonzini <pbonzini@redhat.com> writes:
> This allows QObject to be converted to other formats, for example
> JSON via serde_json.
>
> This is not too useful, since QObjects are consumed by
> C code or deserialized into structs, but it can be used for testing
> and it is part of the full implementation of a serde format.
>
> Co-authored-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[...]
> diff --git a/rust/util/src/qobject/serialize.rs b/rust/util/src/qobject/serialize.rs
> new file mode 100644
> index 00000000000..34ec3847c1d
> --- /dev/null
> +++ b/rust/util/src/qobject/serialize.rs
> @@ -0,0 +1,59 @@
> +//! `QObject` serialization
> +//!
> +//! This module implements the [`Serialize`] trait for `QObject`,
> +//! allowing it to be converted to other formats, for example
> +//! JSON.
> +
> +use std::{ffi::CStr, mem::ManuallyDrop, ptr::addr_of};
> +
> +use serde::ser::{self, Serialize, SerializeMap, SerializeSeq};
> +
> +use super::{match_qobject, QObject};
> +use crate::bindings;
> +
> +impl Serialize for QObject {
> + #[inline]
> + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
> + where
> + S: ::serde::Serializer,
> + {
> + match_qobject! { (self) =>
> + () => serializer.serialize_unit(),
> + bool(b) => serializer.serialize_bool(b),
> + i64(i) => serializer.serialize_i64(i),
> + u64(u) => serializer.serialize_u64(u),
> + f64(f) => serializer.serialize_f64(f),
> + CStr(cstr) => cstr.to_str().map_or_else(
> + |_| Err(ser::Error::custom("invalid UTF-8 in QString")),
.to_str() fails when its argument is invalid UTF-8. It returns "an
error with details of where UTF-8 validation failed."[1]
Why are we replacing this error with a custom one? I guess we add the
clue "in QString". We also lose the details of where. Feels like a
questionable trade.
Moreover, I believe this is a programming error: QString is not meant to
wrap arbitrary byte sequences, it's meant to wrap UTF-8 strings.
Programming errors should panic.
Can we use .expect()? It panics "if the value is an Err, with a panic
message including the passed message, and the content of the Err."[2]
If we decide this isn't a programming error (because QString may contain
arbitrary zero-terminated byte sequences[3]): can we combine all the
readily available information like .expect() does?
In review of v2, you wrote
I'd rather not special case this into the only abort (see the
"#![deny(clippy::unwrap_used)]" in patch 4).
Is this an argument for pretending this isn't a programming error? If
yes, I need a bit more help to understand it.
> + |s| serializer.serialize_str(s),
> + ),
> + QList(l) => {
> + let mut node_ptr = unsafe { l.head.tqh_first };
> + let mut state = serializer.serialize_seq(None)?;
> + while !node_ptr.is_null() {
> + let node = unsafe { &*node_ptr };
> + let elem = unsafe { ManuallyDrop::new(QObject::from_raw(addr_of!(*node.value))) };
> + state.serialize_element(&*elem)?;
> + node_ptr = unsafe { node.next.tqe_next };
> + }
> + state.end()
> + },
> + QDict(d) => {
> + let mut state = serializer.serialize_map(Some(d.size))?;
> + let mut e_ptr = unsafe { bindings::qdict_first(d) };
> + while !e_ptr.is_null() {
> + let e = unsafe { &*e_ptr };
> + let key = unsafe { CStr::from_ptr(e.key) };
> + key.to_str().map_or_else(
> + |_| Err(ser::Error::custom("invalid UTF-8 in key")),
> + |k| state.serialize_key(k),
> + )?;
> + let value = unsafe { ManuallyDrop::new(QObject::from_raw(addr_of!(*e.value))) };
> + state.serialize_value(&*value)?;
> + e_ptr = unsafe { bindings::qdict_next(d, e) };
> + }
> + state.end()
> + }
> + }
> + }
> +}
[1] https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.to_str
[2] https://doc.rust-lang.org/std/result/enum.Result.html#method.expect
[3] Feels like a problematic idea to me.
next prev parent reply other threads:[~2026-02-24 10:30 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-08 13:10 [PATCH v2 00/16] rust: QObject and QAPI bindings Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 01/16] rust/qobject: add basic bindings Paolo Bonzini
2026-02-24 10:03 ` Markus Armbruster
2026-02-24 10:35 ` Paolo Bonzini
2026-02-24 13:33 ` Markus Armbruster
2026-02-25 8:05 ` Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 02/16] subprojects: add serde Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 03/16] rust/qobject: add Serialize implementation Paolo Bonzini
2026-02-24 10:29 ` Markus Armbruster [this message]
2026-02-24 10:48 ` Paolo Bonzini
2026-02-24 13:41 ` Markus Armbruster
2026-01-08 13:10 ` [PATCH v2 04/16] rust/qobject: add Serializer (to_qobject) implementation Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 05/16] rust/qobject: add Deserialize implementation Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 06/16] rust/qobject: add Deserializer (from_qobject) implementation Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 07/16] rust/qobject: add from/to JSON bindings for QObject Paolo Bonzini
2026-01-15 13:17 ` Zhao Liu
2026-01-08 13:10 ` [PATCH v2 08/16] rust/qobject: add Display/Debug Paolo Bonzini
2026-01-15 13:19 ` Zhao Liu
2026-01-08 13:10 ` [PATCH v2 09/16] scripts/qapi: add QAPISchemaIfCond.rsgen() Paolo Bonzini
2026-01-19 6:58 ` Zhao Liu
2026-02-25 6:48 ` Markus Armbruster
2026-02-25 7:53 ` Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 10/16] scripts/qapi: add QAPISchemaType.is_predefined Paolo Bonzini
2026-02-25 7:33 ` Markus Armbruster
2026-02-25 8:01 ` Paolo Bonzini
2026-02-25 8:44 ` Markus Armbruster
2026-02-26 14:12 ` Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 11/16] scripts/qapi: pull c_name from camel_to_upper to caller Paolo Bonzini
2026-01-19 7:05 ` Zhao Liu
2026-02-25 8:32 ` Markus Armbruster
2026-01-08 13:10 ` [PATCH v2 12/16] scripts/qapi: generate high-level Rust bindings Paolo Bonzini
2026-02-23 12:36 ` Markus Armbruster
2026-02-23 16:11 ` Paolo Bonzini
2026-02-24 13:46 ` Markus Armbruster
2026-02-25 14:39 ` Markus Armbruster
2026-03-03 10:00 ` Paolo Bonzini
2026-03-03 12:31 ` Markus Armbruster
2026-03-03 15:55 ` Paolo Bonzini
2026-03-04 8:09 ` Markus Armbruster
2026-03-03 9:19 ` Markus Armbruster
2026-03-03 13:17 ` Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 13/16] scripts/rustc_args: add --no-strict-cfg Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 14/16] rust/util: build QAPI types Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 15/16] scripts/qapi: add serde attributes Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 16/16] rust/tests: QAPI integration tests Paolo Bonzini
2026-02-17 8:10 ` [PATCH v2 00/16] rust: QObject and QAPI bindings Paolo Bonzini
2026-02-19 13:39 ` Markus Armbruster
2026-02-19 16:28 ` Paolo Bonzini
2026-02-23 9:53 ` Daniel P. Berrangé
2026-02-23 15:54 ` Paolo Bonzini
2026-02-23 16:24 ` Daniel P. Berrangé
2026-02-23 19:03 ` Paolo Bonzini
2026-02-24 14:06 ` Markus Armbruster
2026-02-24 17:28 ` Paolo Bonzini
2026-02-26 12:42 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ecma4ep2.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox