From: Zhao Liu Date: Fri, 24 Oct 2025 04:13:44 +0000 (+0800) Subject: rust/qemu-macros: Convert bit value to u8 within #[property] X-Git-Tag: v10.2.0-rc1~40^2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=59d8f86442fb7ef7fb8f62ce0e7fedadd027ba39;p=thirdparty%2Fqemu.git rust/qemu-macros: Convert bit value to u8 within #[property] 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 Signed-off-by: Zhao Liu Link: https://lore.kernel.org/r/20251024041344.1389488-1-zhao1.liu@intel.com Signed-off-by: Paolo Bonzini --- diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs index 86638c0766..23f2eefd1c 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 50239f228b..ee417bb4b4 100644 --- a/rust/qemu-macros/src/lib.rs +++ b/rust/qemu-macros/src/lib.rs @@ -262,12 +262,25 @@ fn derive_device_or_error(input: DeriveInput) -> Result::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 @@ fn derive_device_or_error(input: DeriveInput) -> Result::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 @@ fn test_derive_device() { name: ::std::ffi::CStr::as_ptr(c"flags"), info: ::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 @@ fn test_derive_device() { name: ::std::ffi::CStr::as_ptr(c"msi"), info: ::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