Re: [PATCH v2 00/16] rust: QObject and QAPI bindings
Markus Armbruster <[email protected]> Thu, 19 Feb 2026 14:39:42 +0100
| Newsgroups | org.nongnu.qemu-rust,org.nongnu.qemu-devel |
|---|---|
| Message-ID | <[email protected]> |
Paolo Bonzini <[email protected]> writes: > 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. To and from generated Rust structs, I presume. > 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). Prior discussion of some aspects: Subject: Re: [PATCH preview 0/3] reviving minimal QAPI generation from 2021 Date: Mon, 23 Jun 2025 14:52:46 +0200 Message-ID: <[email protected]> https://lore.kernel.org/qemu-devel/[email protected]/ > The serde format implementation was co-authored by me and Marc-André. > Marc-André did all the bug fixing and integration testing. > > As an example of how this would be used, the marshaling functions for > QMP commands would look like this: > > fn qmp_marshal_query_stats(args: *mut QDict, > retp: *mut *mut QObject, errp: *mut *mut Error) > { > let qobj = unsafe { QObject::cloned_from_raw(args.cast()) }; > > let result = from_qobject::<StatsFilter>(qobj) > .map_err(anyhow::Error::from) > .and_then(qmp_query_stats) > .and_then(|ret| to_qobject::<Vec<StatsResult>>(ret).map_err(anyhow::Error::from)); > > match qmp_marshal_query_stats_rs(qobj) { > Ok(ret) => unsafe { *retp = ret.into_raw(); }, > Err(e) => unsafe { crate::Error::from(e).propagate(errp) }, > } > } Scoring 0.23 UOUPNBL (uses of unsafe per non-blank line). SCNR! > Despite the long v1->v2, the changes are mostly cosmetic; there are a few > small differences in the detection of types to be wrapped with Box<>, > because Vec<> is now considered to break cycles. > > Available at branch rust-next of https://gitlab.com/bonzini/qemu (which > is now almost empty other than this series, yay). > > Paolo