From nobody Fri Nov 14 19:42:45 2025 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=1760109123; cv=none; d=zohomail.com; s=zohoarc; b=RrbDn3whq7c/J4VDR4ArREM8WYPUZH0eibwNSlIoly4i0um/FZlcNYPv+S6XMSdZT/DeoKh+UJlY8EvYkx8mdU1UmLvYOSbieiHhDg0jlnkhnfjQIE5gGDa0lCeJCh7uB1td+xA3vFIevlJCEve0K35gSsAOQlv9voSeDXgA4jo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760109123; 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=3FZgvDx1lu9fJSdOxBvB+MMur5Jir4fvIS9bMP6QLM0=; b=WGeVe66MdZxJVw5Z9Z9fcipWf2IPz6uNATwlwgYfeFiZiByZNUN+/P7OxtXD14j6WBQooQZ1+EoJD/H/vNgL+zaS8yW7PxmuN/2GKkcmOUwj5TgQVVWCDyP5ga+V5kO2XH1rG/92VHo+QMy4xvYQCW/zPTb7LxF+/n7QNt+64hA= 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 1760109123456204.78865932608278; Fri, 10 Oct 2025 08:12:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v7Eld-0008So-Ow; Fri, 10 Oct 2025 11:10:49 -0400 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 1v7ElZ-0008Qc-J3 for qemu-devel@nongnu.org; Fri, 10 Oct 2025 11:10:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v7ElJ-00045r-E2 for qemu-devel@nongnu.org; Fri, 10 Oct 2025 11:10:45 -0400 Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-128-mwdiVTONNimuWLFsdlsQig-1; Fri, 10 Oct 2025 11:10:25 -0400 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-b3cd45a823cso257953966b.2 for ; Fri, 10 Oct 2025 08:10:25 -0700 (PDT) Received: from [192.168.10.48] ([151.49.231.162]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b55d8c12ccbsm249043566b.46.2025.10.10.08.10.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Oct 2025 08:10:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760109026; 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=3FZgvDx1lu9fJSdOxBvB+MMur5Jir4fvIS9bMP6QLM0=; b=VA0GWXIMO3zMQi0mSqzgrjJ7+xw/qwtmdUMs05J4MmSC4ow8sDQaBRl1VyjYACYavhOd4H WVTALi+tIIuV2kV1aVQzFc5NEUNLuIFQpz/N8BWQe9l7l8n8NW7A0i/0iZDPCWqKzAnvL7 KYVlmX02oi+57FpwH98mB4rg003VkVw= X-MC-Unique: mwdiVTONNimuWLFsdlsQig-1 X-Mimecast-MFC-AGG-ID: mwdiVTONNimuWLFsdlsQig_1760109024 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760109023; x=1760713823; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3FZgvDx1lu9fJSdOxBvB+MMur5Jir4fvIS9bMP6QLM0=; b=HcX09fmEwMJN7LZIrGjOK80RokECxEv83GwxbY3MwS2C9wzFdvsrOyUxy9M1Y5S7bu fZD+W6sci/cbnWUAfML2NMKY7lp/e++up8xzM/R9KmMI3cWqjEsypcem0sGS8U27ghXB D6vCKqXJzc6w5XdIqc+xBRZpDvyyo11cKlczA0NaFKXQFlXtRXYQDOOHrHa/F6d8KfDQ mn4Qlqj1SyC0HIej3HYJjdsbtwlV+uZL7HUSJWgZFDgLhHEkrZoGTwX/IigIN7LFre5e fB08yNloDv5q0fmOpGj4aaHfET9dMRfR/tTQLvxw2YzwOnUQWGUOWjLcJRoD0lWOM9ap AW8w== X-Gm-Message-State: AOJu0YyBqKuGuKVK1+3xJ377R0qS1zEwFfdMfUh2QFn5RPIgxGdAr9Ht iDPn2D4NHJtLuwqp9BE2SLXgfXlmyt/l4qPX3vvvrLJ742mgI5RtbDQAAZd4fxUQZ7NaddRsxtw hTAxbHST4+MrEU/SF6yAWBc1hLz2iKcsGotY7Uml10veCuTN+/Lsw5dkbQD6h8nxWmeWr2sSPwl Vcx1FiicbX9XAkueQrpSM2Xaz4sbhasNtulyZtCyLA X-Gm-Gg: ASbGncvQtsp3Gb6kRPKT5UFbP2pwQegy7h8W/7nDAL4kJscOlrWf5LBRbrwX0a6wuF7 /FJhOmPnzxlZsvmfQIMqtXSoRjhHX4uthtoJm3w6/gsjkM9kWUZ7x+lU+3nQMRpccIJDMGpU682 NFW8+3gqSWdemvU5U2qxcz6f2X9lqADaExNdIYNxtKIszB/TgJWmIyhu/IuzXR7pY8D25K3roxb STGio0m5Nk0nGmmKQIpxwFGQ+hGKH+rwzBv73GBxzICKzkXCs0qOODU6UTbQBSE2X50M3KSdlQE xIrToJLutkZQkdblTeheX+CRQi7nuOe1S6xpAFLyka7ALUv5iF5UjfOxeWktj86IwZ32aE6BN58 prZqg0U5loBwIIXH+wvdEd9ExLtjLsNxmlNDSuNv1Rsdx X-Received: by 2002:a17:906:6a14:b0:b44:2d0:e05f with SMTP id a640c23a62f3a-b50ac2cf494mr1311064966b.33.1760109023388; Fri, 10 Oct 2025 08:10:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFTnWAf1thJZKPgL7ao/5GeYS/n+TrYEjpV4ceOm1F1ZsP59hS6+z1hjorwGFq0K+CGDu7qXQ== X-Received: by 2002:a17:906:6a14:b0:b44:2d0:e05f with SMTP id a640c23a62f3a-b50ac2cf494mr1311060466b.33.1760109022780; Fri, 10 Oct 2025 08:10:22 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, marcandre.lureau@redhat.com, qemu-rust@nongnu.org Subject: [PATCH 06/19] rust/qobject: add Serialize implementation Date: Fri, 10 Oct 2025 17:09:51 +0200 Message-ID: <20251010151006.791038-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251010151006.791038-1-pbonzini@redhat.com> References: <20251010151006.791038-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 client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.441, 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham 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: 1760109134542158500 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 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 11085133490..7c9f85d5728 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -445,6 +445,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 ce468ea5227..9fafaf76a37 100644 --- a/rust/util/meson.build +++ b/rust/util/meson.build @@ -39,10 +39,11 @@ _util_rs =3D static_library( {'.': _util_bindings_inc_rs, 'qobject': [ 'src/qobject/mod.rs', + 'src/qobject/serialize.rs', ]}), override_options: ['rust_std=3D2021', 'build.rust_std=3D2021'], rust_abi: 'rust', - dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs, q= om, qemuutil], + dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs, s= erde_rs, qom, qemuutil], ) =20 util_rs =3D declare_dependency(link_with: [_util_rs]) diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs index 9c6f168d6e1..0913fabc1ee 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}, @@ -230,7 +232,6 @@ fn drop(&mut self) { } } =20 -#[allow(unused)] macro_rules! match_qobject { (@internal ($qobj:expr) =3D> $(() =3D> $unit:expr,)? @@ -313,5 +314,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.51.0