On 12/25/24 17:24, Zhao Liu wrote:
On Fri, Dec 20, 2024 at 03:29:44PM +0100, Paolo Bonzini wrote:
Date: Fri, 20 Dec 2024 15:29:44 +0100
From: Paolo Bonzini <pbonz...@redhat.com>
Subject: [PATCH 02/12] rust: add a utility module for compile-time type
checks
X-Mailer: git-send-email 2.47.1
It is relatively common in the low-level qemu_api code to assert that
a field of a struct has a specific type; for example, it can be used
to ensure that the fields match what the qemu_api and C code expects
for safety.
Signed-off-by: Paolo Bonzini <pbonz...@redhat.com>
---
rust/qemu-api/meson.build | 1 +
rust/qemu-api/src/assertions.rs | 90 +++++++++++++++++++++++++++++++++
rust/qemu-api/src/lib.rs | 1 +
3 files changed, 92 insertions(+)
create mode 100644 rust/qemu-api/src/assertions.rs
Very useful! Previously I found qdev property macro lacks such type
check, but I falied to think of a good way to implement type_check in
Rust, and glad to see the correct approach! Besides qdev property, I
think vmstate also needs this.
Right, though for vmstate (and probably qdev properties too) it's
probably better to go from types to C structs
(PropertyInfo/VMStateField) and avoid having to do the assertions.
And I think we can make the examples as the unit tests.
Having doctests support in "make check" is on my list; I have started
contributing a rust.doctest() function to Meson for that purpose. For
now they are limited to what can be run through Cargo (i.e. you cannot
use libqemuutil.a functions) but we'll see if the function is accepted.
Paolo
Reviewed-by: Zhao Liu <zhao1....@intel.com>