* [PATCH preview 00/14] rust: QObject and QAPI bindings
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 01/14] qobject: make refcount atomic Paolo Bonzini
` (13 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
This adds two related parts of the Rust bindings:
- QAPI code generator that creates Rust structs from the JSON
description. The structs are *not* ABI compatible with the
C ones, instead they use native Rust data types.
- QObject bindings and (de)serialization support, which can be used to
convert QObjects to and from QAPI structs.
Unfortunately Rust code is not able to use visitors, other than by
creating an intermediate QObject. This is because of the different
architecture of serde vs. QAPI visitors, and because visitor's
dual-purpose functions, where the same function is used by both input and
output visitors, rely heavily on the structs using the same representation
as the visitor arguments (for example NUL-terminated strings.
The serde format implementation was co-authored by me and Marc-André.
Marc-André did all the bug fixing and integration testing; and there
are a lot more bugs to be fixed / tests to be written, so this is just
a preview.
Paolo
Marc-André Lureau (7):
rust/qobject: add Display/Debug
scripts/qapi: add QAPISchemaIfCond.rsgen()
scripts/qapi: generate high-level Rust bindings
scripts/qapi: strip trailing whitespaces
scripts/rustc_args: add --no-strict-cfg
rust/util: build QAPI types
rust: start qapi tests
Paolo Bonzini (7):
qobject: make refcount atomic
rust: add basic QObject bindings
subprojects: add serde
rust: add Serialize implementation for QObject
rust: add Serializer (to_qobject) implementation for QObject
rust: add Deserialize implementation for QObject
rust: add Deserializer (from_qobject) implementation for QObject
docs/devel/rust.rst | 1 +
meson.build | 4 +-
include/qobject/qobject.h | 5 +-
rust/util/wrapper.h | 8 +
qapi/meson.build | 6 +
rust/Cargo.lock | 2 +
rust/Cargo.toml | 2 +
rust/meson.build | 2 +
rust/tests/meson.build | 10 +-
rust/tests/tests/integration.rs | 2 +
rust/tests/tests/qapi.rs | 35 ++
rust/util/Cargo.toml | 2 +
rust/util/meson.build | 31 +-
rust/util/src/lib.rs | 2 +
rust/util/src/qobject/deserialize.rs | 134 ++++
rust/util/src/qobject/deserializer.rs | 373 +++++++++++
rust/util/src/qobject/error.rs | 58 ++
rust/util/src/qobject/mod.rs | 353 +++++++++++
rust/util/src/qobject/serialize.rs | 59 ++
rust/util/src/qobject/serializer.rs | 585 ++++++++++++++++++
scripts/archive-source.sh | 3 +
scripts/make-release | 2 +-
scripts/qapi/backend.py | 28 +-
scripts/qapi/common.py | 16 +
scripts/qapi/gen.py | 6 +-
scripts/qapi/main.py | 4 +-
scripts/qapi/rs.py | 176 ++++++
scripts/qapi/rs_types.py | 354 +++++++++++
scripts/qapi/schema.py | 4 +
scripts/rust/rustc_args.py | 16 +-
subprojects/.gitignore | 3 +
.../packagefiles/serde-1-rs/meson.build | 36 ++
.../packagefiles/serde-1.0.226-include.patch | 16 +
.../packagefiles/serde_core-1-rs/meson.build | 25 +
.../serde_core-1.0.226-include.patch | 15 +
.../serde_derive-1-rs/meson.build | 35 ++
.../serde_derive-1.0.226-include.patch | 11 +
subprojects/serde-1-rs.wrap | 11 +
subprojects/serde_core-1-rs.wrap | 11 +
subprojects/serde_derive-1-rs.wrap | 11 +
40 files changed, 2438 insertions(+), 19 deletions(-)
create mode 100644 rust/tests/tests/integration.rs
create mode 100644 rust/tests/tests/qapi.rs
create mode 100644 rust/util/src/qobject/deserialize.rs
create mode 100644 rust/util/src/qobject/deserializer.rs
create mode 100644 rust/util/src/qobject/error.rs
create mode 100644 rust/util/src/qobject/mod.rs
create mode 100644 rust/util/src/qobject/serialize.rs
create mode 100644 rust/util/src/qobject/serializer.rs
create mode 100644 scripts/qapi/rs.py
create mode 100644 scripts/qapi/rs_types.py
create mode 100644 subprojects/packagefiles/serde-1-rs/meson.build
create mode 100644 subprojects/packagefiles/serde-1.0.226-include.patch
create mode 100644 subprojects/packagefiles/serde_core-1-rs/meson.build
create mode 100644 subprojects/packagefiles/serde_core-1.0.226-include.patch
create mode 100644 subprojects/packagefiles/serde_derive-1-rs/meson.build
create mode 100644 subprojects/packagefiles/serde_derive-1.0.226-include.patch
create mode 100644 subprojects/serde-1-rs.wrap
create mode 100644 subprojects/serde_core-1-rs.wrap
create mode 100644 subprojects/serde_derive-1-rs.wrap
--
2.51.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH 01/14] qobject: make refcount atomic
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
2025-10-01 8:00 ` [PATCH preview 00/14] rust: QObject and QAPI bindings Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-13 7:51 ` Zhao Liu
2025-10-01 8:00 ` [PATCH 02/14] rust: add basic QObject bindings Paolo Bonzini
` (12 subsequent siblings)
14 siblings, 1 reply; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
The Rust bindings for QObject will only share a complete
object and treat it as immutable from that point on. With
that constraint, it is trivial to make QObjects thread-safe
just by making reference count operations atomic. Do the
same when the C code adds or removes references.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/qobject/qobject.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/include/qobject/qobject.h b/include/qobject/qobject.h
index a6244d0ce00..02f4c6a6eb2 100644
--- a/include/qobject/qobject.h
+++ b/include/qobject/qobject.h
@@ -32,6 +32,7 @@
#ifndef QOBJECT_H
#define QOBJECT_H
+#include "qemu/atomic.h"
#include "qapi/qapi-builtin-types.h"
/* Not for use outside include/qobject/ */
@@ -73,7 +74,7 @@ QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7,
static inline void qobject_ref_impl(QObject *obj)
{
if (obj) {
- obj->base.refcnt++;
+ qatomic_inc(&obj->base.refcnt);
}
}
@@ -95,7 +96,7 @@ void qobject_destroy(QObject *obj);
static inline void qobject_unref_impl(QObject *obj)
{
assert(!obj || obj->base.refcnt);
- if (obj && --obj->base.refcnt == 0) {
+ if (obj && qatomic_fetch_dec(&obj->base.refcnt) == 1) {
qobject_destroy(obj);
}
}
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH 01/14] qobject: make refcount atomic
2025-10-01 8:00 ` [PATCH 01/14] qobject: make refcount atomic Paolo Bonzini
@ 2025-10-13 7:51 ` Zhao Liu
0 siblings, 0 replies; 16+ messages in thread
From: Zhao Liu @ 2025-10-13 7:51 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: qemu-devel, qemu-rust, armbru, marcandre.lureau
On Wed, Oct 01, 2025 at 10:00:38AM +0200, Paolo Bonzini wrote:
> Date: Wed, 1 Oct 2025 10:00:38 +0200
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 01/14] qobject: make refcount atomic
> X-Mailer: git-send-email 2.51.0
>
> The Rust bindings for QObject will only share a complete
> object and treat it as immutable from that point on. With
> that constraint, it is trivial to make QObjects thread-safe
> just by making reference count operations atomic. Do the
> same when the C code adds or removes references.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> include/qobject/qobject.h | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH 02/14] rust: add basic QObject bindings
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
2025-10-01 8:00 ` [PATCH preview 00/14] rust: QObject and QAPI bindings Paolo Bonzini
2025-10-01 8:00 ` [PATCH 01/14] qobject: make refcount atomic Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 03/14] subprojects: add serde Paolo Bonzini
` (11 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
This is only a basic API, intended to be used by the serde traits.
Co-authored-by: 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/wrapper.h | 7 +
rust/util/meson.build | 6 +-
rust/util/src/lib.rs | 2 +
rust/util/src/qobject/mod.rs | 309 +++++++++++++++++++++++++++++++++++
4 files changed, 322 insertions(+), 2 deletions(-)
create mode 100644 rust/util/src/qobject/mod.rs
diff --git a/rust/util/wrapper.h b/rust/util/wrapper.h
index b9ed68a01d8..0907dd59142 100644
--- a/rust/util/wrapper.h
+++ b/rust/util/wrapper.h
@@ -30,3 +30,10 @@ typedef enum memory_order {
#include "qemu/log.h"
#include "qemu/module.h"
#include "qemu/timer.h"
+#include "qobject/qnull.h"
+#include "qobject/qbool.h"
+#include "qobject/qnum.h"
+#include "qobject/qstring.h"
+#include "qobject/qobject.h"
+#include "qobject/qlist.h"
+#include "qobject/qdict.h"
diff --git a/rust/util/meson.build b/rust/util/meson.build
index b0b75e93ff6..38da1ba8cd1 100644
--- a/rust/util/meson.build
+++ b/rust/util/meson.build
@@ -36,8 +36,10 @@ _util_rs = static_library(
'src/module.rs',
'src/timer.rs',
],
- {'.': _util_bindings_inc_rs}
- ),
+ {'.': _util_bindings_inc_rs,
+ 'qobject': [
+ 'src/qobject/mod.rs',
+ ]}),
override_options: ['rust_std=2021', 'build.rust_std=2021'],
rust_abi: 'rust',
dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs, qom, qemuutil],
diff --git a/rust/util/src/lib.rs b/rust/util/src/lib.rs
index 16c89b95174..fe0128103c8 100644
--- a/rust/util/src/lib.rs
+++ b/rust/util/src/lib.rs
@@ -4,6 +4,8 @@
pub mod error;
pub mod log;
pub mod module;
+#[macro_use]
+pub mod qobject;
pub mod timer;
pub use error::{Error, Result};
diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs
new file mode 100644
index 00000000000..f43d87a3b66
--- /dev/null
+++ b/rust/util/src/qobject/mod.rs
@@ -0,0 +1,309 @@
+//! `QObject` bindings
+//!
+//! This module implements bindings for QEMU's `QObject` data structure.
+//! The bindings integrate with `serde`, which take the role of visitors
+//! in Rust code.
+
+#![deny(clippy::unwrap_used)]
+
+use std::{
+ cell::UnsafeCell,
+ ffi::{c_char, CString},
+ mem::ManuallyDrop,
+ ptr::{addr_of, addr_of_mut},
+ sync::atomic::{AtomicUsize, Ordering},
+};
+
+use common::assert_field_type;
+
+use crate::bindings;
+
+/// A wrapper for a C `QObject`.
+///
+/// Because `QObject` is not thread-safe, the safety of these bindings
+/// right now hinges on treating them as immutable. It is part of the
+/// contract with the `QObject` constructors that the Rust struct is
+/// only built after the contents are stable.
+///
+/// Only a bare bones API is public; production and consumption of `QObject`
+/// generally goes through `serde`.
+pub struct QObject(&'static UnsafeCell<bindings::QObject>);
+
+// SAFETY: the QObject API are not thread-safe other than reference counting;
+// but the Rust struct is only created once the contents are stable, and
+// therefore it obeys the aliased XOR mutable invariant.
+unsafe impl Send for QObject {}
+unsafe impl Sync for QObject {}
+
+impl QObject {
+ /// Construct a [`QObject`] from a C `QObjectBase` pointer.
+ /// The caller cedes its reference to the returned struct.
+ ///
+ /// # Safety
+ ///
+ /// The `QObjectBase` must not be changed from C code while
+ /// the Rust `QObject` lives
+ const unsafe fn from_base(p: *const bindings::QObjectBase_) -> Self {
+ QObject(unsafe { &*p.cast() })
+ }
+
+ /// Construct a [`QObject`] from a C `QObject` pointer.
+ /// The caller cedes its reference to the returned struct.
+ ///
+ /// # Safety
+ ///
+ /// The `QObject` must not be changed from C code while
+ /// the Rust `QObject` lives
+ pub const unsafe fn from_raw(p: *const bindings::QObject) -> Self {
+ QObject(unsafe { &*p.cast() })
+ }
+
+ /// Obtain a raw C pointer from a reference. `self` is consumed
+ /// and the C `QObject` pointer is leaked.
+ pub fn into_raw(self) -> *mut bindings::QObject {
+ let src = ManuallyDrop::new(self);
+ src.0.get()
+ }
+
+ /// Construct a [`QObject`] from a C `QObject` pointer.
+ /// The caller *does not* cede its reference to the returned struct.
+ ///
+ /// # Safety
+ ///
+ /// The `QObjectBase` must not be changed from C code while
+ /// the Rust `QObject` lives
+ unsafe fn cloned_from_base(p: *const bindings::QObjectBase_) -> Self {
+ let orig = unsafe { ManuallyDrop::new(QObject::from_base(p)) };
+ (*orig).clone()
+ }
+
+ /// Construct a [`QObject`] from a C `QObject` pointer.
+ /// The caller *does not* cede its reference to the returned struct.
+ ///
+ /// # Safety
+ ///
+ /// The `QObject` must not be changed from C code while
+ /// the Rust `QObject` lives
+ pub unsafe fn cloned_from_raw(p: *const bindings::QObject) -> Self {
+ let orig = unsafe { ManuallyDrop::new(QObject::from_raw(p)) };
+ (*orig).clone()
+ }
+
+ fn refcnt(&self) -> &AtomicUsize {
+ assert_field_type!(bindings::QObjectBase_, refcnt, usize);
+ let qobj = self.0.get();
+ unsafe { AtomicUsize::from_ptr(addr_of_mut!((*qobj).base.refcnt)) }
+ }
+}
+
+impl From<()> for QObject {
+ fn from(_null: ()) -> Self {
+ unsafe { QObject::cloned_from_base(addr_of!(bindings::qnull_.base)) }
+ }
+}
+
+impl<T> From<Option<T>> for QObject
+where
+ QObject: From<T>,
+{
+ fn from(o: Option<T>) -> Self {
+ o.map_or_else(|| ().into(), Into::into)
+ }
+}
+
+impl From<bool> for QObject {
+ fn from(b: bool) -> Self {
+ let qobj = unsafe { &*bindings::qbool_from_bool(b) };
+ unsafe { QObject::from_base(addr_of!(qobj.base)) }
+ }
+}
+
+macro_rules! from_int {
+ ($t:ty) => {
+ impl From<$t> for QObject {
+ fn from(n: $t) -> Self {
+ let qobj = unsafe { &*bindings::qnum_from_int(n.into()) };
+ unsafe { QObject::from_base(addr_of!(qobj.base)) }
+ }
+ }
+ };
+}
+
+from_int!(i8);
+from_int!(i16);
+from_int!(i32);
+from_int!(i64);
+
+macro_rules! from_uint {
+ ($t:ty) => {
+ impl From<$t> for QObject {
+ fn from(n: $t) -> Self {
+ let qobj = unsafe { &*bindings::qnum_from_uint(n.into()) };
+ unsafe { QObject::from_base(addr_of!(qobj.base)) }
+ }
+ }
+ };
+}
+
+from_uint!(u8);
+from_uint!(u16);
+from_uint!(u32);
+from_uint!(u64);
+
+macro_rules! from_double {
+ ($t:ty) => {
+ impl From<$t> for QObject {
+ fn from(n: $t) -> Self {
+ let qobj = unsafe { &*bindings::qnum_from_double(n.into()) };
+ unsafe { QObject::from_base(addr_of!(qobj.base)) }
+ }
+ }
+ };
+}
+
+from_double!(f32);
+from_double!(f64);
+
+impl From<CString> for QObject {
+ fn from(s: CString) -> Self {
+ let qobj = unsafe { &*bindings::qstring_from_str(s.as_ptr()) };
+ unsafe { QObject::from_base(addr_of!(qobj.base)) }
+ }
+}
+
+impl<A> FromIterator<A> for QObject
+where
+ Self: From<A>,
+{
+ fn from_iter<I: IntoIterator<Item = A>>(it: I) -> Self {
+ let qlist = unsafe { &mut *bindings::qlist_new() };
+ for elem in it {
+ let elem: QObject = elem.into();
+ let elem: *mut bindings::QObject = elem.0.get();
+ unsafe {
+ bindings::qlist_append_obj(qlist, elem);
+ }
+ }
+ unsafe { QObject::from_base(addr_of!(qlist.base)) }
+ }
+}
+
+impl<A> FromIterator<(CString, A)> for QObject
+where
+ Self: From<A>,
+{
+ fn from_iter<I: IntoIterator<Item = (CString, A)>>(it: I) -> Self {
+ let qdict = unsafe { &mut *bindings::qdict_new() };
+ for (key, val) in it {
+ let val: QObject = val.into();
+ let val = val.into_raw();
+ unsafe {
+ bindings::qdict_put_obj(qdict, key.as_ptr().cast::<c_char>(), val);
+ }
+ }
+ unsafe { QObject::from_base(addr_of!(qdict.base)) }
+ }
+}
+
+impl Clone for QObject {
+ fn clone(&self) -> Self {
+ self.refcnt().fetch_add(1, Ordering::Acquire);
+ QObject(self.0)
+ }
+}
+
+impl Drop for QObject {
+ fn drop(&mut self) {
+ if self.refcnt().fetch_sub(1, Ordering::Release) == 1 {
+ unsafe {
+ bindings::qobject_destroy(self.0.get());
+ }
+ }
+ }
+}
+
+#[allow(unused)]
+macro_rules! match_qobject {
+ (@internal ($qobj:expr) =>
+ $(() => $unit:expr,)?
+ $(bool($boolvar:tt) => $bool:expr,)?
+ $(i64($i64var:tt) => $i64:expr,)?
+ $(u64($u64var:tt) => $u64:expr,)?
+ $(f64($f64var:tt) => $f64:expr,)?
+ $(CStr($cstrvar:tt) => $cstr:expr,)?
+ $(QList($qlistvar:tt) => $qlist:expr,)?
+ $(QDict($qdictvar:tt) => $qdict:expr,)?
+ $(_ => $other:expr,)?
+ ) => {
+ loop {
+ let qobj_ = $qobj.0.get();
+ match unsafe { &* qobj_ }.base.type_ {
+ $($crate::bindings::QTYPE_QNULL => break $unit,)?
+ $($crate::bindings::QTYPE_QBOOL => break {
+ let qbool__: *mut $crate::bindings::QBool = qobj_.cast();
+ let $boolvar = unsafe { (&*qbool__).value };
+ $bool
+ },)?
+ $crate::bindings::QTYPE_QNUM => {
+ let qnum__: *mut $crate::bindings::QNum = qobj_.cast();
+ let qnum__ = unsafe { &*qnum__ };
+ match qnum__.kind {
+ $crate::bindings::QNUM_I64 |
+ $crate::bindings::QNUM_U64 |
+ $crate::bindings::QNUM_DOUBLE => {}
+ _ => {
+ panic!("unreachable");
+ }
+ }
+
+ match qnum__.kind {
+ $($crate::bindings::QNUM_I64 => break {
+ let $i64var = unsafe { qnum__.u.i64_ };
+ $i64
+ },)?
+ $($crate::bindings::QNUM_U64 => break {
+ let $u64var = unsafe { qnum__.u.u64_ };
+ $u64
+ },)?
+ $($crate::bindings::QNUM_DOUBLE => break {
+ let $f64var = unsafe { qnum__.u.dbl };
+ $f64
+ },)?
+ _ => {}
+ }
+ },
+ $($crate::bindings::QTYPE_QSTRING => break {
+ let qstring__: *mut $crate::bindings::QString = qobj_.cast();
+ let $cstrvar = unsafe { ::core::ffi::CStr::from_ptr((&*qstring__).string) };
+ $cstr
+ },)?
+ $($crate::bindings::QTYPE_QLIST => break {
+ let qlist__: *mut $crate::bindings::QList = qobj_.cast();
+ let $qlistvar = unsafe { &*qlist__ };
+ $qlist
+ },)?
+ $($crate::bindings::QTYPE_QDICT => break {
+ let qdict__: *mut $crate::bindings::QDict = qobj_.cast();
+ let $qdictvar = unsafe { &*qdict__ };
+ $qdict
+ },)?
+ _ => ()
+ };
+ $(break $other;)?
+ #[allow(unreachable_code)]
+ {
+ panic!("unreachable");
+ }
+ }
+ };
+
+ // first cleanup the syntax a bit, checking that there's at least
+ // one pattern and always adding a trailing comma
+ (($qobj:expr) =>
+ $($type:tt$(($val:tt))? => $code:expr ),+ $(,)?) => {
+ match_qobject!(@internal ($qobj) =>
+ $($type $(($val))? => $code,)+)
+ };
+}
+#[allow(unused_imports)]
+use match_qobject;
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 03/14] subprojects: add serde
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (2 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 02/14] rust: add basic QObject bindings Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 04/14] rust: add Serialize implementation for QObject Paolo Bonzini
` (10 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/Cargo.toml | 2 ++
rust/meson.build | 2 ++
scripts/archive-source.sh | 3 ++
scripts/make-release | 2 +-
subprojects/.gitignore | 3 ++
.../packagefiles/serde-1-rs/meson.build | 36 +++++++++++++++++++
.../packagefiles/serde-1.0.226-include.patch | 16 +++++++++
.../packagefiles/serde_core-1-rs/meson.build | 25 +++++++++++++
.../serde_core-1.0.226-include.patch | 15 ++++++++
.../serde_derive-1-rs/meson.build | 35 ++++++++++++++++++
.../serde_derive-1.0.226-include.patch | 11 ++++++
subprojects/serde-1-rs.wrap | 11 ++++++
subprojects/serde_core-1-rs.wrap | 11 ++++++
subprojects/serde_derive-1-rs.wrap | 11 ++++++
14 files changed, 182 insertions(+), 1 deletion(-)
create mode 100644 subprojects/packagefiles/serde-1-rs/meson.build
create mode 100644 subprojects/packagefiles/serde-1.0.226-include.patch
create mode 100644 subprojects/packagefiles/serde_core-1-rs/meson.build
create mode 100644 subprojects/packagefiles/serde_core-1.0.226-include.patch
create mode 100644 subprojects/packagefiles/serde_derive-1-rs/meson.build
create mode 100644 subprojects/packagefiles/serde_derive-1.0.226-include.patch
create mode 100644 subprojects/serde-1-rs.wrap
create mode 100644 subprojects/serde_core-1-rs.wrap
create mode 100644 subprojects/serde_derive-1-rs.wrap
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 783e626802c..a512fb142e2 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -30,6 +30,8 @@ anyhow = "~1.0"
foreign = "~0.3.1"
libc = "0.2.162"
glib-sys = { version = "0.21.2", features = ["v2_66"] }
+serde = "1.0.226"
+serde_derive = "1.0.226"
[workspace.lints.rust]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(MESON)'] }
diff --git a/rust/meson.build b/rust/meson.build
index 76e10699b37..fd2ca40ef90 100644
--- a/rust/meson.build
+++ b/rust/meson.build
@@ -4,6 +4,7 @@ subproject('bilge-impl-0.2-rs', required: true)
subproject('foreign-0.3-rs', required: true)
subproject('glib-sys-0.21-rs', required: true)
subproject('libc-0.2-rs', required: true)
+subproject('serde-1-rs', required: true)
anyhow_rs = dependency('anyhow-1-rs')
bilge_rs = dependency('bilge-0.2-rs')
@@ -11,6 +12,7 @@ bilge_impl_rs = dependency('bilge-impl-0.2-rs')
foreign_rs = dependency('foreign-0.3-rs')
glib_sys_rs = dependency('glib-sys-0.21-rs')
libc_rs = dependency('libc-0.2-rs')
+serde_rs = dependency('serde-1-rs')
subproject('proc-macro2-1-rs', required: true)
subproject('quote-1-rs', required: true)
diff --git a/scripts/archive-source.sh b/scripts/archive-source.sh
index 8f97b19a088..3ed0429d806 100755
--- a/scripts/archive-source.sh
+++ b/scripts/archive-source.sh
@@ -45,6 +45,9 @@ subprojects=(
proc-macro-error-attr-1-rs
proc-macro2-1-rs
quote-1-rs
+ serde-1-rs
+ serde_core-1-rs
+ serde_derive-1-rs
syn-2-rs
unicode-ident-1-rs
)
diff --git a/scripts/make-release b/scripts/make-release
index bc1b43caa25..eb5808b83ec 100755
--- a/scripts/make-release
+++ b/scripts/make-release
@@ -44,7 +44,7 @@ SUBPROJECTS="libvfio-user keycodemapdb berkeley-softfloat-3
bilge-impl-0.2-rs either-1-rs foreign-0.3-rs itertools-0.11-rs
libc-0.2-rs proc-macro2-1-rs
proc-macro-error-1-rs proc-macro-error-attr-1-rs quote-1-rs
- syn-2-rs unicode-ident-1-rs"
+ serde-1-rs serde_core-1-rs serde_derive-1-rs syn-2-rs unicode-ident-1-rs"
src="$1"
version="$2"
diff --git a/subprojects/.gitignore b/subprojects/.gitignore
index c00c8478372..697d1ef3bdb 100644
--- a/subprojects/.gitignore
+++ b/subprojects/.gitignore
@@ -20,6 +20,9 @@
/proc-macro-error-attr-*
/proc-macro*
/quote-*
+/serde-*
+/serde_core-*
+/serde_derive-*
/syn-*
/unicode-ident-*
diff --git a/subprojects/packagefiles/serde-1-rs/meson.build b/subprojects/packagefiles/serde-1-rs/meson.build
new file mode 100644
index 00000000000..6cb2b59a147
--- /dev/null
+++ b/subprojects/packagefiles/serde-1-rs/meson.build
@@ -0,0 +1,36 @@
+project('serde-1-rs', 'rust',
+ meson_version: '>=1.5.0',
+ version: '1.0.226',
+ license: 'MIT OR Apache-2.0',
+ default_options: [])
+
+subproject('serde_core-1-rs', required: true)
+subproject('serde_derive-1-rs', required: true)
+
+serde_core_dep = dependency('serde_core-1-rs')
+serde_derive_dep = dependency('serde_derive-1-rs')
+
+_serde_rs = static_library(
+ 'serde',
+ files('src/lib.rs'),
+ gnu_symbol_visibility: 'hidden',
+ override_options: ['rust_std=2021', 'build.rust_std=2021'],
+ rust_abi: 'rust',
+ rust_args: [
+ '--cap-lints', 'allow',
+ '--cfg', 'feature="alloc"',
+ '--cfg', 'feature="std"',
+ '--cfg', 'feature="derive"',
+ ],
+ dependencies: [
+ serde_core_dep,
+ serde_derive_dep,
+ ]
+)
+
+serde_dep = declare_dependency(
+ link_with: _serde_rs,
+ dependencies: serde_derive_dep,
+)
+
+meson.override_dependency('serde-1-rs', serde_dep, native: true)
diff --git a/subprojects/packagefiles/serde-1.0.226-include.patch b/subprojects/packagefiles/serde-1.0.226-include.patch
new file mode 100644
index 00000000000..92878136878
--- /dev/null
+++ b/subprojects/packagefiles/serde-1.0.226-include.patch
@@ -0,0 +1,16 @@
+--- a/src/lib.rs 2025-09-23 13:41:09.327582205 +0200
++++ b/src/lib.rs 2025-09-23 13:41:23.043271856 +0200
+@@ -241,7 +241,12 @@
+ #[doc(hidden)]
+ mod private;
+
+-include!(concat!(env!("OUT_DIR"), "/private.rs"));
++#[doc(hidden)]
++pub mod __private_MESON {
++ #[doc(hidden)]
++ pub use crate::private::*;
++}
++use serde_core::__private_MESON as serde_core_private;
+
+ // Re-export #[derive(Serialize, Deserialize)].
+ //
diff --git a/subprojects/packagefiles/serde_core-1-rs/meson.build b/subprojects/packagefiles/serde_core-1-rs/meson.build
new file mode 100644
index 00000000000..e917d9337f6
--- /dev/null
+++ b/subprojects/packagefiles/serde_core-1-rs/meson.build
@@ -0,0 +1,25 @@
+project('serde_core-1-rs', 'rust',
+ meson_version: '>=1.5.0',
+ version: '1.0.226',
+ license: 'MIT OR Apache-2.0',
+ default_options: [])
+
+_serde_core_rs = static_library(
+ 'serde_core',
+ files('src/lib.rs'),
+ gnu_symbol_visibility: 'hidden',
+ override_options: ['rust_std=2021', 'build.rust_std=2021'],
+ rust_abi: 'rust',
+ rust_args: [
+ '--cap-lints', 'allow',
+ '--cfg', 'feature="alloc"',
+ '--cfg', 'feature="result"',
+ '--cfg', 'feature="std"',
+ ],
+)
+
+serde_core_dep = declare_dependency(
+ link_with: _serde_core_rs,
+)
+
+meson.override_dependency('serde_core-1-rs', serde_core_dep, native: true)
diff --git a/subprojects/packagefiles/serde_core-1.0.226-include.patch b/subprojects/packagefiles/serde_core-1.0.226-include.patch
new file mode 100644
index 00000000000..d1321dfe272
--- /dev/null
+++ b/subprojects/packagefiles/serde_core-1.0.226-include.patch
@@ -0,0 +1,15 @@
+--- a/src/lib.rs 2025-09-23 13:32:40.872421170 +0200
++++ b/src/lib.rs 2025-09-23 13:32:52.181098856 +0200
+@@ -263,7 +263,11 @@
+ pub use core::result::Result;
+ }
+
+-include!(concat!(env!("OUT_DIR"), "/private.rs"));
++#[doc(hidden)]
++pub mod __private_MESON {
++ #[doc(hidden)]
++ pub use crate::private::*;
++}
+
+ #[cfg(all(not(feature = "std"), no_core_error))]
+ mod std_error;
diff --git a/subprojects/packagefiles/serde_derive-1-rs/meson.build b/subprojects/packagefiles/serde_derive-1-rs/meson.build
new file mode 100644
index 00000000000..6c1001a844a
--- /dev/null
+++ b/subprojects/packagefiles/serde_derive-1-rs/meson.build
@@ -0,0 +1,35 @@
+project('serde_derive-1-rs', 'rust',
+ meson_version: '>=1.5.0',
+ version: '1.0.226',
+ license: 'MIT OR Apache-2.0',
+ default_options: [])
+
+subproject('quote-1-rs', required: true)
+subproject('syn-2-rs', required: true)
+subproject('proc-macro2-1-rs', required: true)
+
+quote_dep = dependency('quote-1-rs', native: true)
+syn_dep = dependency('syn-2-rs', native: true)
+proc_macro2_dep = dependency('proc-macro2-1-rs', native: true)
+
+rust = import('rust')
+
+_serde_derive_rs = rust.proc_macro(
+ 'serde_derive',
+ files('src/lib.rs'),
+ override_options: ['rust_std=2021', 'build.rust_std=2021'],
+ rust_args: [
+ '--cap-lints', 'allow',
+ ],
+ dependencies: [
+ quote_dep,
+ syn_dep,
+ proc_macro2_dep,
+ ],
+)
+
+serde_derive_dep = declare_dependency(
+ link_with: _serde_derive_rs,
+)
+
+meson.override_dependency('serde_derive-1-rs', serde_derive_dep)
diff --git a/subprojects/packagefiles/serde_derive-1.0.226-include.patch b/subprojects/packagefiles/serde_derive-1.0.226-include.patch
new file mode 100644
index 00000000000..81d65564d29
--- /dev/null
+++ b/subprojects/packagefiles/serde_derive-1.0.226-include.patch
@@ -0,0 +1,11 @@
+--- a/src/lib.rs 2025-09-23 13:51:51.540191923 +0200
++++ b/src/lib.rs 2025-09-23 13:52:07.690060195 +0200
+@@ -98,7 +98,7 @@
+ impl private {
+ fn ident(&self) -> Ident {
+ Ident::new(
+- concat!("__private", env!("CARGO_PKG_VERSION_PATCH")),
++ "__private_MESON",
+ Span::call_site(),
+ )
+ }
diff --git a/subprojects/serde-1-rs.wrap b/subprojects/serde-1-rs.wrap
new file mode 100644
index 00000000000..56746dd0f43
--- /dev/null
+++ b/subprojects/serde-1-rs.wrap
@@ -0,0 +1,11 @@
+[wrap-file]
+directory = serde-1.0.226
+source_url = https://crates.io/api/v1/crates/serde/1.0.226/download
+source_filename = serde-1.0.226.0.tar.gz
+source_hash = 0dca6411025b24b60bfa7ec1fe1f8e710ac09782dca409ee8237ba74b51295fd
+#method = cargo
+diff_files = serde-1.0.226-include.patch
+patch_directory = serde-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v1
diff --git a/subprojects/serde_core-1-rs.wrap b/subprojects/serde_core-1-rs.wrap
new file mode 100644
index 00000000000..3692e754935
--- /dev/null
+++ b/subprojects/serde_core-1-rs.wrap
@@ -0,0 +1,11 @@
+[wrap-file]
+directory = serde_core-1.0.226
+source_url = https://crates.io/api/v1/crates/serde_core/1.0.226/download
+source_filename = serde_core-1.0.226.0.tar.gz
+source_hash = ba2ba63999edb9dac981fb34b3e5c0d111a69b0924e253ed29d83f7c99e966a4
+#method = cargo
+diff_files = serde_core-1.0.226-include.patch
+patch_directory = serde_core-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v1
diff --git a/subprojects/serde_derive-1-rs.wrap b/subprojects/serde_derive-1-rs.wrap
new file mode 100644
index 00000000000..00c92dc79cf
--- /dev/null
+++ b/subprojects/serde_derive-1-rs.wrap
@@ -0,0 +1,11 @@
+[wrap-file]
+directory = serde_derive-1.0.226
+source_url = https://crates.io/api/v1/crates/serde_derive/1.0.226/download
+source_filename = serde_derive-1.0.226.0.tar.gz
+source_hash = 8db53ae22f34573731bafa1db20f04027b2d25e02d8205921b569171699cdb33
+#method = cargo
+diff_files = serde_derive-1.0.226-include.patch
+patch_directory = serde_derive-1-rs
+
+# bump this version number on every change to meson.build or the patches:
+# v1
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 04/14] rust: add Serialize implementation for QObject
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (3 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 03/14] subprojects: add serde Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 05/14] rust: add Serializer (to_qobject) " Paolo Bonzini
` (9 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
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>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
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 0c1df625df1..c1075e11d6c 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -446,6 +446,7 @@ dependencies = [
"foreign",
"glib-sys",
"libc",
+ "serde",
]
[[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 = { workspace = true }
foreign = { workspace = true }
glib-sys = { workspace = true }
libc = { workspace = true }
+serde = { workspace = true }
common = { path = "../common" }
[lints]
diff --git a/rust/util/meson.build b/rust/util/meson.build
index 38da1ba8cd1..5b99d38c903 100644
--- a/rust/util/meson.build
+++ b/rust/util/meson.build
@@ -39,10 +39,11 @@ _util_rs = static_library(
{'.': _util_bindings_inc_rs,
'qobject': [
'src/qobject/mod.rs',
+ 'src/qobject/serialize.rs',
]}),
override_options: ['rust_std=2021', 'build.rust_std=2021'],
rust_abi: 'rust',
- dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs, qom, qemuutil],
+ dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs, serde_rs, qom, qemuutil],
)
util_rs = declare_dependency(link_with: [_util_rs], dependencies: [qemuutil, qom])
diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs
index f43d87a3b66..2e3cb247b27 100644
--- a/rust/util/src/qobject/mod.rs
+++ b/rust/util/src/qobject/mod.rs
@@ -6,6 +6,8 @@
#![deny(clippy::unwrap_used)]
+mod serialize;
+
use std::{
cell::UnsafeCell,
ffi::{c_char, CString},
@@ -222,7 +224,6 @@ fn drop(&mut self) {
}
}
-#[allow(unused)]
macro_rules! match_qobject {
(@internal ($qobj:expr) =>
$(() => $unit:expr,)?
@@ -305,5 +306,4 @@ macro_rules! match_qobject {
$($type $(($val))? => $code,)+)
};
}
-#[allow(unused_imports)]
use match_qobject;
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")),
+ |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()
+ }
+ }
+ }
+}
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 05/14] rust: add Serializer (to_qobject) implementation for QObject
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (4 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 04/14] rust: add Serialize implementation for QObject Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 06/14] rust: add Deserialize " Paolo Bonzini
` (8 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
This allows creating QObject from any serializable data structure.
Co-authored-by: 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/meson.build | 2 +
rust/util/src/qobject/error.rs | 52 +++
rust/util/src/qobject/mod.rs | 4 +
rust/util/src/qobject/serializer.rs | 585 ++++++++++++++++++++++++++++
4 files changed, 643 insertions(+)
create mode 100644 rust/util/src/qobject/error.rs
create mode 100644 rust/util/src/qobject/serializer.rs
diff --git a/rust/util/meson.build b/rust/util/meson.build
index 5b99d38c903..fb152766003 100644
--- a/rust/util/meson.build
+++ b/rust/util/meson.build
@@ -39,6 +39,8 @@ _util_rs = static_library(
{'.': _util_bindings_inc_rs,
'qobject': [
'src/qobject/mod.rs',
+ 'src/qobject/error.rs',
+ 'src/qobject/serializer.rs',
'src/qobject/serialize.rs',
]}),
override_options: ['rust_std=2021', 'build.rust_std=2021'],
diff --git a/rust/util/src/qobject/error.rs b/rust/util/src/qobject/error.rs
new file mode 100644
index 00000000000..5212e65c4f7
--- /dev/null
+++ b/rust/util/src/qobject/error.rs
@@ -0,0 +1,52 @@
+//! Error data type for `QObject`'s `serde` integration
+
+use std::{
+ ffi::NulError,
+ fmt::{self, Display},
+ str::Utf8Error,
+};
+
+use serde::ser;
+
+#[derive(Debug)]
+pub enum Error {
+ Custom(String),
+ KeyMustBeAString,
+ InvalidUtf8,
+ NulEncountered,
+ NumberOutOfRange,
+}
+
+impl ser::Error for Error {
+ fn custom<T: Display>(msg: T) -> Self {
+ Error::Custom(msg.to_string())
+ }
+}
+
+impl From<NulError> for Error {
+ fn from(_: NulError) -> Self {
+ Error::NulEncountered
+ }
+}
+
+impl From<Utf8Error> for Error {
+ fn from(_: Utf8Error) -> Self {
+ Error::InvalidUtf8
+ }
+}
+
+impl Display for Error {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Error::Custom(msg) => formatter.write_str(msg),
+ Error::KeyMustBeAString => formatter.write_str("key must be a string"),
+ Error::InvalidUtf8 => formatter.write_str("invalid UTF-8 in string"),
+ Error::NulEncountered => formatter.write_str("NUL character in string"),
+ Error::NumberOutOfRange => formatter.write_str("number out of range"),
+ }
+ }
+}
+
+impl std::error::Error for Error {}
+
+pub type Result<T> = std::result::Result<T, Error>;
diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs
index 2e3cb247b27..cd034185748 100644
--- a/rust/util/src/qobject/mod.rs
+++ b/rust/util/src/qobject/mod.rs
@@ -6,7 +6,9 @@
#![deny(clippy::unwrap_used)]
+mod error;
mod serialize;
+mod serializer;
use std::{
cell::UnsafeCell,
@@ -17,6 +19,8 @@
};
use common::assert_field_type;
+pub use error::{Error, Result};
+pub use serializer::to_qobject;
use crate::bindings;
diff --git a/rust/util/src/qobject/serializer.rs b/rust/util/src/qobject/serializer.rs
new file mode 100644
index 00000000000..59200683f5d
--- /dev/null
+++ b/rust/util/src/qobject/serializer.rs
@@ -0,0 +1,585 @@
+//! `QObject` serializer
+//!
+//! This module implements a [`Serializer`](serde::ser::Serializer) that
+//! produces `QObject`s, allowing them to be created from serializable data
+//! structures (such as primitive data types, or structs that implement
+//! `Serialize`).
+
+use std::ffi::CString;
+
+use serde::ser::{Impossible, Serialize};
+
+use super::{
+ error::{Error, Result},
+ QObject,
+};
+
+pub struct SerializeTupleVariant {
+ name: CString,
+ vec: Vec<QObject>,
+}
+
+impl serde::ser::SerializeTupleVariant for SerializeTupleVariant {
+ type Ok = QObject;
+ type Error = Error;
+
+ fn serialize_field<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ self.vec.push(to_qobject(value)?);
+ Ok(())
+ }
+
+ fn end(self) -> Result<QObject> {
+ let SerializeTupleVariant { name, vec, .. } = self;
+
+ // TODO: insert elements one at a time
+ let list = QObject::from_iter(vec);
+
+ // serde by default represents enums as a single-entry object, with
+ // the variant stored in the key ("external tagging"). Internal tagging
+ // is implemented by the struct that requests it, not by the serializer.
+ let map = [(name, list)];
+ Ok(QObject::from_iter(map))
+ }
+}
+
+pub struct SerializeStructVariant {
+ name: CString,
+ vec: Vec<(CString, QObject)>,
+}
+
+impl serde::ser::SerializeStructVariant for SerializeStructVariant {
+ type Ok = QObject;
+ type Error = Error;
+
+ fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ self.vec.push((CString::new(key)?, to_qobject(value)?));
+ Ok(())
+ }
+
+ fn end(self) -> Result<QObject> {
+ // TODO: insert keys one at a time
+ let SerializeStructVariant { name, vec, .. } = self;
+ let list = QObject::from_iter(vec);
+
+ // serde by default represents enums as a single-entry object, with
+ // the variant stored in the key ("external tagging"). Internal tagging
+ // is implemented by the struct that requests it, not by the serializer.
+ let map = [(name, list)];
+ Ok(QObject::from_iter(map))
+ }
+}
+
+pub struct SerializeVec {
+ vec: Vec<QObject>,
+}
+
+impl serde::ser::SerializeSeq for SerializeVec {
+ type Ok = QObject;
+ type Error = Error;
+
+ fn serialize_element<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ self.vec.push(to_qobject(value)?);
+ Ok(())
+ }
+
+ fn end(self) -> Result<QObject> {
+ // TODO: insert elements one at a time
+ let SerializeVec { vec, .. } = self;
+ let list = QObject::from_iter(vec);
+ Ok(list)
+ }
+}
+
+impl serde::ser::SerializeTuple for SerializeVec {
+ type Ok = QObject;
+ type Error = Error;
+
+ fn serialize_element<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ serde::ser::SerializeSeq::serialize_element(self, value)
+ }
+
+ fn end(self) -> Result<QObject> {
+ serde::ser::SerializeSeq::end(self)
+ }
+}
+
+impl serde::ser::SerializeTupleStruct for SerializeVec {
+ type Ok = QObject;
+ type Error = Error;
+
+ fn serialize_field<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ serde::ser::SerializeSeq::serialize_element(self, value)
+ }
+
+ fn end(self) -> Result<QObject> {
+ serde::ser::SerializeSeq::end(self)
+ }
+}
+
+struct MapKeySerializer;
+
+impl serde::Serializer for MapKeySerializer {
+ type Ok = CString;
+ type Error = Error;
+
+ type SerializeSeq = Impossible<CString, Error>;
+ type SerializeTuple = Impossible<CString, Error>;
+ type SerializeTupleStruct = Impossible<CString, Error>;
+ type SerializeTupleVariant = Impossible<CString, Error>;
+ type SerializeMap = Impossible<CString, Error>;
+ type SerializeStruct = Impossible<CString, Error>;
+ type SerializeStructVariant = Impossible<CString, Error>;
+
+ #[inline]
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ ) -> Result<CString> {
+ Ok(CString::new(variant)?)
+ }
+
+ #[inline]
+ fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<CString>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ fn serialize_bool(self, _value: bool) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_i8(self, _value: i8) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_i16(self, _value: i16) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_i32(self, _value: i32) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_i64(self, _value: i64) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_i128(self, _value: i128) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_u8(self, _value: u8) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_u16(self, _value: u16) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_u32(self, _value: u32) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_u64(self, _value: u64) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_u128(self, _value: u128) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_f32(self, _value: f32) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_f64(self, _value: f64) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ #[inline]
+ fn serialize_char(self, _value: char) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ #[inline]
+ fn serialize_str(self, value: &str) -> Result<CString> {
+ Ok(CString::new(value)?)
+ }
+
+ fn serialize_bytes(self, value: &[u8]) -> Result<CString> {
+ Ok(CString::new(value)?)
+ }
+
+ fn serialize_unit(self) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _value: &T,
+ ) -> Result<CString>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_none(self) -> Result<CString> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_some<T>(self, _value: &T) -> Result<CString>
+ where
+ T: ?Sized + Serialize,
+ {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
+ Err(Error::KeyMustBeAString)
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ _variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStructVariant> {
+ Err(Error::KeyMustBeAString)
+ }
+}
+
+pub struct SerializeMap {
+ vec: Vec<(CString, QObject)>,
+ next_key: Option<CString>,
+}
+
+impl serde::ser::SerializeMap for SerializeMap {
+ type Ok = QObject;
+ type Error = Error;
+
+ fn serialize_key<T>(&mut self, key: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ self.next_key = Some(key.serialize(MapKeySerializer)?);
+ Ok(())
+ }
+
+ fn serialize_value<T>(&mut self, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ let key = self.next_key.take();
+ // Panic because this indicates a bug in the program rather than an
+ // expected failure.
+ let key = key.expect("serialize_value called before serialize_key");
+ self.vec.push((key, to_qobject(value)?));
+ Ok(())
+ }
+
+ fn end(self) -> Result<QObject> {
+ // TODO: insert keys one at a time
+ let SerializeMap { vec, .. } = self;
+ Ok(QObject::from_iter(vec))
+ }
+}
+
+impl serde::ser::SerializeStruct for SerializeMap {
+ type Ok = QObject;
+ type Error = Error;
+
+ fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
+ where
+ T: ?Sized + Serialize,
+ {
+ serde::ser::SerializeMap::serialize_entry(self, key, value)
+ }
+
+ fn end(self) -> Result<QObject> {
+ serde::ser::SerializeMap::end(self)
+ }
+}
+
+/// Serializer whose output is a `QObject`.
+///
+/// This is the serializer that backs [`to_qobject`].
+pub struct Serializer;
+
+impl serde::Serializer for Serializer {
+ type Ok = QObject;
+ type Error = Error;
+ type SerializeSeq = SerializeVec;
+ type SerializeTuple = SerializeVec;
+ type SerializeTupleStruct = SerializeVec;
+ type SerializeTupleVariant = SerializeTupleVariant;
+ type SerializeMap = SerializeMap;
+ type SerializeStruct = SerializeMap;
+ type SerializeStructVariant = SerializeStructVariant;
+
+ #[inline]
+ fn serialize_bool(self, value: bool) -> Result<QObject> {
+ Ok(value.into())
+ }
+
+ #[inline]
+ fn serialize_i8(self, value: i8) -> Result<QObject> {
+ self.serialize_i64(i64::from(value))
+ }
+
+ #[inline]
+ fn serialize_i16(self, value: i16) -> Result<QObject> {
+ self.serialize_i64(i64::from(value))
+ }
+
+ #[inline]
+ fn serialize_i32(self, value: i32) -> Result<QObject> {
+ self.serialize_i64(i64::from(value))
+ }
+
+ fn serialize_i64(self, value: i64) -> Result<QObject> {
+ Ok(value.into())
+ }
+
+ fn serialize_i128(self, value: i128) -> Result<QObject> {
+ if let Ok(value) = u64::try_from(value) {
+ Ok(value.into())
+ } else if let Ok(value) = i64::try_from(value) {
+ Ok(value.into())
+ } else {
+ Err(Error::NumberOutOfRange)
+ }
+ }
+
+ #[inline]
+ fn serialize_u8(self, value: u8) -> Result<QObject> {
+ self.serialize_u64(u64::from(value))
+ }
+
+ #[inline]
+ fn serialize_u16(self, value: u16) -> Result<QObject> {
+ self.serialize_u64(u64::from(value))
+ }
+
+ #[inline]
+ fn serialize_u32(self, value: u32) -> Result<QObject> {
+ self.serialize_u64(u64::from(value))
+ }
+
+ #[inline]
+ fn serialize_u64(self, value: u64) -> Result<QObject> {
+ Ok(value.into())
+ }
+
+ fn serialize_u128(self, value: u128) -> Result<QObject> {
+ if let Ok(value) = u64::try_from(value) {
+ Ok(value.into())
+ } else {
+ Err(Error::NumberOutOfRange)
+ }
+ }
+
+ #[inline]
+ fn serialize_f32(self, float: f32) -> Result<QObject> {
+ self.serialize_f64(f64::from(float))
+ }
+
+ #[inline]
+ fn serialize_f64(self, float: f64) -> Result<QObject> {
+ Ok(float.into())
+ }
+
+ #[inline]
+ fn serialize_char(self, value: char) -> Result<QObject> {
+ let mut s = String::new();
+ s.push(value);
+ Ok(CString::new(s)?.into())
+ }
+
+ #[inline]
+ fn serialize_str(self, value: &str) -> Result<QObject> {
+ Ok(CString::new(value)?.into())
+ }
+
+ fn serialize_bytes(self, value: &[u8]) -> Result<QObject> {
+ // Serialize into a vector of numeric QObjects
+ let it = value.iter().map(|&b| u64::from(b));
+ Ok(QObject::from_iter(it))
+ }
+
+ #[inline]
+ fn serialize_unit(self) -> Result<QObject> {
+ Ok(().into())
+ }
+
+ #[inline]
+ fn serialize_unit_struct(self, _name: &'static str) -> Result<QObject> {
+ self.serialize_unit()
+ }
+
+ #[inline]
+ fn serialize_unit_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ ) -> Result<QObject> {
+ self.serialize_str(variant)
+ }
+
+ #[inline]
+ fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<QObject>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ fn serialize_newtype_variant<T>(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ value: &T,
+ ) -> Result<QObject>
+ where
+ T: ?Sized + Serialize,
+ {
+ // serde by default represents enums as a single-entry object, with
+ // the variant stored in the key ("external tagging"). Internal tagging
+ // is implemented by the struct that requests it, not by the serializer.
+ let map = [(CString::new(variant)?, to_qobject(value)?)];
+ Ok(QObject::from_iter(map))
+ }
+
+ #[inline]
+ fn serialize_none(self) -> Result<QObject> {
+ self.serialize_unit()
+ }
+
+ #[inline]
+ fn serialize_some<T>(self, value: &T) -> Result<QObject>
+ where
+ T: ?Sized + Serialize,
+ {
+ value.serialize(self)
+ }
+
+ fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
+ Ok(SerializeVec {
+ vec: Vec::with_capacity(len.unwrap_or(0)),
+ })
+ }
+
+ fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
+ self.serialize_seq(Some(len))
+ }
+
+ fn serialize_tuple_struct(
+ self,
+ _name: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeTupleStruct> {
+ self.serialize_seq(Some(len))
+ }
+
+ fn serialize_tuple_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ len: usize,
+ ) -> Result<Self::SerializeTupleVariant> {
+ Ok(SerializeTupleVariant {
+ name: CString::new(variant)?,
+ vec: Vec::with_capacity(len),
+ })
+ }
+
+ fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
+ Ok(SerializeMap {
+ vec: Vec::new(),
+ next_key: None,
+ })
+ }
+ fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
+ self.serialize_map(Some(len))
+ }
+
+ fn serialize_struct_variant(
+ self,
+ _name: &'static str,
+ _variant_index: u32,
+ variant: &'static str,
+ _len: usize,
+ ) -> Result<Self::SerializeStructVariant> {
+ Ok(SerializeStructVariant {
+ name: CString::new(variant)?,
+ vec: Vec::new(),
+ })
+ }
+}
+
+pub fn to_qobject<T>(input: T) -> Result<QObject>
+where
+ T: Serialize,
+{
+ input.serialize(Serializer)
+}
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 06/14] rust: add Deserialize implementation for QObject
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (5 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 05/14] rust: add Serializer (to_qobject) " Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 07/14] rust: add Deserializer (from_qobject) " Paolo Bonzini
` (7 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
This allows QObject to be created from any serializable format, for
example JSON via serde_json.
This is not too useful, since QObjects are produced by
C code or by serializing 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>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/util/meson.build | 1 +
rust/util/src/qobject/deserialize.rs | 134 +++++++++++++++++++++++++++
rust/util/src/qobject/mod.rs | 1 +
3 files changed, 136 insertions(+)
create mode 100644 rust/util/src/qobject/deserialize.rs
diff --git a/rust/util/meson.build b/rust/util/meson.build
index fb152766003..2b72af99dd5 100644
--- a/rust/util/meson.build
+++ b/rust/util/meson.build
@@ -39,6 +39,7 @@ _util_rs = static_library(
{'.': _util_bindings_inc_rs,
'qobject': [
'src/qobject/mod.rs',
+ 'src/qobject/deserialize.rs',
'src/qobject/error.rs',
'src/qobject/serializer.rs',
'src/qobject/serialize.rs',
diff --git a/rust/util/src/qobject/deserialize.rs b/rust/util/src/qobject/deserialize.rs
new file mode 100644
index 00000000000..280a577b6be
--- /dev/null
+++ b/rust/util/src/qobject/deserialize.rs
@@ -0,0 +1,134 @@
+//! `QObject` deserialization
+//!
+//! This module implements the [`Deserialize`] trait for `QObject`,
+//! allowing it to be created from any serializable format, for
+//! example JSON.
+
+use core::fmt;
+use std::ffi::CString;
+
+use serde::de::{self, Deserialize, MapAccess, SeqAccess, Visitor};
+
+use super::{to_qobject, QObject};
+
+impl<'de> Deserialize<'de> for QObject {
+ #[inline]
+ fn deserialize<D>(deserializer: D) -> Result<QObject, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ struct ValueVisitor;
+
+ impl<'de> Visitor<'de> for ValueVisitor {
+ type Value = QObject;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str("any valid JSON value")
+ }
+
+ #[inline]
+ fn visit_bool<E>(self, value: bool) -> Result<QObject, E> {
+ Ok(value.into())
+ }
+
+ #[inline]
+ fn visit_i64<E>(self, value: i64) -> Result<QObject, E> {
+ Ok(value.into())
+ }
+
+ fn visit_i128<E>(self, value: i128) -> Result<QObject, E>
+ where
+ E: serde::de::Error,
+ {
+ to_qobject(value).map_err(|_| de::Error::custom("number out of range"))
+ }
+
+ #[inline]
+ fn visit_u64<E>(self, value: u64) -> Result<QObject, E> {
+ Ok(value.into())
+ }
+
+ fn visit_u128<E>(self, value: u128) -> Result<QObject, E>
+ where
+ E: serde::de::Error,
+ {
+ to_qobject(value).map_err(|_| de::Error::custom("number out of range"))
+ }
+
+ #[inline]
+ fn visit_f64<E>(self, value: f64) -> Result<QObject, E> {
+ Ok(value.into())
+ }
+
+ #[inline]
+ fn visit_str<E>(self, value: &str) -> Result<QObject, E>
+ where
+ E: serde::de::Error,
+ {
+ CString::new(value)
+ .map_err(|_| de::Error::custom("NUL character in string"))
+ .map(QObject::from)
+ }
+
+ #[inline]
+ fn visit_string<E>(self, value: String) -> Result<QObject, E>
+ where
+ E: serde::de::Error,
+ {
+ CString::new(value)
+ .map_err(|_| de::Error::custom("NUL character in string"))
+ .map(QObject::from)
+ }
+
+ #[inline]
+ fn visit_none<E>(self) -> Result<QObject, E> {
+ Ok(().into())
+ }
+
+ #[inline]
+ fn visit_some<D>(self, deserializer: D) -> Result<QObject, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ Deserialize::deserialize(deserializer)
+ }
+
+ #[inline]
+ fn visit_unit<E>(self) -> Result<QObject, E> {
+ Ok(().into())
+ }
+
+ #[inline]
+ fn visit_seq<V>(self, mut visitor: V) -> Result<QObject, V::Error>
+ where
+ V: SeqAccess<'de>,
+ {
+ // TODO: insert elements one at a time
+ let mut vec = Vec::<QObject>::new();
+
+ while let Some(elem) = visitor.next_element()? {
+ vec.push(elem);
+ }
+ Ok(QObject::from_iter(vec))
+ }
+
+ fn visit_map<V>(self, mut visitor: V) -> Result<QObject, V::Error>
+ where
+ V: MapAccess<'de>,
+ {
+ // TODO: insert elements one at a time
+ let mut vec = Vec::<(CString, QObject)>::new();
+
+ if let Some(first_key) = visitor.next_key()? {
+ vec.push((first_key, visitor.next_value()?));
+ while let Some((key, value)) = visitor.next_entry()? {
+ vec.push((key, value));
+ }
+ }
+ Ok(QObject::from_iter(vec))
+ }
+ }
+
+ deserializer.deserialize_any(ValueVisitor)
+ }
+}
diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs
index cd034185748..aec635a5ccc 100644
--- a/rust/util/src/qobject/mod.rs
+++ b/rust/util/src/qobject/mod.rs
@@ -6,6 +6,7 @@
#![deny(clippy::unwrap_used)]
+mod deserialize;
mod error;
mod serialize;
mod serializer;
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 07/14] rust: add Deserializer (from_qobject) implementation for QObject
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (6 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 06/14] rust: add Deserialize " Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 08/14] rust/qobject: add Display/Debug Paolo Bonzini
` (6 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
This allows creating any serializable data structure from QObject.
Co-authored-by: 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>
---
docs/devel/rust.rst | 1 +
rust/util/meson.build | 1 +
rust/util/src/qobject/deserializer.rs | 373 ++++++++++++++++++++++++++
rust/util/src/qobject/error.rs | 8 +-
rust/util/src/qobject/mod.rs | 2 +
5 files changed, 384 insertions(+), 1 deletion(-)
create mode 100644 rust/util/src/qobject/deserializer.rs
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index 79c26d9d165..c7f3a496a25 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -162,6 +162,7 @@ module status
``util::error`` stable
``util::log`` proof of concept
``util::module`` complete
+``util::qobject`` stable
``util::timer`` stable
========================== ======================
diff --git a/rust/util/meson.build b/rust/util/meson.build
index 2b72af99dd5..45366d03786 100644
--- a/rust/util/meson.build
+++ b/rust/util/meson.build
@@ -39,6 +39,7 @@ _util_rs = static_library(
{'.': _util_bindings_inc_rs,
'qobject': [
'src/qobject/mod.rs',
+ 'src/qobject/deserializer.rs',
'src/qobject/deserialize.rs',
'src/qobject/error.rs',
'src/qobject/serializer.rs',
diff --git a/rust/util/src/qobject/deserializer.rs b/rust/util/src/qobject/deserializer.rs
new file mode 100644
index 00000000000..a2fe417e72a
--- /dev/null
+++ b/rust/util/src/qobject/deserializer.rs
@@ -0,0 +1,373 @@
+//! `QObject` deserializer
+//!
+//! This module implements a [`Deserializer`](serde::de::Deserializer) that
+//! produces `QObject`s, allowing them to be turned into deserializable data
+//! structures (such as primitive data types, or structs that implement
+//! `Deserialize`).
+
+use std::ffi::CStr;
+
+use serde::de::{
+ self, value::StrDeserializer, DeserializeSeed, Expected, MapAccess, SeqAccess, Unexpected,
+ Visitor,
+};
+
+use super::{
+ error::{Error, Result},
+ match_qobject, QObject,
+};
+use crate::bindings;
+
+impl QObject {
+ #[cold]
+ fn invalid_type<E>(&self, exp: &dyn Expected) -> E
+ where
+ E: serde::de::Error,
+ {
+ serde::de::Error::invalid_type(self.unexpected(), exp)
+ }
+
+ #[cold]
+ fn unexpected(&self) -> Unexpected<'_> {
+ match_qobject! { (self) =>
+ () => Unexpected::Unit,
+ bool(b) => Unexpected::Bool(b),
+ i64(n) => Unexpected::Signed(n),
+ u64(n) => Unexpected::Unsigned(n),
+ f64(n) => Unexpected::Float(n),
+ CStr(s) => s.to_str().map_or_else(
+ |_| Unexpected::Other("string with invalid UTF-8"),
+ Unexpected::Str),
+ QList(_) => Unexpected::Seq,
+ QDict(_) => Unexpected::Map,
+ }
+ }
+}
+
+fn visit_qlist_ref<'de, V>(qlist: &'de bindings::QList, visitor: V) -> Result<V::Value>
+where
+ V: Visitor<'de>,
+{
+ struct QListDeserializer(*mut bindings::QListEntry, usize);
+
+ impl<'de> SeqAccess<'de> for QListDeserializer {
+ type Error = Error;
+
+ fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ if self.0.is_null() {
+ return Ok(None);
+ }
+
+ let e = unsafe { &*self.0 };
+ // increment the reference count because deserialize consumes `value`.
+ let value = unsafe { QObject::cloned_from_raw(e.value.cast_const()) };
+ let result = seed.deserialize(value);
+ self.0 = unsafe { e.next.tqe_next };
+ self.1 += 1;
+ result.map(Some)
+ }
+ }
+
+ let mut deserializer = QListDeserializer(unsafe { qlist.head.tqh_first }, 0);
+ let seq = visitor.visit_seq(&mut deserializer)?;
+ if deserializer.0.is_null() {
+ Ok(seq)
+ } else {
+ Err(serde::de::Error::invalid_length(
+ deserializer.1,
+ &"fewer elements in array",
+ ))
+ }
+}
+
+fn visit_qdict_ref<'de, V>(qdict: &'de bindings::QDict, visitor: V) -> Result<V::Value>
+where
+ V: Visitor<'de>,
+{
+ struct QDictDeserializer(*mut bindings::QDict, *const bindings::QDictEntry);
+
+ impl<'de> MapAccess<'de> for QDictDeserializer {
+ type Error = Error;
+
+ fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ if self.1.is_null() {
+ return Ok(None);
+ }
+
+ let e = unsafe { &*self.1 };
+ let key = unsafe { CStr::from_ptr(e.key) };
+ let key_de = StrDeserializer::new(key.to_str()?);
+ seed.deserialize(key_de).map(Some)
+ }
+
+ fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value>
+ where
+ T: DeserializeSeed<'de>,
+ {
+ if self.1.is_null() {
+ panic!("next_key must have returned None");
+ }
+
+ let e = unsafe { &*self.1 };
+ // increment the reference count because deserialize consumes `value`.
+ let value = unsafe { QObject::cloned_from_raw(e.value) };
+ let result = seed.deserialize(value);
+ self.1 = unsafe { bindings::qdict_next(self.0, self.1) };
+ result
+ }
+ }
+
+ let qdict = (qdict as *const bindings::QDict).cast_mut();
+ let e = unsafe { &*bindings::qdict_first(qdict) };
+ let mut deserializer = QDictDeserializer(qdict, e);
+ let map = visitor.visit_map(&mut deserializer)?;
+ if deserializer.1.is_null() {
+ Ok(map)
+ } else {
+ Err(serde::de::Error::invalid_length(
+ unsafe { bindings::qdict_size(qdict) },
+ &"fewer elements in map",
+ ))
+ }
+}
+
+fn visit_qnum_ref<'de, V>(qnum: QObject, visitor: V) -> Result<V::Value>
+where
+ V: Visitor<'de>,
+{
+ match_qobject! { (qnum) =>
+ i64(n) => visitor.visit_i64(n),
+ u64(n) => visitor.visit_u64(n),
+ f64(n) => visitor.visit_f64(n),
+ _ => Err(qnum.invalid_type(&"number")),
+ }
+}
+
+macro_rules! deserialize_number {
+ ($method:ident) => {
+ fn $method<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ visit_qnum_ref(self, visitor)
+ }
+ };
+}
+impl<'de> serde::Deserializer<'de> for QObject {
+ type Error = Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ () => visitor.visit_unit(),
+ bool(v) => visitor.visit_bool(v),
+ i64(n) => visitor.visit_i64(n),
+ u64(n) => visitor.visit_u64(n),
+ f64(n) => visitor.visit_f64(n),
+ CStr(cstr) => visitor.visit_borrowed_str(cstr.to_str()?),
+ QList(qlist) => visit_qlist_ref(qlist, visitor),
+ QDict(qdict) => visit_qdict_ref(qdict, visitor),
+ }
+ }
+
+ deserialize_number!(deserialize_i8);
+ deserialize_number!(deserialize_i16);
+ deserialize_number!(deserialize_i32);
+ deserialize_number!(deserialize_i64);
+ deserialize_number!(deserialize_i128);
+ deserialize_number!(deserialize_u8);
+ deserialize_number!(deserialize_u16);
+ deserialize_number!(deserialize_u32);
+ deserialize_number!(deserialize_u64);
+ deserialize_number!(deserialize_u128);
+ deserialize_number!(deserialize_f32);
+ deserialize_number!(deserialize_f64);
+
+ fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ () => visitor.visit_none(),
+ _ => visitor.visit_some(self),
+ }
+ }
+
+ fn deserialize_enum<V>(
+ self,
+ _name: &'static str,
+ _variants: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ CStr(cstr) => visitor.visit_borrowed_str(cstr.to_str()?),
+ QDict(qdict) => visit_qdict_ref(qdict, visitor),
+ _ => Err(self.invalid_type(&"string or map")),
+ }
+ }
+
+ #[inline]
+ fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ let _ = name;
+ visitor.visit_newtype_struct(self)
+ }
+
+ fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ bool(v) => visitor.visit_bool(v),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ CStr(cstr) => visitor.visit_borrowed_str(cstr.to_str()?),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ CStr(cstr) => visitor.visit_borrowed_str(cstr.to_str()?),
+ QList(qlist) => visit_qlist_ref(qlist, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_bytes(visitor)
+ }
+
+ fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ () => visitor.visit_unit(),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_unit(visitor)
+ }
+
+ fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ QList(qlist) => visit_qlist_ref(qlist, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_tuple_struct<V>(
+ self,
+ _name: &'static str,
+ _len: usize,
+ visitor: V,
+ ) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_seq(visitor)
+ }
+
+ fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ QDict(qdict) => visit_qdict_ref(qdict, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_struct<V>(
+ self,
+ _name: &'static str,
+ _fields: &'static [&'static str],
+ visitor: V,
+ ) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ match_qobject! { (self) =>
+ QList(qlist) => visit_qlist_ref(qlist, visitor),
+ QDict(qdict) => visit_qdict_ref(qdict, visitor),
+ _ => Err(self.invalid_type(&visitor)),
+ }
+ }
+
+ fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_str(visitor)
+ }
+
+ fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
+ where
+ V: Visitor<'de>,
+ {
+ visitor.visit_unit()
+ }
+}
+
+pub fn from_qobject<T>(value: QObject) -> Result<T>
+where
+ T: de::DeserializeOwned,
+{
+ T::deserialize(value)
+}
diff --git a/rust/util/src/qobject/error.rs b/rust/util/src/qobject/error.rs
index 5212e65c4f7..2d7c180187a 100644
--- a/rust/util/src/qobject/error.rs
+++ b/rust/util/src/qobject/error.rs
@@ -6,7 +6,7 @@
str::Utf8Error,
};
-use serde::ser;
+use serde::{de, ser};
#[derive(Debug)]
pub enum Error {
@@ -23,6 +23,12 @@ fn custom<T: Display>(msg: T) -> Self {
}
}
+impl de::Error for Error {
+ fn custom<T: Display>(msg: T) -> Self {
+ Error::Custom(msg.to_string())
+ }
+}
+
impl From<NulError> for Error {
fn from(_: NulError) -> Self {
Error::NulEncountered
diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs
index aec635a5ccc..1c18a491720 100644
--- a/rust/util/src/qobject/mod.rs
+++ b/rust/util/src/qobject/mod.rs
@@ -7,6 +7,7 @@
#![deny(clippy::unwrap_used)]
mod deserialize;
+mod deserializer;
mod error;
mod serialize;
mod serializer;
@@ -20,6 +21,7 @@
};
use common::assert_field_type;
+pub use deserializer::from_qobject;
pub use error::{Error, Result};
pub use serializer::to_qobject;
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 08/14] rust/qobject: add Display/Debug
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (7 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 07/14] rust: add Deserializer (from_qobject) " Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 09/14] scripts/qapi: add QAPISchemaIfCond.rsgen() Paolo Bonzini
` (5 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
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 1c18a491720..b6e86f11a64 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},
@@ -231,6 +232,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.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 09/14] scripts/qapi: add QAPISchemaIfCond.rsgen()
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (8 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 08/14] rust/qobject: add Display/Debug Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 10/14] scripts/qapi: generate high-level Rust bindings Paolo Bonzini
` (4 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
From: Marc-André Lureau <marcandre.lureau@redhat.com>
Generate Rust #[cfg(...)] guards from QAPI 'if' conditions.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Link: https://lore.kernel.org/r/20210907121943.3498701-15-marcandre.lureau@redhat.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
scripts/qapi/common.py | 16 ++++++++++++++++
scripts/qapi/schema.py | 4 ++++
2 files changed, 20 insertions(+)
diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index d7c8aa3365c..f16b9568bb9 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -199,6 +199,22 @@ def guardend(name: str) -> str:
name=c_fname(name).upper())
+def rsgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str:
+
+ def cfg(ifcond: Union[str, Dict[str, Any]]) -> str:
+ if isinstance(ifcond, str):
+ return ifcond
+ if isinstance(ifcond, list):
+ return ', '.join([cfg(c) for c in ifcond])
+ oper, operands = next(iter(ifcond.items()))
+ operands = cfg(operands)
+ return f'{oper}({operands})'
+
+ if not ifcond:
+ return ''
+ return '#[cfg(%s)]' % cfg(ifcond)
+
+
def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]],
cond_fmt: str, not_fmt: str,
all_operator: str, any_operator: str) -> str:
diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
index 8d88b40de2e..848a7401251 100644
--- a/scripts/qapi/schema.py
+++ b/scripts/qapi/schema.py
@@ -37,6 +37,7 @@
docgen_ifcond,
gen_endif,
gen_if,
+ rsgen_ifcond,
)
from .error import QAPIError, QAPISemError, QAPISourceError
from .expr import check_exprs
@@ -63,6 +64,9 @@ def gen_endif(self) -> str:
def docgen(self) -> str:
return docgen_ifcond(self.ifcond)
+ def rsgen(self) -> str:
+ return rsgen_ifcond(self.ifcond)
+
def is_present(self) -> bool:
return bool(self.ifcond)
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 10/14] scripts/qapi: generate high-level Rust bindings
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (9 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 09/14] scripts/qapi: add QAPISchemaIfCond.rsgen() Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 11/14] scripts/qapi: strip trailing whitespaces Paolo Bonzini
` (3 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
From: Marc-André Lureau <marcandre.lureau@redhat.com>
Generate high-level native Rust declarations for the QAPI types.
- char* is mapped to String, scalars to there corresponding Rust types
- enums are simply aliased from FFI
- has_foo/foo members are mapped to Option<T>
- lists are represented as Vec<T>
- structures have Rust versions, with To/From FFI conversions
- alternate are represented as Rust enum
- unions are represented in a similar way as in C: a struct S with a "u"
member (since S may have extra 'base' fields). However, the discriminant
isn't a member of S, since Rust enum already include it.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Link: https://lore.kernel.org/r/20210907121943.3498701-21-marcandre.lureau@redhat.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
meson.build | 4 +-
scripts/qapi/backend.py | 28 +++-
scripts/qapi/main.py | 4 +-
scripts/qapi/rs.py | 176 +++++++++++++++++++
scripts/qapi/rs_types.py | 354 +++++++++++++++++++++++++++++++++++++++
5 files changed, 562 insertions(+), 4 deletions(-)
create mode 100644 scripts/qapi/rs.py
create mode 100644 scripts/qapi/rs_types.py
diff --git a/meson.build b/meson.build
index 0eb4f850582..8236205d4c5 100644
--- a/meson.build
+++ b/meson.build
@@ -3559,12 +3559,14 @@ qapi_gen_depends = [ meson.current_source_dir() / 'scripts/qapi/__init__.py',
meson.current_source_dir() / 'scripts/qapi/introspect.py',
meson.current_source_dir() / 'scripts/qapi/main.py',
meson.current_source_dir() / 'scripts/qapi/parser.py',
+ meson.current_source_dir() / 'scripts/qapi/rs_types.py',
meson.current_source_dir() / 'scripts/qapi/schema.py',
meson.current_source_dir() / 'scripts/qapi/source.py',
meson.current_source_dir() / 'scripts/qapi/types.py',
meson.current_source_dir() / 'scripts/qapi/features.py',
meson.current_source_dir() / 'scripts/qapi/visit.py',
- meson.current_source_dir() / 'scripts/qapi-gen.py'
+ meson.current_source_dir() / 'scripts/qapi-gen.py',
+ meson.current_source_dir() / 'scripts/qapi/rs.py',
]
tracetool = [
diff --git a/scripts/qapi/backend.py b/scripts/qapi/backend.py
index 49ae6ecdd33..9b9c9708f46 100644
--- a/scripts/qapi/backend.py
+++ b/scripts/qapi/backend.py
@@ -7,6 +7,7 @@
from .events import gen_events
from .features import gen_features
from .introspect import gen_introspect
+from .rs_types import gen_rs_types
from .schema import QAPISchema
from .types import gen_types
from .visit import gen_visit
@@ -36,7 +37,7 @@ def generate(self,
"""
-class QAPICBackend(QAPIBackend):
+class QAPICodeBackend(QAPIBackend):
# pylint: disable=too-few-public-methods
def generate(self,
@@ -63,3 +64,28 @@ def generate(self,
gen_commands(schema, output_dir, prefix, gen_tracing)
gen_events(schema, output_dir, prefix)
gen_introspect(schema, output_dir, prefix, unmask)
+
+
+
+class QAPIRsBackend(QAPIBackend):
+ # pylint: disable=too-few-public-methods
+
+ def generate(self,
+ schema: QAPISchema,
+ output_dir: str,
+ prefix: str,
+ unmask: bool,
+ builtins: bool,
+ gen_tracing: bool) -> None:
+ """
+ Generate Rust code for the given schema into the target directory.
+
+ :param schema_file: The primary QAPI schema file.
+ :param output_dir: The output directory to store generated code.
+ :param prefix: Optional C-code prefix for symbol names.
+ :param unmask: Expose non-ABI names through introspection?
+ :param builtins: Generate code for built-in types?
+
+ :raise QAPIError: On failures.
+ """
+ gen_rs_types(schema, output_dir, prefix, builtins)
diff --git a/scripts/qapi/main.py b/scripts/qapi/main.py
index 0e2a6ae3f07..4ad75e213f5 100644
--- a/scripts/qapi/main.py
+++ b/scripts/qapi/main.py
@@ -12,7 +12,7 @@
import sys
from typing import Optional
-from .backend import QAPIBackend, QAPICBackend
+from .backend import QAPIBackend, QAPICodeBackend
from .common import must_match
from .error import QAPIError
from .schema import QAPISchema
@@ -27,7 +27,7 @@ def invalid_prefix_char(prefix: str) -> Optional[str]:
def create_backend(path: str) -> QAPIBackend:
if path is None:
- return QAPICBackend()
+ return QAPICodeBackend()
module_path, dot, class_name = path.rpartition('.')
if not dot:
diff --git a/scripts/qapi/rs.py b/scripts/qapi/rs.py
new file mode 100644
index 00000000000..37b9d4ad569
--- /dev/null
+++ b/scripts/qapi/rs.py
@@ -0,0 +1,176 @@
+# This work is licensed under the terms of the GNU GPL, version 2.
+# See the COPYING file in the top-level directory.
+"""
+QAPI Rust generator
+"""
+
+import os
+import re
+import subprocess
+from typing import NamedTuple, Optional
+
+from .common import POINTER_SUFFIX
+from .gen import QAPIGen
+from .schema import QAPISchemaModule, QAPISchemaVisitor
+
+
+# see to_snake_case() below
+snake_case = re.compile(r'((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
+
+
+rs_name_trans = str.maketrans('.-', '__')
+
+
+# Map @name to a valid Rust identifier.
+# If @protect, avoid returning certain ticklish identifiers (like
+# keywords) by prepending raw identifier prefix 'r#'.
+def rs_name(name: str, protect: bool = True) -> str:
+ name = name.translate(rs_name_trans)
+ if name[0].isnumeric():
+ name = '_' + name
+ if not protect:
+ return name
+ # based from the list:
+ # https://doc.rust-lang.org/reference/keywords.html
+ if name in ('Self', 'abstract', 'as', 'async',
+ 'await', 'become', 'box', 'break',
+ 'const', 'continue', 'crate', 'do',
+ 'dyn', 'else', 'enum', 'extern',
+ 'false', 'final', 'fn', 'for',
+ 'if', 'impl', 'in', 'let',
+ 'loop', 'macro', 'match', 'mod',
+ 'move', 'mut', 'override', 'priv',
+ 'pub', 'ref', 'return', 'self',
+ 'static', 'struct', 'super', 'trait',
+ 'true', 'try', 'type', 'typeof',
+ 'union', 'unsafe', 'unsized', 'use',
+ 'virtual', 'where', 'while', 'yield'):
+ name = 'r#' + name
+ # avoid some clashes with the standard library
+ if name in ('String',):
+ name = 'Qapi' + name
+
+ return name
+
+
+def rs_type(c_type: str,
+ qapi_ns: str = 'qapi::',
+ optional: bool = False,
+ box: bool = False) -> str:
+ (is_pointer, _, is_list, c_type) = rs_ctype_parse(c_type)
+ to_rs = {
+ 'QNull': '()',
+ 'QObject': 'QObject',
+ 'bool': 'bool',
+ 'char': 'i8',
+ 'double': 'f64',
+ 'int': 'i64',
+ 'int16': 'i16',
+ 'int16_t': 'i16',
+ 'int32': 'i32',
+ 'int32_t': 'i32',
+ 'int64': 'i64',
+ 'int64_t': 'i64',
+ 'int8': 'i8',
+ 'int8_t': 'i8',
+ 'number': 'f64',
+ 'size': 'u64',
+ 'str': 'String',
+ 'uint16': 'u16',
+ 'uint16_t': 'u16',
+ 'uint32': 'u32',
+ 'uint32_t': 'u32',
+ 'uint64': 'u64',
+ 'uint64_t': 'u64',
+ 'uint8': 'u8',
+ 'uint8_t': 'u8',
+ 'String': 'QapiString',
+ }
+ if is_pointer:
+ to_rs.update({
+ 'char': 'String',
+ })
+
+ if is_list:
+ c_type = c_type[:-4]
+
+ ret = to_rs.get(c_type, qapi_ns + c_type)
+ if is_list:
+ ret = 'Vec<%s>' % ret
+ elif is_pointer and c_type not in to_rs and box:
+ ret = 'Box<%s>' % ret
+ if optional:
+ ret = 'Option<%s>' % ret
+ return ret
+
+
+class CType(NamedTuple):
+ is_pointer: bool
+ is_const: bool
+ is_list: bool
+ c_type: str
+
+
+def rs_ctype_parse(c_type: str) -> CType:
+ is_pointer = False
+ if c_type.endswith(POINTER_SUFFIX):
+ is_pointer = True
+ c_type = c_type[:-len(POINTER_SUFFIX)]
+ is_list = c_type.endswith('List')
+ is_const = False
+ if c_type.startswith('const '):
+ is_const = True
+ c_type = c_type[6:]
+
+ c_type = rs_name(c_type)
+ return CType(is_pointer, is_const, is_list, c_type)
+
+
+def to_camel_case(value: str) -> str:
+ # special case for last enum value
+ if value == '_MAX':
+ return value
+ raw_id = False
+ if value.startswith('r#'):
+ raw_id = True
+ value = value[2:]
+ value = ''.join('_' + word if word[0].isdigit()
+ else word[:1].upper() + word[1:]
+ for word in filter(None, re.split("[-_]+", value)))
+ if raw_id:
+ return 'r#' + value
+ return value
+
+
+def to_snake_case(value: str) -> str:
+ return snake_case.sub(r'_\1', value).lower()
+
+
+class QAPIGenRs(QAPIGen):
+ pass
+
+
+class QAPISchemaRsVisitor(QAPISchemaVisitor):
+
+ def __init__(self, prefix: str, what: str):
+ super().__init__()
+ self._prefix = prefix
+ self._what = what
+ self._gen = QAPIGenRs(self._prefix + self._what + '.rs')
+ self._main_module: Optional[str] = None
+
+ def visit_module(self, name: Optional[str]) -> None:
+ if name is None:
+ return
+ if QAPISchemaModule.is_user_module(name):
+ if self._main_module is None:
+ self._main_module = name
+
+ def write(self, output_dir: str) -> None:
+ self._gen.write(output_dir)
+
+ pathname = os.path.join(output_dir, self._gen.fname)
+ try:
+ subprocess.check_call(['rustfmt', pathname])
+ except FileNotFoundError:
+ pass
diff --git a/scripts/qapi/rs_types.py b/scripts/qapi/rs_types.py
new file mode 100644
index 00000000000..f124106d7f0
--- /dev/null
+++ b/scripts/qapi/rs_types.py
@@ -0,0 +1,354 @@
+# This work is licensed under the terms of the GNU GPL, version 2.
+# See the COPYING file in the top-level directory.
+"""
+QAPI Rust types generator
+"""
+
+from typing import List, Optional, Set
+
+from .common import camel_to_upper, mcgen
+from .rs import (
+ QAPISchemaRsVisitor,
+ rs_name,
+ rs_type,
+ to_camel_case,
+ to_snake_case,
+)
+from .schema import (
+ QAPISchema,
+ QAPISchemaEnumMember,
+ QAPISchemaFeature,
+ QAPISchemaIfCond,
+ QAPISchemaBuiltinType,
+ QAPISchemaAlternateType,
+ QAPISchemaArrayType,
+ QAPISchemaObjectType,
+ QAPISchemaObjectTypeMember,
+ QAPISchemaType,
+ QAPISchemaVariants,
+)
+from .source import QAPISourceInfo
+
+
+objects_seen = set()
+
+
+def gen_rs_variants_to_tag(name: str,
+ ifcond: QAPISchemaIfCond,
+ variants: QAPISchemaVariants) -> str:
+ ret = mcgen('''
+
+%(cfg)s
+impl From<&%(rs_name)sVariant> for %(tag)s {
+ fn from(e: &%(rs_name)sVariant) -> Self {
+ match e {
+ ''',
+ cfg=ifcond.rsgen(),
+ rs_name=rs_name(name),
+ tag=rs_type(variants.tag_member.type.c_type(), ''))
+
+ for var in variants.variants:
+ type_name = var.type.name
+ var_name = to_camel_case(rs_name(var.name))
+ patt = '(_)'
+ if type_name == 'q_empty':
+ patt = ''
+ ret += mcgen('''
+ %(cfg)s
+ %(rs_name)sVariant::%(var_name)s%(patt)s => Self::%(var_name)s,
+''',
+ cfg=var.ifcond.rsgen(),
+ rs_name=rs_name(name),
+ var_name=var_name,
+ patt=patt)
+
+ ret += mcgen('''
+ }
+ }
+}
+''')
+ return ret
+
+
+def gen_rs_variants(name: str,
+ ifcond: QAPISchemaIfCond,
+ variants: QAPISchemaVariants) -> str:
+ ret = mcgen('''
+
+%(cfg)s
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub enum %(rs_name)sVariant {
+''',
+ cfg=ifcond.rsgen(),
+ rs_name=rs_name(name))
+
+ for var in variants.variants:
+ type_name = var.type.name
+ var_name = to_camel_case(rs_name(var.name, False))
+ if type_name == 'q_empty':
+ ret += mcgen('''
+ %(cfg)s
+ %(var_name)s,
+''',
+ cfg=var.ifcond.rsgen(),
+ var_name=var_name)
+ else:
+ c_type = var.type.c_unboxed_type()
+ if c_type.endswith('_wrapper'):
+ c_type = c_type[6:-8] # remove q_obj*-wrapper
+ ret += mcgen('''
+ %(cfg)s
+ %(var_name)s(%(rs_type)s),
+''',
+ cfg=var.ifcond.rsgen(),
+ var_name=var_name,
+ rs_type=rs_type(c_type, ''))
+
+ ret += mcgen('''
+}
+''')
+
+ ret += gen_rs_variants_to_tag(name, ifcond, variants)
+
+ return ret
+
+
+def gen_rs_members(members: List[QAPISchemaObjectTypeMember],
+ exclude: Optional[List[str]] = None) -> List[str]:
+ exclude = exclude or []
+ return [f"{m.ifcond.rsgen()} {to_snake_case(rs_name(m.name))}"
+ for m in members if m.name not in exclude]
+
+
+def has_recursive_type(memb: QAPISchemaType,
+ name: str,
+ visited: Set[str]) -> bool:
+ if name == memb.name:
+ return True
+ if memb.name in visited:
+ return False
+ visited.add(memb.name)
+ if isinstance(memb, QAPISchemaObjectType):
+ if memb.base and has_recursive_type(memb.base, name, visited):
+ return True
+ if memb.branches and any(has_recursive_type(m.type, name, visited) for m in memb.branches.variants):
+ return True
+ if any(has_recursive_type(m.type, name, visited) for m in memb.members):
+ return True
+ return any(has_recursive_type(m.type, name, visited) for m in memb.local_members)
+ elif isinstance(memb, QAPISchemaAlternateType):
+ return any(has_recursive_type(m.type, name, visited) for m in memb.alternatives.variants)
+ elif isinstance(memb, QAPISchemaArrayType):
+ return has_recursive_type(memb.element_type, name, visited)
+ else:
+ pass
+ return False
+
+
+def gen_struct_members(members: List[QAPISchemaObjectTypeMember],
+ name: str) -> str:
+ ret = ''
+ for memb in members:
+ is_recursive = has_recursive_type(memb.type, name, set())
+ typ = rs_type(memb.type.c_type(), '', optional=memb.optional, box=is_recursive)
+ ret += mcgen('''
+ %(cfg)s
+ pub %(rs_name)s: %(rs_type)s,
+''',
+ cfg=memb.ifcond.rsgen(),
+ rs_type=typ,
+ rs_name=to_snake_case(rs_name(memb.name)))
+ return ret
+
+
+def gen_rs_object(name: str,
+ ifcond: QAPISchemaIfCond,
+ base: Optional[QAPISchemaObjectType],
+ members: List[QAPISchemaObjectTypeMember],
+ variants: Optional[QAPISchemaVariants]) -> str:
+ if name in objects_seen:
+ return ''
+
+ if variants:
+ members = [m for m in members
+ if m.name != variants.tag_member.name]
+
+ ret = ''
+ objects_seen.add(name)
+
+ if variants:
+ ret += gen_rs_variants(name, ifcond, variants)
+
+ ret += mcgen('''
+
+%(cfg)s
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub struct %(rs_name)s {
+''',
+ cfg=ifcond.rsgen(),
+ rs_name=rs_name(name))
+
+ if base:
+ if not base.is_implicit():
+ ret += mcgen('''
+ // Members inherited:
+''',
+ c_name=base.c_name())
+ base_members = base.members
+ if variants:
+ base_members = [m for m in base.members
+ if m.name != variants.tag_member.name]
+ ret += gen_struct_members(base_members, name)
+ if not base.is_implicit():
+ ret += mcgen('''
+ // Own members:
+''')
+
+ ret += gen_struct_members(members, name)
+
+ if variants:
+ ret += mcgen('''
+ pub u: %(rs_type)sVariant,
+''', rs_type=rs_name(name))
+ ret += mcgen('''
+}
+''')
+ return ret
+
+
+def gen_rs_enum(name: str,
+ ifcond: QAPISchemaIfCond,
+ members: List[QAPISchemaEnumMember]) -> str:
+ # append automatically generated _max value
+ enum_members = members + [QAPISchemaEnumMember('_MAX', None)]
+
+ ret = mcgen('''
+
+%(cfg)s
+#[repr(u32)]
+#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, common::TryInto)]
+pub enum %(rs_name)s {
+''',
+ cfg=ifcond.rsgen(),
+ rs_name=rs_name(name))
+
+ for member in enum_members:
+ ret += mcgen('''
+ %(cfg)s
+ %(c_enum)s,
+''',
+ cfg=member.ifcond.rsgen(),
+ c_enum=to_camel_case(rs_name(member.name)))
+ # picked the first, since that's what malloc0 does
+ # but arguably could use _MAX instead, or a qapi annotation
+ default = to_camel_case(rs_name(enum_members[0].name))
+ ret += mcgen('''
+}
+
+%(cfg)s
+impl Default for %(rs_name)s {
+ #[inline]
+ fn default() -> %(rs_name)s {
+ Self::%(default)s
+ }
+}
+''',
+ cfg=ifcond.rsgen(),
+ rs_name=rs_name(name),
+ default=default)
+ return ret
+
+
+def gen_rs_alternate(name: str,
+ ifcond: QAPISchemaIfCond,
+ variants: QAPISchemaVariants) -> str:
+ if name in objects_seen:
+ return ''
+
+ ret = ''
+ objects_seen.add(name)
+
+ ret += mcgen('''
+%(cfg)s
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub enum %(rs_name)s {
+''',
+ cfg=ifcond.rsgen(),
+ rs_name=rs_name(name))
+
+ for var in variants.variants:
+ if var.type.name == 'q_empty':
+ continue
+ is_recursive = has_recursive_type(var.type, name, set())
+ ret += mcgen('''
+ %(cfg)s
+ %(mem_name)s(%(rs_type)s),
+''',
+ cfg=var.ifcond.rsgen(),
+ rs_type=rs_type(var.type.c_unboxed_type(), '', box=is_recursive),
+ mem_name=to_camel_case(rs_name(var.name)))
+
+ ret += mcgen('''
+}
+''')
+ return ret
+
+
+class QAPISchemaGenRsTypeVisitor(QAPISchemaRsVisitor):
+
+ def __init__(self, prefix: str) -> None:
+ super().__init__(prefix, 'qapi-types')
+
+ def visit_begin(self, schema: QAPISchema) -> None:
+ # don't visit the empty type
+ objects_seen.add(schema.the_empty_object_type.name)
+ self._gen.preamble_add(
+ mcgen('''
+// generated by qapi-gen, DO NOT EDIT
+
+#![allow(unexpected_cfgs)]
+#![allow(non_camel_case_types)]
+#![allow(non_upper_case_globals)]
+
+use serde_derive::{Serialize, Deserialize};
+
+use util::qobject::QObject;
+'''))
+
+ def visit_object_type(self,
+ name: str,
+ info: Optional[QAPISourceInfo],
+ ifcond: QAPISchemaIfCond,
+ features: List[QAPISchemaFeature],
+ base: Optional[QAPISchemaObjectType],
+ members: List[QAPISchemaObjectTypeMember],
+ branches: Optional[QAPISchemaVariants]) -> None:
+ if name.startswith('q_'):
+ return
+ self._gen.add(gen_rs_object(name, ifcond, base, members, branches))
+
+ def visit_enum_type(self,
+ name: str,
+ info: Optional[QAPISourceInfo],
+ ifcond: QAPISchemaIfCond,
+ features: List[QAPISchemaFeature],
+ members: List[QAPISchemaEnumMember],
+ prefix: Optional[str]) -> None:
+ self._gen.add(gen_rs_enum(name, ifcond, members))
+
+ def visit_alternate_type(self,
+ name: str,
+ info: Optional[QAPISourceInfo],
+ ifcond: QAPISchemaIfCond,
+ features: List[QAPISchemaFeature],
+ alternatives: QAPISchemaVariants) -> None:
+ self._gen.add(gen_rs_alternate(name, ifcond, alternatives))
+
+
+def gen_rs_types(schema: QAPISchema, output_dir: str, prefix: str,
+ builtins: bool) -> None:
+ # pylint: disable=unused-argument
+ # TODO: builtins?
+ vis = QAPISchemaGenRsTypeVisitor(prefix)
+ schema.visit(vis)
+ vis.write(output_dir)
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 11/14] scripts/qapi: strip trailing whitespaces
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (10 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 10/14] scripts/qapi: generate high-level Rust bindings Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 12/14] scripts/rustc_args: add --no-strict-cfg Paolo Bonzini
` (2 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
From: Marc-André Lureau <marcandre.lureau@redhat.com>
This help workaround a rustfmt issue.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Link: https://lore.kernel.org/r/20210907121943.3498701-16-marcandre.lureau@redhat.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
scripts/qapi/gen.py | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/scripts/qapi/gen.py b/scripts/qapi/gen.py
index 0c9b8db3b02..c9721545ea7 100644
--- a/scripts/qapi/gen.py
+++ b/scripts/qapi/gen.py
@@ -58,7 +58,11 @@ def add(self, text: str) -> None:
self._body += text
def get_content(self) -> str:
- return self._top() + self._preamble + self._body + self._bottom()
+ content = self._top() + self._preamble + self._body + self._bottom()
+ # delete trailing white-spaces (working around
+ # https://github.com/rust-lang/rustfmt/issues/4248)
+ content = re.sub(r'\s+$', '\n', content, 0, re.M)
+ return content
def _top(self) -> str:
# pylint: disable=no-self-use
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 12/14] scripts/rustc_args: add --no-strict-cfg
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (11 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 11/14] scripts/qapi: strip trailing whitespaces Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 13/14] rust/util: build QAPI types Paolo Bonzini
2025-10-01 8:00 ` [PATCH 14/14] rust: start qapi tests Paolo Bonzini
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
From: Marc-André Lureau <marcandre.lureau@redhat.com>
Allow to generate all --cfg flags, regardless of Cargo.toml content.
We can't easily list and include all the features used by QAPI types.
Access via #[cfg()] then requires #![allow(unexpected_cfgs)].
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
scripts/rust/rustc_args.py | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/scripts/rust/rustc_args.py b/scripts/rust/rustc_args.py
index 63b0748e0d3..c70b95b8bed 100644
--- a/scripts/rust/rustc_args.py
+++ b/scripts/rust/rustc_args.py
@@ -116,7 +116,7 @@ def generate_lint_flags(cargo_toml: CargoTOML, strict_lints: bool) -> Iterable[s
yield from lint.flags
-def generate_cfg_flags(header: str, cargo_toml: CargoTOML) -> Iterable[str]:
+def generate_cfg_flags(header: str, cargo_toml: Optional[CargoTOML]) -> Iterable[str]:
"""Converts defines from config[..].h headers to rustc --cfg flags."""
with open(header, encoding="utf-8") as cfg:
@@ -125,8 +125,9 @@ def generate_cfg_flags(header: str, cargo_toml: CargoTOML) -> Iterable[str]:
cfg_list = []
for cfg in config:
name = cfg[0]
- if f'cfg({name})' not in cargo_toml.check_cfg:
- continue
+ if cargo_toml:
+ if f'cfg({name})' not in cargo_toml.check_cfg:
+ continue
if len(cfg) >= 2 and cfg[1] != "1":
continue
cfg_list.append("--cfg")
@@ -194,6 +195,13 @@ def main() -> None:
help="apply stricter checks (for nightly Rust)",
default=False,
)
+ parser.add_argument(
+ "--no-strict-cfg",
+ help="only generate expected cfg",
+ action="store_false",
+ dest="strict_cfg",
+ default=True,
+ )
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
@@ -224,7 +232,7 @@ def main() -> None:
print(f'cfg(feature,values("{feature}"))')
for header in args.config_headers:
- for tok in generate_cfg_flags(header, cargo_toml):
+ for tok in generate_cfg_flags(header, cargo_toml if args.strict_cfg else None):
print(tok)
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 13/14] rust/util: build QAPI types
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (12 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 12/14] scripts/rustc_args: add --no-strict-cfg Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
2025-10-01 8:00 ` [PATCH 14/14] rust: start qapi tests Paolo Bonzini
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
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/wrapper.h | 1 +
qapi/meson.build | 6 ++++++
rust/Cargo.lock | 1 +
rust/util/Cargo.toml | 1 +
rust/util/meson.build | 18 +++++++++++++++++-
rust/util/src/qobject/mod.rs | 9 +++++++++
6 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/rust/util/wrapper.h b/rust/util/wrapper.h
index 0907dd59142..c88820a5e5b 100644
--- a/rust/util/wrapper.h
+++ b/rust/util/wrapper.h
@@ -37,3 +37,4 @@ typedef enum memory_order {
#include "qobject/qobject.h"
#include "qobject/qlist.h"
#include "qobject/qdict.h"
+#include "qobject/qjson.h"
diff --git a/qapi/meson.build b/qapi/meson.build
index ca6b61a608d..5d4b55b47f8 100644
--- a/qapi/meson.build
+++ b/qapi/meson.build
@@ -129,3 +129,9 @@ foreach output : qapi_outputs
util_ss.add(qapi_files[i])
i = i + 1
endforeach
+
+qapi_rs_files = custom_target('QAPI Rust',
+ output: 'qapi-types.rs',
+ input: [ files('qapi-schema.json') ],
+ command: [ qapi_gen, '-o', 'qapi', '-b', '@INPUT0@', '-B', 'qapi.backend.QAPIRsBackend' ],
+ depend_files: [ qapi_inputs, qapi_gen_depends ])
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index c1075e11d6c..991533d92c1 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -447,6 +447,7 @@ dependencies = [
"glib-sys",
"libc",
"serde",
+ "serde_derive",
]
[[package]]
diff --git a/rust/util/Cargo.toml b/rust/util/Cargo.toml
index 554004816eb..9f6c52c5acd 100644
--- a/rust/util/Cargo.toml
+++ b/rust/util/Cargo.toml
@@ -18,6 +18,7 @@ foreign = { workspace = true }
glib-sys = { workspace = true }
libc = { workspace = true }
serde = { workspace = true }
+serde_derive = { workspace = true }
common = { path = "../common" }
[lints]
diff --git a/rust/util/meson.build b/rust/util/meson.build
index 45366d03786..a2d0e6ded0d 100644
--- a/rust/util/meson.build
+++ b/rust/util/meson.build
@@ -50,7 +50,7 @@ _util_rs = static_library(
dependencies: [anyhow_rs, libc_rs, foreign_rs, glib_sys_rs, common_rs, serde_rs, qom, qemuutil],
)
-util_rs = declare_dependency(link_with: [_util_rs], dependencies: [qemuutil, qom])
+util_rs = declare_dependency(link_with: [_util_rs], dependencies: [])
rust.test('rust-util-tests', _util_rs,
dependencies: [qemuutil, qom],
@@ -64,3 +64,19 @@ rust.doctest('rust-util-rs-doctests',
dependencies: util_rs,
suite: ['doc', 'rust']
)
+
+_qapi_cfg = run_command(rustc_args,
+ '--no-strict-cfg',
+ '--config-headers', config_host_h,
+ capture: true, check: true).stdout().strip().splitlines()
+
+_qapi_rs = static_library(
+ 'qapi',
+ qapi_rs_files,
+ rust_args: _qapi_cfg,
+ override_options: ['rust_std=2021', 'build.rust_std=2021'],
+ rust_abi: 'rust',
+ dependencies: [common_rs, util_rs, serde_rs],
+)
+
+qapi_rs = declare_dependency(link_with: [_qapi_rs])
diff --git a/rust/util/src/qobject/mod.rs b/rust/util/src/qobject/mod.rs
index b6e86f11a64..f2618c86473 100644
--- a/rust/util/src/qobject/mod.rs
+++ b/rust/util/src/qobject/mod.rs
@@ -13,6 +13,7 @@
mod serializer;
use core::fmt::{self, Debug, Display};
+use foreign::prelude::*;
use std::{
cell::UnsafeCell,
ffi::{c_char, CString},
@@ -104,6 +105,14 @@ fn refcnt(&self) -> &AtomicUsize {
let qobj = self.0.get();
unsafe { AtomicUsize::from_ptr(addr_of_mut!((*qobj).base.refcnt)) }
}
+
+ pub fn to_json(&self) -> String {
+ let qobj = self.0.get();
+ unsafe {
+ let json = bindings::qobject_to_json(qobj);
+ glib_sys::g_string_free(json, glib_sys::GFALSE).into_native()
+ }
+ }
}
impl From<()> for QObject {
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH 14/14] rust: start qapi tests
[not found] <20251001075005.1041833-1-pbonzini@redhat.com>
` (13 preceding siblings ...)
2025-10-01 8:00 ` [PATCH 13/14] rust/util: build QAPI types Paolo Bonzini
@ 2025-10-01 8:00 ` Paolo Bonzini
14 siblings, 0 replies; 16+ messages in thread
From: Paolo Bonzini @ 2025-10-01 8:00 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-rust, armbru, marcandre.lureau
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/tests/meson.build | 10 +++++++---
rust/tests/tests/integration.rs | 2 ++
rust/tests/tests/qapi.rs | 35 +++++++++++++++++++++++++++++++++
3 files changed, 44 insertions(+), 3 deletions(-)
create mode 100644 rust/tests/tests/integration.rs
create mode 100644 rust/tests/tests/qapi.rs
diff --git a/rust/tests/meson.build b/rust/tests/meson.build
index 00688c66fb1..c36cab1886e 100644
--- a/rust/tests/meson.build
+++ b/rust/tests/meson.build
@@ -1,11 +1,15 @@
test('rust-integration',
executable(
'rust-integration',
- files('tests/vmstate_tests.rs'),
+ files(
+ 'tests/integration.rs',
+ 'tests/vmstate_tests.rs',
+ 'tests/qapi.rs',
+ ),
override_options: ['rust_std=2021', 'build.rust_std=2021'],
- rust_args: ['--test'],
+ rust_args: ['--test'] + _qapi_cfg,
install: false,
- dependencies: [bql_rs, common_rs, util_rs, migration_rs, qom_rs]),
+ dependencies: [bql_rs, common_rs, util_rs, migration_rs, qom_rs, qapi_rs]),
args: [
'--test', '--test-threads', '1',
'--format', 'pretty',
diff --git a/rust/tests/tests/integration.rs b/rust/tests/tests/integration.rs
new file mode 100644
index 00000000000..ebc17cb5550
--- /dev/null
+++ b/rust/tests/tests/integration.rs
@@ -0,0 +1,2 @@
+mod qapi;
+mod vmstate_tests;
diff --git a/rust/tests/tests/qapi.rs b/rust/tests/tests/qapi.rs
new file mode 100644
index 00000000000..3a54d37edaa
--- /dev/null
+++ b/rust/tests/tests/qapi.rs
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#![allow(unexpected_cfgs)]
+
+use qapi;
+use util::qobject::{from_qobject, to_qobject};
+
+#[test]
+fn test_qapi() {
+ let sa = qapi::InetSocketAddress {
+ host: "host-val".to_string(),
+ port: "port-val".to_string(),
+ numeric: None,
+ to: None,
+ ipv4: None,
+ ipv6: None,
+ keep_alive: None,
+ #[cfg(HAVE_TCP_KEEPCNT)]
+ keep_alive_count: None,
+ #[cfg(HAVE_TCP_KEEPIDLE)]
+ keep_alive_idle: Some(42),
+ #[cfg(HAVE_TCP_KEEPINTVL)]
+ keep_alive_interval: None,
+ #[cfg(HAVE_IPPROTO_MPTCP)]
+ mptcp: None,
+ };
+
+ // let qi: QObject = 32.into();
+ // dbg!(&qi);
+
+ let qsa = to_qobject(&sa).unwrap();
+ let _json = qsa.to_json();
+ let sa: qapi::InetSocketAddress = from_qobject(qsa).unwrap();
+ dbg!(sa);
+}
--
2.51.0
^ permalink raw reply related [flat|nested] 16+ messages in thread