qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Zhao Liu <zhao1.liu@intel.com>
Subject: [PULL 12/18] rust/qemu-macros: Convert bit value to u8 within #[property]
Date: Tue, 28 Oct 2025 18:34:24 +0100	[thread overview]
Message-ID: <20251028173430.2180057-13-pbonzini@redhat.com> (raw)
In-Reply-To: <20251028173430.2180057-1-pbonzini@redhat.com>

From: Zhao Liu <zhao1.liu@intel.com>

For bit property, make the type conversion within the #[property] macro
so that users do not need to handle the conversion.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20251024041344.1389488-1-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 rust/hw/timer/hpet/src/device.rs |  2 +-
 rust/qemu-macros/src/lib.rs      | 23 ++++++++++++++++++-----
 rust/qemu-macros/src/tests.rs    | 15 ++++++++++++---
 3 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 86638c07666..23f2eefd1cd 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -539,7 +539,7 @@ pub struct HPETState {
     // Internal state
     /// Capabilities that QEMU HPET supports.
     /// bit 0: MSI (or FSB) support.
-    #[property(rename = "msi", bit = HPET_FLAG_MSI_SUPPORT_SHIFT as u8, default = false)]
+    #[property(rename = "msi", bit = HPET_FLAG_MSI_SUPPORT_SHIFT, default = false)]
     flags: u32,
 
     /// Offset of main counter relative to qemu clock.
diff --git a/rust/qemu-macros/src/lib.rs b/rust/qemu-macros/src/lib.rs
index 50239f228be..ee417bb4b4e 100644
--- a/rust/qemu-macros/src/lib.rs
+++ b/rust/qemu-macros/src/lib.rs
@@ -262,12 +262,25 @@ macro_rules! str_to_c_str {
             },
         )?;
         let field_ty = field.ty.clone();
-        let qdev_prop = if bitnr.is_none() {
-            quote! { <#field_ty as ::hwcore::QDevProp>::BASE_INFO }
+        let (qdev_prop, bitval) = if let Some(bitval) = bitnr {
+            (
+                quote! { <#field_ty as ::hwcore::QDevProp>::BIT_INFO },
+                quote! {
+                    {
+                        const {
+                            assert!(#bitval >= 0 && #bitval < #field_ty::BITS as _,
+                                    "bit number exceeds type bits range");
+                        }
+                        #bitval as u8
+                    }
+                },
+            )
         } else {
-            quote! { <#field_ty as ::hwcore::QDevProp>::BIT_INFO }
+            (
+                quote! { <#field_ty as ::hwcore::QDevProp>::BASE_INFO },
+                quote! { 0 },
+            )
         };
-        let bitnr = bitnr.unwrap_or(syn::Expr::Verbatim(quote! { 0 }));
         let set_default = defval.is_some();
         let defval = defval.unwrap_or(syn::Expr::Verbatim(quote! { 0 }));
         properties_expanded.push(quote! {
@@ -275,7 +288,7 @@ macro_rules! str_to_c_str {
                 name: ::std::ffi::CStr::as_ptr(#prop_name),
                 info: #qdev_prop,
                 offset: ::core::mem::offset_of!(#name, #field_name) as isize,
-                bitnr: #bitnr,
+                bitnr: #bitval,
                 set_default: #set_default,
                 defval: ::hwcore::bindings::Property__bindgen_ty_1 { u: #defval as u64 },
                 ..::common::Zeroable::ZERO
diff --git a/rust/qemu-macros/src/tests.rs b/rust/qemu-macros/src/tests.rs
index 65691412ff5..b65cf656fa3 100644
--- a/rust/qemu-macros/src/tests.rs
+++ b/rust/qemu-macros/src/tests.rs
@@ -179,7 +179,10 @@ unsafe impl ::hwcore::DevicePropertiesImpl for DummyState {
                         name: ::std::ffi::CStr::as_ptr(c"flags"),
                         info: <u32 as ::hwcore::QDevProp>::BIT_INFO,
                         offset: ::core::mem::offset_of!(DummyState, flags) as isize,
-                        bitnr: 3,
+                        bitnr : {
+                            const { assert!(3 >= 0 && 3 < u32::BITS as _ , "bit number exceeds type bits range"); }
+                            3 as u8
+                        },
                         set_default: false,
                         defval: ::hwcore::bindings::Property__bindgen_ty_1 { u: 0 as u64 },
                         ..::common::Zeroable::ZERO
@@ -207,7 +210,10 @@ unsafe impl ::hwcore::DevicePropertiesImpl for DummyState {
                         name: ::std::ffi::CStr::as_ptr(c"flags"),
                         info: <u32 as ::hwcore::QDevProp>::BIT_INFO,
                         offset: ::core::mem::offset_of!(DummyState, flags) as isize,
-                        bitnr: 3,
+                        bitnr : {
+                            const { assert!(3 >= 0 && 3 < u32::BITS as _ , "bit number exceeds type bits range"); }
+                            3 as u8
+                        },
                         set_default: true,
                         defval: ::hwcore::bindings::Property__bindgen_ty_1 { u: true as u64 },
                         ..::common::Zeroable::ZERO
@@ -235,7 +241,10 @@ unsafe impl ::hwcore::DevicePropertiesImpl for DummyState {
                         name: ::std::ffi::CStr::as_ptr(c"msi"),
                         info: <u64 as ::hwcore::QDevProp>::BIT_INFO,
                         offset: ::core::mem::offset_of!(DummyState, flags) as isize,
-                        bitnr: 3,
+                        bitnr : {
+                            const { assert!(3 >= 0 && 3 < u64::BITS as _ , "bit number exceeds type bits range"); }
+                            3 as u8
+                        },
                         set_default: true,
                         defval: ::hwcore::bindings::Property__bindgen_ty_1 { u: false as u64 },
                         ..::common::Zeroable::ZERO
-- 
2.51.1



  parent reply	other threads:[~2025-10-28 17:37 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-28 17:34 [PULL 00/18] Miscellaneous changes for 2025-10-28 Paolo Bonzini
2025-10-28 17:34 ` [PULL 01/18] scripts: clean up meson-buildoptions.py Paolo Bonzini
2025-10-28 17:34 ` [PULL 02/18] i386/kvm/cpu: Init SMM cpu address space for hotplugged CPUs Paolo Bonzini
2025-10-29  7:01   ` Zhao Liu
2025-10-30  2:19     ` Xiaoyao Li
2025-10-30  7:36   ` Michael Tokarev
2025-10-30  7:49     ` Xiaoyao Li
2025-10-30  8:03       ` Michael Tokarev
2025-10-28 17:34 ` [PULL 03/18] rcu: Unify force quiescent state Paolo Bonzini
2025-11-03 13:59   ` Regression with the "replay" test on target alpha (was: [PULL 03/18] rcu: Unify force quiescent state) Thomas Huth
2025-11-04  1:45     ` Regression with the "replay" test on target alpha Akihiko Odaki
2025-11-04  7:41       ` Thomas Huth
2025-11-04  8:08         ` Akihiko Odaki
2025-11-04  8:38           ` Thomas Huth
2025-11-04 12:18             ` Paolo Bonzini
2025-11-05  6:29               ` Akihiko Odaki
2025-11-07  7:41                 ` Thomas Huth
2025-11-07  7:49                   ` Paolo Bonzini
2025-10-28 17:34 ` [PULL 04/18] rust: remove useless glib_sys bindings Paolo Bonzini
2025-10-28 17:34 ` [PULL 05/18] rust: only leave leaf crates as workspace members Paolo Bonzini
2025-10-28 17:34 ` [PULL 06/18] qobject: make refcount atomic Paolo Bonzini
2025-10-28 17:34 ` [PULL 07/18] char: rename CharBackend->CharFrontend Paolo Bonzini
2025-10-28 17:34 ` [PULL 08/18] accel/mshv: initialize thread name Paolo Bonzini
2025-10-28 17:34 ` [PULL 09/18] accel/mshv: use return value of handle_pio_str_read Paolo Bonzini
2025-10-28 17:34 ` [PULL 10/18] esp.c: fix esp_cdb_ready() FIFO wraparound limit calculation Paolo Bonzini
2025-10-29 11:42   ` Michael Tokarev
2025-10-28 17:34 ` [PULL 11/18] qtest/am53c974-test: add additional test for cmdfifo overflow Paolo Bonzini
2025-10-28 17:34 ` Paolo Bonzini [this message]
2025-10-28 17:34 ` [PULL 13/18] scsi: make SCSIRequest refcount atomic Paolo Bonzini
2025-10-28 17:34 ` [PULL 14/18] qdev: Change PropertyInfo method print() to return malloc'ed string Paolo Bonzini
2025-10-28 17:34 ` [PULL 15/18] hw/i386/isapc.c: warn rather than reject modern x86 CPU models Paolo Bonzini
2025-10-28 17:34 ` [PULL 16/18] docs/about/deprecated.rst: document isapc deprecation for " Paolo Bonzini
2025-10-28 17:34 ` [PULL 17/18] target/i386: clear CPU_INTERRUPT_SIPI for all accelerators Paolo Bonzini
2025-10-28 17:34 ` [PULL 18/18] rust: migration: allow passing ParentField<> to vmstate_of! Paolo Bonzini
2025-10-29  6:34   ` Richard Henderson

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=20251028173430.2180057-13-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=zhao1.liu@intel.com \
    /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;
as well as URLs for NNTP newsgroup(s).