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(-)
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>
---
Changes Since v2:
- Check #field_ty::BITS instead of u8::MAX.
- Update test cases.
---
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 86638c076666..23f2eefd1cd9 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 50239f228be2..ee417bb4b4ef 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 65691412ff57..b65cf656fa36 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.34.1
On Fri, 24 Oct 2025 07:13, Zhao Liu <zhao1.liu@intel.com> wrote:
>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>
>---
>Changes Since v2:
> - Check #field_ty::BITS instead of u8::MAX.
> - Update test cases.
>---
LGTM, some questions...
> 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 86638c076666..23f2eefd1cd9 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 50239f228be2..ee417bb4b4ef 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");
Const panic messages cannot use formatting parameters yet, but.
Can we interpolate the type (e.g u32) in the compile-time panic message?
Not important but would make the error message friendlier.
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
>+ }
>+ #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 65691412ff57..b65cf656fa36 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.34.1
>
> >+ quote! {
> >+ {
> >+ const {
> >+ assert!(#bitval >= 0 && #bitval < #field_ty::BITS as _,
> >+ "bit number exceeds type bits
> >range");
>
> Const panic messages cannot use formatting parameters yet, but.
> Can we interpolate the type (e.g u32) in the compile-time panic message?
Good idea! `concat!` with `stringify!(#field_ty)` should work.
> Not important but would make the error message friendlier.
Yeah, let me refresh a v3.
> Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Thanks!
Regards,
Zhao
© 2016 - 2025 Red Hat, Inc.