From nobody Sat Jan 31 20:30:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1767877937; cv=none; d=zohomail.com; s=zohoarc; b=JUk7vf71S/k84guhdvY3x97gdgf5LWZtV8kGKnDxObKBHATbHlUl1fTB9Vec2HboBmTpIjphit0fNsce13HraoSWaeAeZgKliTLL6xxqVxbU1FCrGhGpQhuQnRSAUcHsDA8MXytkTyoMFo4yUXxyxRPZFmXhTsaQRCau0ppu8Ic= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1767877937; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=UqCrVvUX1EXb2s0loBz213vZBI1pR55uJOq0h9TgEQM=; b=oLhp3K4/JaIunfGPRhMI/5yvTSHjZeNuMj6uYOrPFhIH/nTiwqrk/b1uujkMABvRgn6ePbALNt4xEPhCxus9OrMXHH07IZuAIdfaIfnFefLdwzoR7XqiYbHSCT+paaMyoQ+tl3RIE93M8uTMNHET5bjTG49s7aBMAD7micELaYw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1767877937827481.2213970717995; Thu, 8 Jan 2026 05:12:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vdpn3-0007XX-C6; Thu, 08 Jan 2026 08:11:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vdpn1-0007We-N0 for qemu-devel@nongnu.org; Thu, 08 Jan 2026 08:10:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vdpmz-0007yR-Rk for qemu-devel@nongnu.org; Thu, 08 Jan 2026 08:10:59 -0500 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-205-q3OO3Vm4OfqoFsZxHZ42Hw-1; Thu, 08 Jan 2026 08:10:56 -0500 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-47d4029340aso30791515e9.3 for ; Thu, 08 Jan 2026 05:10:55 -0800 (PST) Received: from [192.168.10.48] ([151.61.26.160]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d8384646fsm118655955e9.15.2026.01.08.05.10.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jan 2026 05:10:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767877857; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UqCrVvUX1EXb2s0loBz213vZBI1pR55uJOq0h9TgEQM=; b=iqyG4m8lKobk+XdPwf0oqgGBS65sSLIlD4CGxcBxbrD7ToigjF/1r5uX8DP5QHtiMZ9Fei T9VF3lRZBfeYt6PAQmp9X8Rgbrncx0KwN0fVYAN7QvrHTjWoxPOJx7m2GCYuEafJ8RiBL+ nRnd87JNhqF1S4aqmvrOeyEfc+0pVyA= X-MC-Unique: q3OO3Vm4OfqoFsZxHZ42Hw-1 X-Mimecast-MFC-AGG-ID: q3OO3Vm4OfqoFsZxHZ42Hw_1767877855 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1767877854; x=1768482654; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UqCrVvUX1EXb2s0loBz213vZBI1pR55uJOq0h9TgEQM=; b=nAhVPVEviqooGCZabp5RWYFOZaeEF+Sr5+4zFTEhn/Dcey1hWHlxJC74H9icwrmcxL FAliysCfG98PMHIOZL3XmEt5Qm0oMC8FtcSpfNqjZe+qF/ewfPRqELiVQiujKbVRpuHX SYXAkge+gn/haMA2ry+gKnCckJzwfVgI6HEe0OPuDkLtcoGB48WaO8EoA0zonFazrDcO 6gPE9Vf9DGmHIpMESAFZ75jZC95F8WeS2wdG7ftacpxcjVdRCgkoifFhmjEpSJWvTlhH +ndvjM7kYVswHPoHlxPrbcznNBSe/6S0IN9qLyV6Aci6PQbeEiVyolikbzH+/6nsHnOG oxKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767877854; x=1768482654; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=UqCrVvUX1EXb2s0loBz213vZBI1pR55uJOq0h9TgEQM=; b=h0RiWByuhtgZFe1BpEXorvQsWw8N3UhOizmDVf/Z/iUa6CZ1SS8gb8WppLxps7swZ6 NWt3paD1eN1DwDJH3Nrt97xCrHDDpDXrN3x8P6B+K/ovfBODWS4tBlSUaJMzQw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X-Google-Smtp-Source: AGHT+IGw4iQ0MovB5pjL581zvBOb6KCYC+7Qdo6ESflVbfDkVmmWAPAC8XTWbr5jesFGfAiDcesXQg== X-Received: by 2002:a05:600c:4e49:b0:476:4efc:8ed4 with SMTP id 5b1f17b1804b1-47d84b172bcmr73726575e9.11.1767877853571; Thu, 08 Jan 2026 05:10:53 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , qemu-rust@nongnu.org, Zhao Liu Subject: [PATCH v2 03/16] rust/qobject: add Serialize implementation Date: Thu, 8 Jan 2026 14:10:30 +0100 Message-ID: <20260108131043.490084-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260108131043.490084-1-pbonzini@redhat.com> References: <20260108131043.490084-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1767877938204158500 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=C3=A9 Lureau Signed-off-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Zhao Liu Signed-off-by: Paolo Bonzini --- rust/Cargo.lock | 1 + rust/util/Cargo.toml | 1 + rust/util/meson.build | 3 +- rust/util/src/qobject/mod.rs | 4 +- rust/util/src/qobject/serialize.rs | 59 ++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 rust/util/src/qobject/serialize.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 42ae857fe53..b6c9f934140 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -453,6 +453,7 @@ dependencies =3D [ "foreign", "glib-sys", "libc", + "serde", ] =20 [[package]] diff --git a/rust/util/Cargo.toml b/rust/util/Cargo.toml index 85f91436545..554004816eb 100644 --- a/rust/util/Cargo.toml +++ b/rust/util/Cargo.toml @@ -17,6 +17,7 @@ anyhow =3D { workspace =3D true } foreign =3D { workspace =3D true } glib-sys =3D { workspace =3D true } libc =3D { workspace =3D true } +serde =3D { workspace =3D true } common =3D { path =3D "../common" } =20 [lints] diff --git a/rust/util/meson.build b/rust/util/meson.build index 28593286134..166a792bce7 100644 --- a/rust/util/meson.build +++ b/rust/util/meson.build @@ -40,8 +40,9 @@ _util_rs =3D static_library( {'.': _util_bindings_inc_rs, 'qobject': [ 'src/qobject/mod.rs', + 'src/qobject/serialize.rs', ]}), - dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs], + dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs, s= erde_rs], ) =20 util_rs =3D declare_dependency(link_with: [_util_rs], dependencies: [qemuu= til, qom]) diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs index 5744870ad1c..7b9f0516fe1 100644 --- a/rust/util/src/qobject/mod.rs +++ b/rust/util/src/qobject/mod.rs @@ -6,6 +6,8 @@ =20 #![deny(clippy::unwrap_used)] =20 +mod serialize; + use std::{ cell::UnsafeCell, ffi::{c_char, CString}, @@ -244,7 +246,6 @@ fn drop(&mut self) { } } =20 -#[allow(unused)] macro_rules! match_qobject { (@internal ($qobj:expr) =3D> $(() =3D> $unit:expr,)? @@ -327,5 +328,4 @@ macro_rules! match_qobject { $($type $(($val))? =3D> $code,)+) }; } -#[allow(unused_imports)] use match_qobject; diff --git a/rust/util/src/qobject/serialize.rs b/rust/util/src/qobject/ser= ialize.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(&self, serializer: S) -> Result + where + S: ::serde::Serializer, + { + match_qobject! { (self) =3D> + () =3D> serializer.serialize_unit(), + bool(b) =3D> serializer.serialize_bool(b), + i64(i) =3D> serializer.serialize_i64(i), + u64(u) =3D> serializer.serialize_u64(u), + f64(f) =3D> serializer.serialize_f64(f), + CStr(cstr) =3D> cstr.to_str().map_or_else( + |_| Err(ser::Error::custom("invalid UTF-8 in QString")), + |s| serializer.serialize_str(s), + ), + QList(l) =3D> { + let mut node_ptr =3D unsafe { l.head.tqh_first }; + let mut state =3D serializer.serialize_seq(None)?; + while !node_ptr.is_null() { + let node =3D unsafe { &*node_ptr }; + let elem =3D unsafe { ManuallyDrop::new(QObject::from_= raw(addr_of!(*node.value))) }; + state.serialize_element(&*elem)?; + node_ptr =3D unsafe { node.next.tqe_next }; + } + state.end() + }, + QDict(d) =3D> { + let mut state =3D serializer.serialize_map(Some(d.size))?; + let mut e_ptr =3D unsafe { bindings::qdict_first(d) }; + while !e_ptr.is_null() { + let e =3D unsafe { &*e_ptr }; + let key =3D 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 =3D unsafe { ManuallyDrop::new(QObject::from= _raw(addr_of!(*e.value))) }; + state.serialize_value(&*value)?; + e_ptr =3D unsafe { bindings::qdict_next(d, e) }; + } + state.end() + } + } + } +} --=20 2.52.0