reviving minimal QAPI generation from 2021
Subsystems (2)
cover reviving minimal QAPI generation from 2021
This is just an extremely minimal extraction from the patches at
https://patchew.org/QEMU/[email protected]/,
limited to generating structs and enums from the QAPI schema. It does not include them in any crate and does not compile them.
While I'm not going to work on this, I was curious how much work it was to produce *some* kind of Rust QAPI struct, which could be a first step towards using serde as an interface to C visitors, like this:
trait QapiType: FreeForeign {
unsafe fn visit(v: bindings::Visitor, name: *const c_char, obj: *mut <Self as FreeForeign>::Foreign, errp: *mut *mut bindings::Error);
}fn to_c<T: QAPIType>(obj: &T) -> *mut <T as FreeForeign>::Foreign {
let mut ptr = libc::calloc(...);
let mut ser = QapiSerializer::<T>::new(ptr);
obj.serialize(&mut ser).unwrap();
ptr.cast()
}unsafe fn from_c<T: QAPIType>(obj: *const <T as FreeForeign>::Foreign) -> T {
let mut de = QapiDeserializer::new(T::visit, obj as *const c_void);
let value = de::Deserialize::deserialize(&mut de).unwrap();
de.end().unwrap();
value
}/* Generated code below: */
impl QapiType for UefiVariable {
unsafe fn visit(v: bindings::Visitor, name: *const c_char, obj: *mut bindings::UefiVariable, errp: *mut *mut bindings::Error) {
unsafe extern "C" visit_type_DumpGuestMemoryFormat(v: bindings::Visitor, name: *const c_char, obj: *mut bindings::UefiVariable, errp: *mut *mut bindings::Error) {
unsafe { visit_type_DumpGuestMemoryFormat(v, name, obj, errp); }
}
}impl FreeForeign for UefiVariable {
type Foreign = bindings::UefiVariable;
unsafe fn free_foreign(p: *mut bindings::UefiVariable) {
unsafe extern "C" qapi_free_UefiVariable(p: *mut bindings::UefiVariable);
unsafe { qapi_free_UefiVariable(p); }
}
}impl CloneToForeign for UefiVariable {
fn clone_to_foreign(&self) -> OwnedPointer<Self> {
OwnedPointer::new(qapi::to_c(self))
}
}impl FromForeign for UefiVariable {
unsafe fn cloned_from_foreign(obj: *const bindings::UefiVariable) -> Self {
qapi::from_c(obj)
}
}The FFI types could be generated by qapi-gen, as in Marc-André's proposal, or from bindgen.
I am not sure what approach is better---whether to use serde or to automatically generate the marshaling and unmarshaling code; and whether to use bindgen or generate C-compatible FFI types---but it made sense, from the point of view of extracting "some" code from Marc-André's proof of concept and enticing other people, :) to start from high-level types.
Paolo
Marc-André Lureau (2):
scripts/qapi: add QAPISchemaIfCond.rsgen()
scripts/qapi: generate high-level Rust bindings
Paolo Bonzini (1):
rust: make TryFrom macro more resilient
meson.build | 4 +- rust/qemu-api-macros/src/lib.rs | 7 +- scripts/qapi/backend.py | 4 +- scripts/qapi/common.py | 16 ++ scripts/qapi/main.py | 4 +- scripts/qapi/rs.py | 183 ++++++++++++++++++ scripts/qapi/rs_types.py | 320 ++++++++++++++++++++++++++++++++ scripts/qapi/schema.py | 4 + 8 files changed, 535 insertions(+), 7 deletions(-) create mode 100644 scripts/qapi/rs.py create mode 100644 scripts/qapi/rs_types.py
Signature · 1 line
2.49.0
PATCH 1/3 rust: make TryFrom macro more resilient
If the enum includes values such as "Ok", "Err", or "Error", the TryInto macro can cause errors. Be careful and qualify identifiers with the full path, or in the case of TryFrom<>::Error do not use the associated type at all.
--- rust/qemu-api-macros/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
rust/qemu-api-macros/src/lib.rs +4 -3
@@ -203,8 +203,8 @@ fn derive_tryinto_body(Ok (quote! { #(const #discriminants: #repr = #name::#discriminantsas #repr ;)*;match value { - #(#discriminants =>Ok (#name::#discriminants),)* - _ =>Err (value), + #(#discriminants => core::result::Result ::Ok (#name::#discriminants),)* + _ => core::result::Result ::Err (value), } }) }
@@ -236,7 +236,8 @@ pub const fn from_bits(value: #repr) -> Self { impl core::convert::TryFrom<#repr> for #name { type Error = #repr; - fn try_from(value: #repr) -> Result<Self, Self::Error> { +#[allow(ambiguous_associated_items)] + fn try_from(value: #repr) -> Result<Self, #repr> { #body } }
Signature · 1 line
2.49.0
Re: [PATCH 1/3] rust: make TryFrom macro more resilient
Quoted 10 lines · Paolo Bonzini
Date: Thu, 5 Jun 2025 12:11:22 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/3] rust: make TryFrom macro more resilient
X-Mailer: git-send-email 2.49.0If the enum includes values such as "Ok", "Err", or "Error", the TryInto macro can cause errors. Be careful and qualify identifiers with the full path, or in the case of TryFrom<>::Error do not use the associated type at all.
Quoted 4 lines
Signed-off-by Paolo Bonzini <[email protected]>--- rust/qemu-api-macros/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
Though I'm late...
rust/qemu-api-macros/src/lib.rs fn derive_tryinto_body( · line 207
Quoted 12 lines
rust/qemu-api-macros/src/lib.rs +2
-2@@ -203,8 +203,8 @@ fn derive_tryinto_body(Ok (quote! { #(const #discriminants: #repr = #name::#discriminantsas #repr ;)*;match value { - #(#discriminants =>Ok (#name::#discriminants),)* - _ =>Err (value), + #(#discriminants => core::result::Result ::Ok (#name::#discriminants),)* + _ => core::result::Result ::Err (value),
error.rs is using std::result, so I think it's better to use std rather than core if possible.
But there are many other cases like this where std and core are mixed, maybe we can clean it up to always prefer the std.
Re: [PATCH 1/3] rust: make TryFrom macro more resilient
In reply to Paolo BonziniQuoted 7 lines · Paolo Bonzini
If the enum includes values such as "Ok", "Err", or "Error", the TryInto macro can cause errors. Be careful and qualify identifiers with the full path, or in the case of TryFrom<>::Error do not use the associated type at all.
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 35 lines
--- rust/qemu-api-macros/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)rust/qemu-api-macros/src/ renamed from rust/qemu-api-macros/src/lib.rs +0
-0
lib.rs
index103470785e3..c18bb4e036f100644rust/qemu-api-macros/src/lib.rs +1
-2@@ -203,8 +203,8 @@ fn derive_tryinto_body(Ok (quote! { #(const #discriminants: #repr = #name::#discriminantsas #repr ;)*;match value { - #(#discriminants =>Ok (#name::#discriminants),)* - _ =>Err (value), + #(#discriminants =>core::result::Result::Ok(#name::#discriminants),)*
rust/qemu-api-macros/src/lib.rs +3
-2+ _ => core::result::Result::Err(value), } }) }@@ -236,7 +236,8 @@ pub const fn from_bits(value: #repr) -> Self { impl core::convert::TryFrom<#repr> for #name { type Error = #repr; - fn try_from(value: #repr) -> Result<Self, Self::Error> { +#[allow(ambiguous_associated_items)] + fn try_from(value: #repr) -> Result<Self, #repr> { #body } } --2.49.0
PATCH 2/3 scripts/qapi: add QAPISchemaIfCond.rsgen()
In reply to Paolo BonziniFrom: Marc-André Lureau <[email protected]>
Generate Rust #[cfg(...)] guards from QAPI 'if' conditions.
--- scripts/qapi/common.py | 16 ++++++++++++++++ scripts/qapi/schema.py | 4 ++++ 2 files changed, 20 insertions(+)
scripts/qapi/common.py +16 -0
@@ -199,6 +199,22 @@ def guardend(name: str) -> str: name=c_fname(name).upper()) +def rsgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str: + + def cfg(ifcond: Union[str, Dict[str, Any]]): + 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:
scripts/qapi/schema.py +4 -0
@@ -39,6 +39,7 @@ docgen_ifcond, gen_endif, gen_if, + rsgen_ifcond, ) from .error import QAPIError, QAPISemError, QAPISourceError from .expr import check_exprs
@@ -65,6 +66,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)
Signature · 1 line
2.49.0
Re: [PATCH 2/3] scripts/qapi: add QAPISchemaIfCond.rsgen()
Hi
Quoted 18 lines · Paolo Bonzini
From: Marc-André Lureau <[email protected]>
Generate Rust #[cfg(...)] guards from QAPI 'if' conditions.
Signed-off-by Marc-André Lureau <[email protected]>Link:
https://lore.kernel.org/r/[email protected]Signed-off-by Paolo Bonzini <[email protected]>--- scripts/qapi/common.py | 16 ++++++++++++++++ scripts/qapi/schema.py | 4 ++++ 2 files changed, 20 insertions(+)scripts/qapi/common.py +0
-0
scripts/qapi/common.py def guardend(name: str) -> str: · line 203
Quoted 5 lines
scripts/qapi/common.py +1
-0@@ -199,6 +199,22 @@ def guardend(name: str) -> str: name=c_fname(name).upper()) +def rsgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str:
def rsgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str:
to make type checking happy.
+
Quoted 43 lines
scripts/qapi/common.py +14
-0+ def cfg(ifcond: Union[str, Dict[str, Any]]): + 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:scripts/qapi/schema.py +4
-1@@ -39,6 +39,7 @@ docgen_ifcond, gen_endif, gen_if, + rsgen_ifcond, ) from .error import QAPIError, QAPISemError, QAPISourceError from .expr import check_exprs@@ -65,6 +66,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.49.0
PATCH 3/3 scripts/qapi: generate high-level Rust bindings
In reply to Paolo BonziniFrom: Marc-André Lureau <[email protected]>
Generate high-level idiomatic Rust code for the QAPI types, with to/from translations for the C FFI.
- 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.
--- meson.build | 4 +- scripts/qapi/backend.py | 4 +- scripts/qapi/main.py | 4 +- scripts/qapi/rs.py | 183 ++++++++++++++++++++++ scripts/qapi/rs_types.py | 320 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 511 insertions(+), 4 deletions(-) create mode 100644 scripts/qapi/rs.py create mode 100644 scripts/qapi/rs_types.py
meson.build +3 -1
@@ -3531,12 +3531,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 = [
scripts/qapi/backend.py +3 -1
@@ -9,6 +9,7 @@ from .introspect import gen_introspect from .schema import QAPISchema from .types import gen_types +from .rs_types import gen_rs_types from .visit import gen_visit
@@ -35,7 +36,7 @@ def generate(self, """ -class QAPICBackend(QAPIBackend): +class QAPICodeBackend(QAPIBackend): def generate(self, schema: QAPISchema,
@@ -55,6 +56,7 @@ def generate(self, :raise QAPIError: On failures. """ + gen_rs_types(schema, output_dir, prefix, builtins) gen_types(schema, output_dir, prefix, builtins) gen_features(schema, output_dir, prefix) gen_visit(schema, output_dir, prefix, builtins)
scripts/qapi/main.py +2 -2
@@ -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:
scripts/qapi/rs.py new file +183 -0
183 lines not on this page · raw message
scripts/qapi/rs_types.py new file +320 -0
320 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH 3/3] scripts/qapi: generate high-level Rust bindings
Quoted 18 lines · Paolo Bonzini
Date: Thu, 5 Jun 2025 12:11:24 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 3/3]scripts/qapi: generate high-level Rust bindings
X-Mailer: git-send-email 2.49.0From: Marc-André Lureau <[email protected]>
Generate high-level idiomatic Rust code for the QAPI types, with to/from translations for the C FFI.
- 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>
Quoted 7 lines
- 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.
Why not map the C union to rust union directly (in pub enum %(rs_name)sVariant)?
(latter comments are all about format nits)
...
Quoted 7 lines
+%(cfg)s
scripts/qapi/rs_types.py new file +6
-0+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),
scripts/qapi/rs_types.py line 56
Quoted 10 lines
scripts/qapi/rs_types.py new file +10
-0+ 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
This introduces extra \n, which will generate a blank line if there's no cfg.
Quoted 1 line
+ %(rs_name)sVariant::%(var_name)s%(patt)s => Self::%(var_name)s,
So, I think it should be:
%(cfg)s %(rs_name)sVariant::%(var_name)s%(patt)s => Self::%(var_name)s,
Quoted 23 lines
+''',
scripts/qapi/rs_types.py new file +22
-0+ 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: Optional[QAPISchemaVariants]) -> str: + ret = mcgen(''' + +%(cfg)s +#[derive(Clone,Debug)] +pub enum %(rs_name)sVariant { +''',
scripts/qapi/rs_types.py line 90
Quoted 10 lines
scripts/qapi/rs_types.py new file +10
-0+ 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,
ditto,
%(cfg)s %(var_name)s,
scripts/qapi/rs_types.py line 100
Quoted 10 lines
+''',
scripts/qapi/rs_types.py new file +9
-0+ 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)s %(var_name)s(%(rs_type)s),
Quoted 18 lines
+''',
scripts/qapi/rs_types.py new file +17
-0+ 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: List[str] = None) -> str: + exclude = exclude or [] + return [f"{m.ifcond.rsgen()} {to_snake_case(rs_name(m.name))}"
scripts/qapi/rs_types.py line 128
Quoted 10 lines
scripts/qapi/rs_types.py new file +10
-0+ for m in members if m.name not in exclude] + + +def gen_struct_members(members: List[QAPISchemaObjectTypeMember]) -> str: + ret = '' + for memb in members: + typ = rs_type(memb.type.c_type(), '', optional=memb.optional, box=True) + ret += mcgen(''' + %(cfg)s + pub %(rs_name)s: %(rs_type)s,
%(cfg)s pub %(rs_name)s: %(rs_type)s,
Quoted 7 lines
+''',
scripts/qapi/rs_types.py new file +6
-0+ cfg=memb.ifcond.rsgen(), + rs_type=typ, + rs_name=to_snake_case(rs_name(memb.name))) + return ret + +
...
scripts/qapi/rs_types.py line 210
Quoted 12 lines
+%(cfg)s
scripts/qapi/rs_types.py new file +11
-0+#[repr(u32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, qemu_api_macros::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)s %(c_enum)s,
Quoted 9 lines
+''',
scripts/qapi/rs_types.py new file +8
-0+ 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(''' +} +
...
Quoted 13 lines
scripts/qapi/rs_types.py new file +13
-0+def gen_rs_alternate(name: str, + ifcond: QAPISchemaIfCond, + variants: Optional[QAPISchemaVariants]) -> str: + if name in objects_seen: + return '' + + ret = '' + objects_seen.add(name) + + ret += mcgen(''' +%(cfg)s +#[derive(Clone, Debug)] +pub enum %(rs_name)s {
scripts/qapi/rs_types.py line 256
Quoted 10 lines
scripts/qapi/rs_types.py new file +10
-0+''', + cfg=ifcond.rsgen(), + rs_name=rs_name(name)) + + for var in variants.variants: + if var.type.name == 'q_empty': + continue + ret += mcgen(''' + %(cfg)s + %(mem_name)s(%(rs_type)s),
%(cfg)s %(mem_name)s(%(rs_type)s),
Quoted 9 lines
+''',
scripts/qapi/rs_types.py new file +8
-0+ cfg=var.ifcond.rsgen(), + rs_type=rs_type(var.type.c_unboxed_type(), ''), + mem_name=to_camel_case(rs_name(var.name))) + + ret += mcgen(''' +} +''') + return ret
Hi Paolo,
Quoted 8 lines · Paolo Bonzini
This is just an extremely minimal extraction from the patches at
https://patchew.org/QEMU/[email protected]/
,
limited to generating structs and enums from the QAPI schema. It does not include them in any crate and does not compile them.
Do you keep an up to date branch for those patches?
I fail to understand the advantage of going through Serde to deserialize/serialize from C when you can have C types in Rust - having less generated code? I also do not fully understand how that would work.
While I'm not going to work on this, I was curious how much work it
Quoted 25 lines
was to produce *some* kind of Rust QAPI struct, which could be a first step towards using serde as an interface to C visitors, like this:
trait QapiType: FreeForeign {
unsafe fn visit(v:bindings::Visitor, name: *constc_char, obj: *mut
<Self as FreeForeign>::Foreign, errp: *mut *mutbindings::Error); }fn to_c<T: QAPIType>(obj: &T) -> *mut <T as FreeForeign>::Foreign { let mut ptr = libc::calloc(...); let mut ser = QapiSerializer::<T>::new(ptr); obj.serialize(&mut ser).unwrap(); ptr.cast() }unsafe fn from_c<T: QAPIType>(obj: *const <T as FreeForeign>::Foreign) ->
T { let mut de = QapiDeserializer::new(T::visit, obj as *const c_void); let value = de::Deserialize::deserialize(&mut de).unwrap(); de.end().unwrap(); value }/* Generated code below: */
Quoted 8 lines
impl QapiType for UefiVariable {
unsafe fn visit(v:bindings::Visitor, name: *constc_char, obj: *mutbindings::UefiVariable, errp: *mut *mutbindings::Error) {
unsafe extern "C" visit_type_DumpGuestMemoryFormat(v:bindings::Visitor, name: *constc_char, obj: *mutbindings::UefiVariable,errp: *mut *mut bindings::Error) { unsafe { visit_type_DumpGuestMemoryFormat(v, name, obj, errp); }
I guess .._UefiVariable.
Quoted 61 lines
} }impl FreeForeign for UefiVariable { type Foreign = bindings::UefiVariable; unsafe fn free_foreign(p: *mut bindings::UefiVariable) { unsafe extern "C" qapi_free_UefiVariable(p: *mut bindings::UefiVariable); unsafe { qapi_free_UefiVariable(p); } } }impl CloneToForeign for UefiVariable { fn clone_to_foreign(&self) -> OwnedPointer<Self> { OwnedPointer::new(qapi::to_c(self)) } }impl FromForeign for UefiVariable { unsafe fn cloned_from_foreign(obj: *const bindings::UefiVariable) -> Self { qapi::from_c(obj) } }The FFI types could be generated by qapi-gen, as in Marc-André's proposal, or from bindgen.
I am not sure what approach is better---whether to use serde or to automatically generate the marshaling and unmarshaling code; and whether to use bindgen or generate C-compatible FFI types---but it made sense, from the point of view of extracting "some" code from Marc-André's proof of concept and enticing other people, :) to start from high-level types.
Paolo
Marc-André Lureau (2):
scripts/qapi: addQAPISchemaIfCond.rsgen()
scripts/qapi: generate high-level Rust bindingsPaolo Bonzini (1):
rust: make TryFrom macro more resilientmeson.build | 4 +- rust/qemu-api-macros/src/lib.rs | 7 +- scripts/qapi/backend.py | 4 +- scripts/qapi/common.py | 16 ++ scripts/qapi/main.py | 4 +- scripts/qapi/rs.py | 183 ++++++++++++++++++ scripts/qapi/rs_types.py | 320 ++++++++++++++++++++++++++++++++ scripts/qapi/schema.py | 4 + 8 files changed, 535 insertions(+), 7 deletions(-) create mode 100644 scripts/qapi/rs.py create mode 100644 scripts/qapi/rs_types.py--
2.49.0
Quoted 7 lines · Marc-André Lureau
Quoted 4 lines · Paolo Bonzini
This is just an extremely minimal extraction from the patches at
https://patchew.org/QEMU/[email protected]/,
limited to generating structs and enums from the QAPI schema. It does not include them in any crate and does not compile them.Do you keep an up to date branch for those patches?
I can add them to my rust-next branch but they can in theory just be applied to upstream/master.
Quoted 1 line
I fail to understand the advantage of going through Serde to deserialize/serialize from C when you can have C types in Rust - having less generated code?
It's indeed trading generated Rust code for generic serializers and deserializers + #[derive(Serializer, Deserializer)]. It's not the primary reason but the size of your code generator patches was a bit scary indeed and I don't want to inflict too much review effort on QAPI maintainers.
But also, I'm not sure that the C types are needed at all for implementing QAPI commands. The QAPI command generator could have a directive like 'rust': 'qga::qmp'" directive (or maybe just 'language': 'rust', I don't know) that would generate Rust marshaling/unmarshaling functions:
pub extern "C" fn qmp_guest_get_host_name(
args: *mut QDict,
ret: *mut *mut QObject,
errp: *mut *mut bindings::Error) {
let args = unsafe { from_qobject::<()>(args) };
match ::qga::qmp::guest_get_host_name(args) {
Ok(ref v) => unsafe { *ret = to_qobject::<GuestHostName>(v); },
Err(err) => unsafe { err.propagate(errp); }
}
}and this (more specifically from_qobject/to_qobject) could also use serde, this time to go directly from QObject to Rust and back. A QObject serializer/deserializer should be easier to write (and very similar to existing code in serde_json for
https://docs.rs/serde_json/latest/serde_json/enum.Value.html), so you could reach the stage of your old posting with qapi-gen changes that are little more than these two patches.
Quoted 1 line
I also do not fully understand how that would work.
To be honest neither do I entirely. But what I understood is that Serde is based on a visitor API that is very similar to what we have in QEMU. See the above serde_json link where it implements Serializer and Deserializer, and the blog post at
https://ohadravid.github.io/posts/2025-05-serde-reflect/ should be a good introduction too.
That said: the *possibility* of using Serde is why I started from the native structs. I'm not wed to it and generating/converting the C types remains a possibility as well.
Paolo
Quoted 41 lines · Paolo Bonzini
Date: Thu, 5 Jun 2025 12:11:21 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH preview 0/3] reviving minimal QAPI generation from 2021
X-Mailer: git-send-email 2.49.0This is just an extremely minimal extraction from the patches at
https://patchew.org/QEMU/[email protected]/,
limited to generating structs and enums from the QAPI schema. It does not include them in any crate and does not compile them.While I'm not going to work on this, I was curious how much work it was to produce *some* kind of Rust QAPI struct, which could be a first step towards using serde as an interface to C visitors, like this:
trait QapiType: FreeForeign { unsafe fn visit(v: bindings::Visitor, name: *const c_char, obj: *mut <Self as FreeForeign>::Foreign, errp: *mut *mut bindings::Error); }fn to_c<T: QAPIType>(obj: &T) -> *mut <T as FreeForeign>::Foreign { let mut ptr = libc::calloc(...); let mut ser = QapiSerializer::<T>::new(ptr); obj.serialize(&mut ser).unwrap(); ptr.cast() }unsafe fn from_c<T: QAPIType>(obj: *const <T as FreeForeign>::Foreign) -> T { let mut de = QapiDeserializer::new(T::visit, obj as *const c_void); let value = de::Deserialize::deserialize(&mut de).unwrap(); de.end().unwrap(); value }/* Generated code below: */
impl QapiType for UefiVariable { unsafe fn visit(v: bindings::Visitor, name: *const c_char, obj: *mut bindings::UefiVariable, errp: *mut *mut bindings::Error) { unsafe extern "C" visit_type_DumpGuestMemoryFormat(v: bindings::Visitor, name: *const c_char, obj: *mut bindings::UefiVariable, errp: *mut *mut bindings::Error) { unsafe { visit_type_DumpGuestMemoryFormat(v, name, obj, errp); } } }
Quoted 2 lines
impl FreeForeign for UefiVariable {
type Foreign = bindings::UefiVariable;My question seems to be different from Marc-André's...
As patch 3 did, qapi will generate Rust 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
It seems we still need some raw bindings (generated by bindgen) as the type Foreign, and then implement Foreign traits for the Rust structures generated by this patch.
For this example, UefiVariable is generated by qapi (patch 3) and bindings::UefiVariable is generated by bindgen. Ah! I feel I'm wrong, could you please correct me?
Thanks,
Zhao
Quoted 28 lines
unsafe fn free_foreign(p: *mut bindings::UefiVariable) { unsafe extern "C" qapi_free_UefiVariable(p: *mut bindings::UefiVariable); unsafe { qapi_free_UefiVariable(p); } } }impl CloneToForeign for UefiVariable { fn clone_to_foreign(&self) -> OwnedPointer<Self> { OwnedPointer::new(qapi::to_c(self)) } }impl FromForeign for UefiVariable { unsafe fn cloned_from_foreign(obj: *const bindings::UefiVariable) -> Self { qapi::from_c(obj) } }The FFI types could be generated by qapi-gen, as in Marc-André's proposal, or from bindgen.
I am not sure what approach is better---whether to use serde or to automatically generate the marshaling and unmarshaling code; and whether to use bindgen or generate C-compatible FFI types---but it made sense, from the point of view of extracting "some" code from Marc-André's proof of concept and enticing other people, :) to start from high-level types.
Quoted 6 lines · Zhao Liu
As patch 3 did, qapi will generate Rust 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>
Yep, so far so good.
Quoted 1 line
- structures have Rust versions, with To/From FFI conversions
The conversion part is not included in this minimal series. The raw structure (the type Foreign as you point out) could be generated by either bindgen or qapi-gen. Marc-André chose the latter for his old prototype, I'm ambivalent.
Quoted 3 lines
It seems we still need some raw bindings (generated by bindgen) as the
type Foreign, and then implement Foreign traits for the Rust structures generated by this patch.
Yes. If using serde the implementation of the traits is very small, and basically the same for all types. If not using serde, it would need some (or most) of the infrastructure in Marc-André's original series.
Quoted 3 lines
For this example, UefiVariable is generated by qapi (patch 3) and
bindings::UefiVariableis generated by bindgen. Ah! I feel I'm wrong, could you please correct me?
You're not wrong. :)
Paolo
Quoted 61 lines · Paolo Bonzini
Quoted 60 lines
Date: Thu, 5 Jun 2025 12:11:21 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH preview 0/3] reviving minimal QAPI generation from 2021
X-Mailer: git-send-email 2.49.0This is just an extremely minimal extraction from the patches at
https://patchew.org/QEMU/[email protected]/,
limited to generating structs and enums from the QAPI schema. It does not include them in any crate and does not compile them.While I'm not going to work on this, I was curious how much work it was to produce *some* kind of Rust QAPI struct, which could be a first step towards using serde as an interface to C visitors, like this:
trait QapiType: FreeForeign { unsafe fn visit(v: bindings::Visitor, name: *const c_char, obj: *mut <Self as FreeForeign>::Foreign, errp: *mut *mut bindings::Error); }fn to_c<T: QapiType>(obj: &T) -> *mut <T as FreeForeign>::Foreign { let mut ptr = libc::calloc(...); let mut ser = QapiSerializer::<T>::new(ptr); obj.serialize(&mut ser).unwrap(); ptr.cast() }unsafe fn from_c<T: QapiType>(obj: *const <T as FreeForeign>::Foreign) -> T { let mut de = QapiDeserializer::new(T::visit, obj as *const c_void); let value = de::Deserialize::deserialize(&mut de).unwrap(); de.end().unwrap(); value }/* Generated code below: */
impl QapiType for UefiVariable { unsafe fn visit(v: bindings::Visitor, name: *const c_char, obj: *mut bindings::UefiVariable, errp: *mut *mut bindings::Error) { unsafe extern "C" visit_type_UefiVariable(v: bindings::Visitor, name: *const c_char, obj: *mut bindings::UefiVariable, errp: *mut *mut bindings::Error) { unsafe { visit_type_UefiVariable(v, name, obj, errp); } } }impl FreeForeign for UefiVariable { type Foreign = bindings::UefiVariable; unsafe fn free_foreign(p: *mut bindings::UefiVariable) { unsafe extern "C" qapi_free_UefiVariable(p: *mut bindings::UefiVariable); unsafe { qapi_free_UefiVariable(p); } } }impl CloneToForeign for UefiVariable { fn clone_to_foreign(&self) -> OwnedPointer<Self> { OwnedPointer::new(qapi::to_c(self)) } }impl FromForeign for UefiVariable { unsafe fn cloned_from_foreign(obj: *const bindings::UefiVariable) -> Self { qapi::from_c(obj) } }
Quoted 4 lines · Paolo Bonzini
Yes. If using serde the implementation of the traits is very small, and basically the same for all types. If not using serde, it would need some (or most) of the infrastructure in Marc-André's original series.
Looking more at it, the Rust->QObject and QObject->Rust parts *can* be done with serde (following the model of serde_json's Value (de)serializer) but the Rust<->C part has a problem.
To recap, Rust->C is the serialization and corresponds to output visitors. C->Rust is the deserialization and corresponds to input visitors.
For serialization, serde has a push model where the generated code looks like this:
let mut state =
Serializer::serialize_struct(serializer, "S", 2);
SerializeStruct::serialize_field(&mut state, "a", &self.a)?;
SerializeStruct::serialize_field(&mut state, "b", &self.b)?;
SerializeStruct::end(state)whereas QAPI has a pull model where visit_type_* drives the process and requests the fields one by one.
For deserialization, serde has a pull model where the generated code asks for the field names one by one:
fn visit_map<__A>(self, mut __map: __A)
while let Some(key) =
MapAccess::next_key::<__Field>(&mut __map)? {
match __key { ... }
}
}whereas QAPI has a push model where visit_type_* again drives the process and sends fields one by one.
For commands this is not a problem because the real underlying transformation is QObject->QObject and the intermediate steps (to and from QObject) can use serde.
However, QOM property getters/setters (especially, but not exclusively, for properties with compound types) remain a problem since these use callbacks with a Visitor* argument. I see three possibilities:
1) everything is done through an intermediate QObject step (e.g. for a
setter: Visitor->QObject with an input visitor, and QObject->Rust with serde deserialization).
+ easy, Rust only sees serde
+ QMP commands use a single conversion step
- inefficient
2) everything is done through an intermediate C step (e.g. for a
setter: Visitor->C with a visit_type_* function, and C->Rust with generated code that does not need to use serde). There is still a double conversion step, but it's more efficient than option 1
+ one framework (visitor)
- double conversion for the QMP commands
- lots of generated code
3) generating a Rust visit_type_* implementation as well, either in qapi-gen (3a) or through a procedural macro (3b). This should not be hard to write but it would remove a lot of the advantages from using serde.
+ efficient
+ preserves single conversion for QMP commands
- two frameworks
I am leaning towards option 1, i.e. keep using serde but only cover conversions to and from QObject. The reason is that one future usecase for Rust in QEMU is the UEFI variable store; that one also has some Rust<->JSON conversions and could be served by either QObject or serde_json. Either way, it'd be nice for the UEFI variable store to remain within the Rust serde ecosystem and allow sharing code between QEMU and Coconut SVSM. But I'm not so sure...
Paolo
Quoted 52 lines · Paolo Bonzini
Date: Thu, 12 Jun 2025 12:24:44 +0200
From: Paolo Bonzini <[email protected]>
Subject: Re: [PATCH preview 0/3] reviving minimal QAPI generation from 2021Quoted 4 lines · Paolo Bonzini
Yes. If using serde the implementation of the traits is very small, and basically the same for all types. If not using serde, it would need some (or most) of the infrastructure in Marc-André's original series.
Looking more at it, the
Rust->QObjectandQObject->Rustparts *can* be done with serde (following the model of serde_json's Value (de)serializer) but the Rust<->C part has a problem.To recap,
Rust->Cis the serialization and corresponds to output visitors.C->Rustis the deserialization and corresponds to input visitors.For serialization, serde has a push model where the generated code looks like this:
let mut state = Serializer::serialize_struct(serializer, "S", 2); SerializeStruct::serialize_field(&mut state, "a", &self.a)?; SerializeStruct::serialize_field(&mut state, "b", &self.b)?; SerializeStruct::end(state)whereas QAPI has a pull model where visit_type_* drives the process and requests the fields one by one.
For deserialization, serde has a pull model where the generated code asks for the field names one by one:
fn visit_map<__A>(self, mut __map: __A) while let Some(key) = MapAccess::next_key::<__Field>(&mut __map)? { match __key { ... } } }whereas QAPI has a push model where visit_type_* again drives the process and sends fields one by one.
For commands this is not a problem because the real underlying transformation is
QObject->QObjectand the intermediate steps (to and from QObject) can use serde.However, QOM property getters/setters (especially, but not exclusively, for properties with compound types) remain a problem since these use callbacks with a Visitor* argument. I see three possibilities:
Quoted 6 lines
1) everything is done through an intermediate QObject step (e.g. for a
setter:Visitor->QObjectwith an input visitor, andQObject->Rustwith serde deserialization).
+ easy, Rust only sees serde
+ QMP commands use a single conversion step
- inefficient
This sounds like a natural approach.
Quoted 3 lines
2) everything is done through an intermediate C step (e.g. for a
setter:Visitor->Cwith a visit_type_* function, andC->Rustwith generated code that does not need to use serde).
I understand this step indicates to use bindgen to generate visit_type_* bindings...
Quoted 5 lines
There is still a
double conversion step, but it's more efficient than option 1
+ one framework (visitor)
- double conversion for the QMP commands
- lots of generated code
...if so, then yes, there would be too much generated and "unsafe" codes, and further abstraction may be needed to ensure safety, similar to the lots of work currently done in the qemu-api.
Quoted 7 lines
3) generating a Rust visit_type_* implementation as well, either in qapi-gen (3a) or through a procedural macro (3b).> This should not be hard to write but it would remove a lot of the advantages from using serde.
+ efficient
+ preserves single conversion for QMP commands
- two frameworks
The difference between the two frameworks is that one is for C and the other is for Rust, which increases the maintenance burden.
Quoted 2 lines
I am leaning towards option 1, i.e. keep using serde but only cover conversions to and from QObject.
Based on my understanding of the three options above, I also think that option 1 is the more appropriate approach.
Quoted 6 lines
The reason is that one future usecase
for Rust in QEMU is the UEFI variable store; that one also has some Rust<->JSON conversions and could be served by either QObject orserde_json. Either way, it'd be nice for the UEFI variable store to remain within the Rust serde ecosystem and allow sharing code between QEMU and Coconut SVSM. But I'm not so sure...
Thanks,
Zhao
Quoted 8 lines · Zhao Liu
Quoted 6 lines
1) everything is done through an intermediate QObject step (e.g. for a
setter:Visitor->QObjectwith an input visitor, andQObject->Rustwith serde deserialization).
+ easy, Rust only sees serde
+ QMP commands use a single conversion step
- inefficientThis sounds like a natural approach.
Yep, serde is complex but the result is very natural. In some cases serde has different defaults than QEMU, but attributes like #[serde(skip_serializing_if = "Option::is_none")] can be automatically generated by qapi-gen.
Quoted 6 lines
Quoted 3 lines
2) everything is done through an intermediate C step (e.g. for a
setter:Visitor->Cwith a visit_type_* function, andC->Rustwith generated code that does not need to use serde).I understand this step indicates to use bindgen to generate visit_type_* bindings...
More than that, it's using qapi-gen to generate implementations like the *Foreign traits used by Error. Marc-André wrote his own traits but I suspect a lot of the code could be reused.
So I must give credit to Marc-André that there is a big advantage here, in that a lot of the code is already written and it is simpler than serde.
Paolo
I don't know enough about Rust/serde to give advice. I do know how to make a fool of myself by asking dumb questions.
Quoted 36 lines · Paolo Bonzini
Quoted 4 lines · Paolo Bonzini
Yes. If using serde the implementation of the traits is very small, and basically the same for all types. If not using serde, it would need some (or most) of the infrastructure in Marc-André's original series.
Looking more at it, the
Rust->QObjectandQObject->Rustparts *can* be done with serde (following the model of serde_json's Value (de)serializer) but the Rust<->C part has a problem.To recap,
Rust->Cis the serialization and corresponds to output visitors.C->Rustis the deserialization and corresponds to input visitors.For serialization, serde has a push model where the generated code looks like this:
let mut state = Serializer::serialize_struct(serializer, "S", 2); SerializeStruct::serialize_field(&mut state, "a", &self.a)?; SerializeStruct::serialize_field(&mut state, "b", &self.b)?; SerializeStruct::end(state)whereas QAPI has a pull model where visit_type_* drives the process and requests the fields one by one.
For deserialization, serde has a pull model where the generated code asks for the field names one by one:
fn visit_map<__A>(self, mut __map: __A) while let Some(key) = MapAccess::next_key::<__Field>(&mut __map)? { match __key { ... } } }
Quoted 6 lines
whereas QAPI has a push model where visit_type_* again drives the process and sends fields one by one.
For commands this is not a problem because the real underlying transformation is
QObject->QObjectand the intermediate steps (to and from QObject) can use serde.
Are you talking about commands implemented in Rust?
The existing data flow is roughly like this (I'm simplifying):
1. Parse JSON text into request QObject, pass to QMP core
2. Extract command name string and argument QDict
3. Look up generated command marshaller / unmarshaller, pass argument QDict to it
4. Unmarshall argument QDict with the QObject input visitor and generated visit_type_ARG()
5. Pass the C arguments to the handwritten command handler, receive the C return value
6. Marshall the return value into a QObject with the QObject output visitor and generated visit_type_RET(), return it to QMP core
7. Insert it into a response QObject
8. Unparse response QObject into JSON text
How would a Serde flow look like?
Quoted 3 lines
However, QOM property getters/setters (especially, but not exclusively, for properties with compound types) remain a problem since these use callbacks with a Visitor* argument.
object_property_set() takes the new property value wrapped in an input visitor. The property setter extracts it using visit_type_FOOs() with this input visitor as it sees fit. Ideally, it uses exactly visit_type_PROPTYPE().
object_property_get() takes an output visitor to be wrapped it around the property value. The property getter inserts it using visit_type_FOOs() with this output visitor as it sees fit. Ideally, it uses exactly visit_type_PROPTYPE().
We sometimes use a QObject input / output visitor, and sometimes a string input / output visitor. The latter come with restrictions, and are evolutionary dead ends.
The QObject visitors wrap a QObject, the string visitors wrap a string (d'oh).
Quoted 18 lines
I see three
possibilities:1) everything is done through an intermediate QObject step (e.g. for a
setter:Visitor->QObjectwith an input visitor, andQObject->Rustwith serde deserialization).
+ easy, Rust only sees serde
+ QMP commands use a single conversion step
- inefficient2) everything is done through an intermediate C step (e.g. for a
setter:Visitor->Cwith a visit_type_* function, andC->Rustwith generated code that does not need to use serde). There is still a double conversion step, but it's more efficient than option 1
+ one framework (visitor)
- double conversion for the QMP commands
- lots of generated code
Quoted 7 lines
3) generating a Rust visit_type_* implementation as well, either in qapi-gen (3a) or through a procedural macro (3b). This should not be hard to write but it would remove a lot of the advantages from using serde.
+ efficient
+ preserves single conversion for QMP commands
- two frameworks
I'm afraid this is too terse for ignorant me.
Quoted 9 lines
I am leaning towards option 1, i.e. keep using serde but only cover conversions to and from QObject. The reason is that one future usecase for Rust in QEMU is the UEFI variable store; that one also has some Rust<->JSON conversions and could be served by either QObject or
serde_json. Either way, it'd be nice for the UEFI variable store to remain within the Rust serde ecosystem and allow sharing code between QEMU and Coconut SVSM. But I'm not so sure...Paolo
Il mer 18 giu 2025, 16:25 Markus Armbruster <[email protected]> ha scritto:
Quoted 3 lines
I don't know enough about Rust/serde to give advice. I do know how to make a fool of myself by asking dumb questions.
No dumb questions, only dumb answers.
Quoted 6 lines
For commands this is not a problem because the real underlying
Quoted 2 lines
transformation is
QObject->QObjectand the intermediate steps (to and from QObject) can use serde.Are you talking about commands implemented in Rust?
Yes. I will intersperse your text with the corresponding Rust/serde implementation.
The existing data flow is roughly like this (I'm simplifying):
Quoted 6 lines
1. Parse JSON text into request QObject, pass to QMP core
2. Extract command name string and argument QDict
3. Look up generated command marshaller / unmarshaller, pass argument QDict to it
Same so far since this is C code.
4. Unmarshall argument QDict with the QObject input visitor and
Quoted 2 lines
generated
visit_type_ARG()
Unmarshall with QObject Deserializer, which talks to a serde-generated Deserialize implementation.
5. Pass the C arguments to the handwritten command handler, receive the
Quoted 2 lines
C return value
Same.
6. Marshall the return value into a QObject with the QObject output
Quoted 2 lines
visitor and generated
visit_type_RET(), return it to QMP core
Marshall with the QObject Serializer, which talks to a serde-generated Serialize implementation.
7. Insert it into a response QObject
Quoted 2 lines
8. Unparse response QObject into JSON text
Same.
How would a Serde flow look like?
Quoted 1 line
As described above, visitors are bypassed and the marshalling/unmarshalling works directly at the QObject level.
Implementation-wise the main difference is that 1) the type code (Serialize/Deserialize) is not the same for serialization and desetialization, unlike visit_type_*() 2) the code generation is done by serde instead of qapi-gen and we'd be mostly oblivious to how it works.
The Serializer and Deserializer should be about 1500 lines of Rust + tests, and they would do the functionality of the QObject input and output visitors.
Quoted 14 lines
However, QOM property getters/setters (especially, but not
Quoted 2 lines
exclusively, for properties with compound types) remain a problem since these use callbacks with a Visitor* argument.
object_property_set()takes the new property value wrapped in an input visitor. The property setter extracts it usingvisit_type_FOOs()with this input visitor as it sees fit. Ideally, it uses exactlyvisit_type_PROPTYPE().
object_property_get()takes an output visitor to be wrapped it around the property value. The property getter inserts it usingvisit_type_FOOs()with this output visitor as it sees fit. Ideally, it uses exactlyvisit_type_PROPTYPE().
Quoted 7 lines
We sometimes use a QObject input / output visitor, and sometimes a string input / output visitor. The latter come with restrictions, and are evolutionary dead ends.
The QObject visitors wrap a QObject, the string visitors wrap a string (d'oh).
Yep. The string visitor is why we cannot just change getters and setters to use QObject.
In this case, without writing a visit_type_*() implementation that can write to a Rust struct, an intermediate QObject would be the only way to turn a Visitor into a Rust data type. So I can imagine three ways to operate:
* Keep using serde like for commands: in the callback that is invoked by object_property_set() do Visitor->QObject->setter (yes that means double conversion when the source visitor is and QObject visitor) or for the getter case, getter->QObject->Visitor. This has the minimum amount of code added to qapi-gen.
* Generate a visit_type_*() implementation that emits a Rust struct (i.e. one that maps for example 'str' to a String and not a *mut c_char) and forgo serde completely. Use this generated implementation everywhere: QOM getters and setters, as well as QMP commands. This is how C code works.
* Generate rust->C (e.g. String->*mut c_char) and C->rust converters from qapi-gen; use the existing C visit_type_*() to extract data from visitors and then apply said converters to turn the data into a Rust struct, and likewise in the other direction. This was the way Marc-André's prototype worked.
I'm afraid this is too terse for ignorant me.
Quoted 1 line
I tried to translate that. :)
Paolo
Quoted 11 lines
Quoted 9 lines
I am leaning towards option 1, i.e. keep using serde but only cover conversions to and from QObject. The reason is that one future usecase for Rust in QEMU is the UEFI variable store; that one also has some Rust<->JSON conversions and could be served by either QObject or
serde_json. Either way, it'd be nice for the UEFI variable store to remain within the Rust serde ecosystem and allow sharing code between QEMU and Coconut SVSM. But I'm not so sure...Paolo
Quoted 54 lines · Paolo Bonzini
Il mer 18 giu 2025, 16:25 Markus Armbruster <[email protected]> ha scritto:
Quoted 3 lines
I don't know enough about Rust/serde to give advice. I do know how to make a fool of myself by asking dumb questions.
No dumb questions, only dumb answers.
Quoted 6 lines
For commands this is not a problem because the real underlying
Quoted 2 lines
transformation is
QObject->QObjectand the intermediate steps (to and from QObject) can use serde.Are you talking about commands implemented in Rust?
Yes. I will intersperse your text with the corresponding Rust/serde implementation.
Quoted 9 lines
The existing data flow is roughly like this (I'm simplifying):
1. Parse JSON text into request QObject, pass to QMP core
2. Extract command name string and argument QDict
3. Look up generated command marshaller / unmarshaller, pass argument QDict to it
Same so far since this is C code.
Quoted 2 lines
4. Unmarshall argument QDict with the QObject input visitor and generated
visit_type_ARG()Unmarshall with QObject Deserializer, which talks to a serde-generated Deserialize implementation.
Quoted 3 lines
5. Pass the C arguments to the handwritten command handler, receive the C return value
Same.
Quoted 3 lines
6. Marshall the return value into a QObject with the QObject output visitor and generated
visit_type_RET(), return it to QMP coreMarshall with the QObject Serializer, which talks to a serde-generated Serialize implementation.
Quoted 4 lines
7. Insert it into a response QObject
8. Unparse response QObject into JSON text
Quoted 7 lines
Same.
Quoted 3 lines
How would a Serde flow look like?
As described above, visitors are bypassed and the marshalling/unmarshalling works directly at the QObject level.
We use the same code (handwritten QMP core) up to QObject and from QObject on.
In between, we use QObject input/output visitors with generated C data types for commands implemented in C, and Serde-generated deserializers/serializers with generated Rust data types for commands implemented in Rust.
Correct?
Quoted 8 lines
Implementation-wise the main difference is that 1) the type code (Serialize/Deserialize) is not the same for serialization and desetialization, unlike visit_type_*() 2) the code generation is done by serde instead of qapi-gen and we'd be mostly oblivious to how it works.
The Serializer and Deserializer should be about 1500 lines of Rust + tests, and they would do the functionality of the QObject input and output visitors.
We end up with two separate ways to do the same job, which is kind of sad.
I gather the alternative would be to use generated QAPI visitors for the generated Rust data types instead of Serde. This would be unusual for Rust code: Serde exists and works, so reinvent it would be wasteful and dumb.
Gut feeling: sticking to how things are usually done (here: with Serde) is the more prudent choice.
Quoted 18 lines
Quoted 18 lines
Quoted 3 lines
However, QOM property getters/setters (especially, but not exclusively, for properties with compound types) remain a problem since these use callbacks with a Visitor* argument.
object_property_set()takes the new property value wrapped in an input visitor. The property setter extracts it usingvisit_type_FOOs()with this input visitor as it sees fit. Ideally, it uses exactlyvisit_type_PROPTYPE().
object_property_get()takes an output visitor to be wrapped it around the property value. The property getter inserts it usingvisit_type_FOOs()with this output visitor as it sees fit. Ideally, it uses exactlyvisit_type_PROPTYPE().We sometimes use a QObject input / output visitor, and sometimes a string input / output visitor. The latter come with restrictions, and are evolutionary dead ends.
Quoted 6 lines
Quoted 3 lines
The QObject visitors wrap a QObject, the string visitors wrap a string (d'oh).
Yep. The string visitor is why we cannot just change getters and setters to use QObject.
The string visitors have long been a thorn in my side.
I wish QOM wasn't so annoyingly flexible. I wish a property had to be of QAPI type (possibly complex). Less headaches. Less boilerplate, too.
Almost all QOM properties are. And the few that aren't feel like bad ideas to me.
Quoted 10 lines
In this case, without writing a visit_type_*() implementation that can write to a Rust struct, an intermediate QObject would be the only way to turn a Visitor into a Rust data type. So I can imagine three ways to operate:
* Keep using serde like for commands: in the callback that is invoked by
object_property_set()doVisitor->QObject->setter(yes that means double conversion when the source visitor is and QObject visitor) or for the getter case,getter->QObject->Visitor. This has the minimum amount of code added to qapi-gen.
Recall the callback gets the new property value wrapped in an input visitor. Whereas a C setter extracts it into some C variable(s), a Rust setter extracts it into a QObject, which it then passes to Serde for deserialization into Rust variables. Correct?
Quoted 4 lines
* Generate a visit_type_*() implementation that emits a Rust struct (i.e. one that maps for example 'str' to a String and not a *mut
c_char) and forgo serde completely. Use this generated implementation everywhere: QOM getters and setters, as well as QMP commands. This is how C code works.
This is the alternative mentioned above.
Quoted 5 lines
* Generate
rust->C(e.g. String->*mutc_char) andC->rustconverters from qapi-gen; use the existing C visit_type_*() to extract data from visitors and then apply said converters to turn the data into a Rust struct, and likewise in the other direction. This was the way Marc-André's prototype worked.
Converters between C and Rust data types let us cut at the C data type instead of at QObject.
We use the same code up to QAPI-generated C data type and from QAPI-generated C data type on.
In between, we work directly with the C data type for properties implemented in C, and convert to and from the Rust data type for properties implemented in Rust.
Correct?
The simplest such converters convert via QObject. Grossly inefficient :)
Marc-André's prototype demonstrates efficient converters can be had. The question is whether they're worth their keep.
Quoted 4 lines
Quoted 2 lines
I'm afraid this is too terse for ignorant me.
I tried to translate that. :)
Thank you!
Il lun 23 giu 2025, 08:53 Markus Armbruster <[email protected]> ha scritto:
Quoted 8 lines
We end up with two separate ways to do the same job, which is kind of sad.
I gather the alternative would be to use generated QAPI visitors for the generated Rust data types instead of Serde. This would be unusual for Rust code: Serde exists and works, so reinvent it would be wasteful and dumb.
My feeling as well.
The string visitors have long been a thorn in my side.
Quoted 8 lines
I wish QOM wasn't so annoyingly flexible. I wish a property had to be of QAPI type (possibly complex). Less headaches. Less boilerplate, too.
Almost all QOM properties are. And the few that aren't feel like bad ideas to me.
Makes sense. On the other hand, from the C point of view I admit that passing a Visitor* or a QObject* to the setter would not be very different—or possibly even a bit more code for the QObject*. Likewise for passing a QObject** on the getter side.
So the problem isn't the flexibility... It's using/exploiting it.
Quoted 4 lines
* Keep using serde like for commands: in the callback that is invoked by
Quoted 3 lines
object_property_set()doVisitor->QObject->setter(yes that means double conversion when the source visitor is and QObject visitor) or for the getter case,getter->QObject->Visitor. This has the minimum amount of
Quoted 8 lines
code
Quoted 1 line
added to qapi-gen.
Recall the callback gets the new property value wrapped in an input visitor. Whereas a C setter extracts it into some C variable(s), a Rust setter extracts it into a QObject, which it then passes to Serde for deserialization into Rust variables. Correct?
Yes.
Quoted 7 lines
* Generate a visit_type_*() implementation that emits a Rust struct (i.e.
Quoted 3 lines
one that maps for example 'str' to a String and not a *mut
c_char) and forgo serde completely. Use this generated implementation everywhere: QOM getters and setters, as well as QMP commands. This is how C code works.This is the alternative mentioned above.
Yes.
Quoted 9 lines
* Generate
rust->C(e.g. String->*mutc_char) andC->rustconverters fromQuoted 4 lines
qapi-gen; use the existing C visit_type_*() to extract data from visitors and then apply said converters to turn the data into a Rust struct, and likewise in the other direction. This was the way Marc-André's prototype worked.
Converters between C and Rust data types let us cut at the C data type instead of at QObject.
Quoted 9 lines
We use the same code up to QAPI-generated C data type and from QAPI-generated C data type on.
In between, we work directly with the C data type for properties implemented in C, and convert to and from the Rust data type for properties implemented in Rust.
Correct?
Yes.
The simplest such converters convert via QObject. Grossly inefficient
Quoted 5 lines
:)
Marc-André's prototype demonstrates efficient converters can be had. The question is whether they're worth their keep.
Exactly. What keeps me on the edge is that for QMP the result is still a bit less efficient than straight QObject->Rust via Serde. For the QOM property Visitor->C->Rust is better than Visitor->QObject->Rust, but is it worth the extra amount of code generator? Leaving the code generation business to Serde is appealing and it gets one of the two (QMP) as efficient as it can be.
The Serde interface code is pretty much write once and forget, since QObject hasn't changed substantially in 15 years. The qapi-gen code is write once but stays more in the way of other Python code we maintain. On the other hand the Serde code is probably going to be a bit on the impenetrable side, and until someone writes it we don't know if the balance may tilt back towards preferring Marc-André's ideas.
Paolo
pylint isn't happy with the new module:
************* Module qapi.rs_typesscripts/qapi/rs_types.py:286:4: W0237: Parameter 'branches' has been renamed to 'variants' in overriding 'QAPISchemaGenRsTypeVisitor.visit_object_type' method (arguments-renamed) scripts/qapi/rs_types.py:307:4: W0237: Parameter 'alternatives' has been renamed to 'variants' in overriding 'QAPISchemaGenRsTypeVisitor.visit_alternate_type' method (arguments-renamed) scripts/qapi/rs_types.py:316:67: W0613: Unused argument 'builtins' (unused-argument)scripts/qapi/rs_types.py:9:0: W0611: Unused POINTER_SUFFIX imported from common (unused-import)scripts/qapi/rs_types.py:10:0: W0611: Unused rs_ctype_parse imported from rs (unused-import)scripts/qapi/rs_types.py:18:0: W0611: Unused QAPISchemaEnumType imported from schema (unused-import)scripts/qapi/rs_types.py:18:0: W0611: Unused QAPISchemaType imported from schema (unused-import)
Might be too early to bother with the type hints, but here goes anyway:
$ mypy scripts/qapi-gen.py
scripts/qapi/schema.py:70: error: Argument 1 to "rsgen_ifcond" has incompatible type "str | dict[str, object] | None"; expected "str | dict[str, Any]" [arg-type]
scripts/qapi/rs.py:99: error: Incompatible types in assignment (expression has type "str | None", variable has type "dict[str, str]") [assignment]
scripts/qapi/rs.py:103: error: Unsupported left operand type for + ("None") [operator]
scripts/qapi/rs.py:103: note: Left operand is of type "str | None"
scripts/qapi/rs.py:103: error: Incompatible types in assignment (expression has type "str | Any", variable has type "dict[str, str]") [assignment]
scripts/qapi/rs.py:106: error: Incompatible types in assignment (expression has type "str", variable has type "dict[str, str]") [assignment]
scripts/qapi/rs.py:108: error: Incompatible types in assignment (expression has type "str", variable has type "dict[str, str]") [assignment]
scripts/qapi/rs.py:110: error: Incompatible types in assignment (expression has type "str", variable has type "dict[str, str]") [assignment]
scripts/qapi/rs.py:111: error: Incompatible return value type (got "dict[str, str]", expected "str") [return-value]
scripts/qapi/rs_types.py:47: error: Item "None" of "QAPISchemaVariants | None" has no attribute "tag_member" [union-attr]
scripts/qapi/rs_types.py:49: error: Item "None" of "QAPISchemaVariants | None" has no attribute "variants" [union-attr]
scripts/qapi/rs_types.py:84: error: Item "None" of "QAPISchemaVariants | None" has no attribute "variants" [union-attr]
scripts/qapi/rs_types.py:116: error: Incompatible default for argument "exclude" (default has type "None", argument has type "list[str]") [assignment]
scripts/qapi/rs_types.py:116: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
scripts/qapi/rs_types.py:116: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
scripts/qapi/rs_types.py:118: error: Incompatible return value type (got "list[str]", expected "str") [return-value]
scripts/qapi/rs_types.py:251: error: Item "None" of "QAPISchemaVariants | None" has no attribute "variants" [union-attr]
scripts/qapi/rs_types.py:309: error: Argument 2 of "visit_alternate_type" is incompatible with supertype "QAPISchemaVisitor"; supertype defines the argument type as "QAPISourceInfo | None" [override]
scripts/qapi/rs_types.py:309: note: This violates the Liskov substitution principle
scripts/qapi/rs_types.py:309: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
Found 15 errors in 3 files (checked 1 source file)Il mar 17 giu 2025, 09:49 Markus Armbruster <[email protected]> ha scritto:
Quoted 2 lines
pylint isn't happy with the new module:
Fixed, will post a new version.
Can I pick your brain on whether to focus on Rust<->QObject conversion or give the Rust structs the full visitor treatment? (See other messages in reply to the cover letter).
Paolo
************* Module qapi.rs_types
Quoted 71 lines
scripts/qapi/rs_types.py:286:4: W0237: Parameter 'branches' has been renamed to 'variants' in overriding 'QAPISchemaGenRsTypeVisitor.visit_object_type' method (arguments-renamed)scripts/qapi/rs_types.py:307:4: W0237: Parameter 'alternatives' has been renamed to 'variants' in overriding 'QAPISchemaGenRsTypeVisitor.visit_alternate_type' method (arguments-renamed)scripts/qapi/rs_types.py:316:67: W0613: Unused argument 'builtins' (unused-argument)scripts/qapi/rs_types.py:9:0: W0611: UnusedPOINTER_SUFFIXimported from common (unused-import)scripts/qapi/rs_types.py:10:0: W0611: Unusedrs_ctype_parseimported from rs (unused-import)scripts/qapi/rs_types.py:18:0: W0611: Unused QAPISchemaEnumType imported from schema (unused-import)scripts/qapi/rs_types.py:18:0: W0611: Unused QAPISchemaType imported from schema (unused-import)Might be too early to bother with the type hints, but here goes anyway:
$ mypy scripts/qapi-gen.py scripts/qapi/schema.py:70: error: Argument 1 to "rsgen_ifcond" has incompatible type "str | dict[str, object] | None"; expected "str | dict[str, Any]" [arg-type] scripts/qapi/rs.py:99: error: Incompatible types in assignment (expression has type "str | None", variable has type "dict[str, str]") [assignment] scripts/qapi/rs.py:103: error: Unsupported left operand type for + ("None") [operator] scripts/qapi/rs.py:103: note: Left operand is of type "str | None" scripts/qapi/rs.py:103: error: Incompatible types in assignment (expression has type "str | Any", variable has type "dict[str, str]") [assignment] scripts/qapi/rs.py:106: error: Incompatible types in assignment (expression has type "str", variable has type "dict[str, str]") [assignment] scripts/qapi/rs.py:108: error: Incompatible types in assignment (expression has type "str", variable has type "dict[str, str]") [assignment] scripts/qapi/rs.py:110: error: Incompatible types in assignment (expression has type "str", variable has type "dict[str, str]") [assignment] scripts/qapi/rs.py:111: error: Incompatible return value type (got "dict[str, str]", expected "str") [return-value] scripts/qapi/rs_types.py:47: error: Item "None" of "QAPISchemaVariants | None" has no attribute "tag_member" [union-attr] scripts/qapi/rs_types.py:49: error: Item "None" of "QAPISchemaVariants | None" has no attribute "variants" [union-attr] scripts/qapi/rs_types.py:84: error: Item "None" of "QAPISchemaVariants | None" has no attribute "variants" [union-attr] scripts/qapi/rs_types.py:116: error: Incompatible default for argument "exclude" (default has type "None", argument has type "list[str]") [assignment] scripts/qapi/rs_types.py:116: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True scripts/qapi/rs_types.py:116: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase scripts/qapi/rs_types.py:118: error: Incompatible return value type (got "list[str]", expected "str") [return-value] scripts/qapi/rs_types.py:251: error: Item "None" of "QAPISchemaVariants | None" has no attribute "variants" [union-attr] scripts/qapi/rs_types.py:309: error: Argument 2 of "visit_alternate_type" is incompatible with supertype "QAPISchemaVisitor"; supertype defines the argument type as "QAPISourceInfo | None" [override] scripts/qapi/rs_types.py:309: note: This violates the Liskov substitution principle scripts/qapi/rs_types.py:309: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides Found 15 errors in 3 files (checked 1 source file)