From: Zhao Liu <zhao1.liu@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-rust@nongnu.org, armbru@redhat.com,
marcandre.lureau@redhat.com
Subject: Re: [PATCH v3 15/19] scripts/qapi: generate high-level Rust bindings
Date: Thu, 11 Jun 2026 23:23:14 +0800 [thread overview]
Message-ID: <airS4hj5u/9T2T2H@intel.com> (raw)
In-Reply-To: <20260526175618.227743-16-pbonzini@redhat.com>
On Tue, May 26, 2026 at 07:56:14PM +0200, Paolo Bonzini wrote:
> Date: Tue, 26 May 2026 19:56:14 +0200
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH v3 15/19] scripts/qapi: generate high-level Rust bindings
> X-Mailer: git-send-email 2.54.0
>
> 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 use #[repr(u32)] and can be transmuted to their C counterparts
>
> - has_foo/foo members are mapped to Option<T>
>
> - lists are represented as Vec<T>
>
> - structures map fields 1:1 to Rust
>
> - alternate are represented as Rust enum, each variant being a 1-element
> tuple
>
> - unions are represented in a similar way as in C: a struct S with a "u"
> member (since S may have extra 'base' fields). The discriminant
> isn't a member of S, since Rust enum already include it, but it can be
> recovered with "mystruct.u.into()"
>
> Anything that includes a recursive struct puts it in a Box. Lists are
> not considered recursive, because Vec breaks the recursion (it's possible
> to construct an object containing an empty Vec of its own type).
>
> Given the experimental nature of Rust, and the incompleteness of the
> backend (it lacks commands and events), QAPIRsBackend is not modular
> and is not built together with the C and trace-event files. It can
> be used by specifying "-B qapi.backend.QAPIRsBackend" on the qapi-gen
> command line.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> Link: https://lore.kernel.org/r/20210907121943.3498701-21-marcandre.lureau@redhat.com
> [Paolo: rewrite conversion of leaf types]
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> meson.build | 4 +-
> scripts/qapi/backend.py | 25 +++
> scripts/qapi/common.py | 49 ++++++
> scripts/qapi/rs.py | 50 ++++++
> scripts/qapi/rs_types.py | 372 +++++++++++++++++++++++++++++++++++++++
> scripts/qapi/schema.py | 59 +++++--
> 6 files changed, 540 insertions(+), 19 deletions(-)
> create mode 100644 scripts/qapi/rs.py
> create mode 100644 scripts/qapi/rs_types.py
[snip]
> +def rs_name(name: str) -> str:
> + """
> + Map @name to a valid, possibly raw Rust identifier.
> + """
> + name = re.sub(r'[^A-Za-z0-9_]', '_', name)
> + if name[0].isnumeric():
> + name = '_' + 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
EMM, r#self, r#Self, r#crate and r#super are "RESERVED_RAW_IDENTIFIER",
and "It is an error to use the RESERVED_RAW_IDENTIFIER token". [*]
[*]: https://doc.rust-lang.org/reference/identifiers.html#grammar-RESERVED_RAW_IDENTIFIER
We can't use 'q_' prefix for these special case, or what about
name + '_'?
e.g.,
if name in ('self', 'Self', 'crate', 'super'):
name = name + '_'
elif name in (...)
name = 'r#' + name
> +
> + return name
> +
[snip]
> @@ -502,6 +515,9 @@ def is_implicit(self) -> bool:
> def c_type(self) -> str:
> return c_name(self.name) + POINTER_SUFFIX
>
> + def rs_type(self) -> str:
Only a nit, it's better to check _checked before accessing element_type
(just refer Markus' suggestion in v2)
assert self._checked
> + return 'Vec<%s>' % self.element_type.rs_type()
> +
Regards,
Zhao
next prev parent reply other threads:[~2026-06-11 14:56 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-26 17:55 [PATCH v3 00/19] rust: QObject and QAPI bindings Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 01/19] rust/qobject: add basic bindings Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 02/19] subprojects: add serde Paolo Bonzini
2026-06-01 14:47 ` Zhao Liu
2026-05-26 17:56 ` [PATCH v3 03/19] rust/qobject: add Serialize implementation Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 04/19] rust/qobject: add Serializer (to_qobject) implementation Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 05/19] rust/qobject: add Deserialize implementation Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 06/19] rust/qobject: add Deserializer (from_qobject) implementation Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 07/19] rust/qobject: add from/to JSON bindings for QObject Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 08/19] rust/qobject: add Display/Debug Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 09/19] scripts/qapi: reject empty enums Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 10/19] scripts/qapi: enum with conditional first item must be optional Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 11/19] scripts/qapi: add QAPISchemaIfCond.rsgen() Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 12/19] scripts/qapi: add QAPISchemaType.is_predefined Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 13/19] scripts/qapi: pull c_name and lstrip from camel_to_upper to caller Paolo Bonzini
2026-06-10 14:42 ` Zhao Liu
2026-05-26 17:56 ` [PATCH v3 14/19] scripts/qapi: allow passing multiple segments to mcgen Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 15/19] scripts/qapi: generate high-level Rust bindings Paolo Bonzini
2026-05-26 21:03 ` Marc-André Lureau
2026-06-04 10:11 ` Zhao Liu
2026-06-11 15:23 ` Zhao Liu [this message]
2026-06-11 15:33 ` Manos Pitsidianakis
2026-05-26 17:56 ` [PATCH v3 16/19] scripts/rustc_args: add --no-strict-cfg Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 17/19] rust/util: build QAPI types Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 18/19] scripts/qapi: add serde attributes Paolo Bonzini
2026-05-26 17:56 ` [PATCH v3 19/19] rust/tests: QAPI integration tests Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=airS4hj5u/9T2T2H@intel.com \
--to=zhao1.liu@intel.com \
--cc=armbru@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox