[PATCH 08/12] rust/qdev: Support property info for more common types

Zhao Liu posted 12 patches 1 week, 5 days ago
There is a newer version of this series
[PATCH 08/12] rust/qdev: Support property info for more common types
Posted by Zhao Liu 1 week, 5 days ago
Add a helper macro to implement QDevProp trait for u8/u16/u32/usize/i32
/i64.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 rust/hw/core/src/qdev.rs | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/rust/hw/core/src/qdev.rs b/rust/hw/core/src/qdev.rs
index d887046d8de1..b57dc05ebb0e 100644
--- a/rust/hw/core/src/qdev.rs
+++ b/rust/hw/core/src/qdev.rs
@@ -134,20 +134,24 @@ pub unsafe trait QDevProp {
     const BASE_INFO: *const bindings::PropertyInfo;
 }
 
-/// Use [`bindings::qdev_prop_bool`] for `bool`.
-unsafe impl QDevProp for bool {
-    const BASE_INFO: *const bindings::PropertyInfo = addr_of!(bindings::qdev_prop_bool);
-}
-
-/// Use [`bindings::qdev_prop_uint64`] for `u64`.
-unsafe impl QDevProp for u64 {
-    const BASE_INFO: *const bindings::PropertyInfo = addr_of!(bindings::qdev_prop_uint64);
+macro_rules! impl_qdev_prop {
+    ($type:ty,$info:ident) => {
+        unsafe impl $crate::qdev::QDevProp for $type {
+            const BASE_INFO: *const $crate::bindings::PropertyInfo =
+                addr_of!($crate::bindings::$info);
+        }
+    };
 }
 
-/// Use [`bindings::qdev_prop_chr`] for [`chardev::CharBackend`].
-unsafe impl QDevProp for chardev::CharBackend {
-    const BASE_INFO: *const bindings::PropertyInfo = addr_of!(bindings::qdev_prop_chr);
-}
+impl_qdev_prop!(bool, qdev_prop_bool);
+impl_qdev_prop!(u8, qdev_prop_uint8);
+impl_qdev_prop!(u16, qdev_prop_uint16);
+impl_qdev_prop!(u32, qdev_prop_uint32);
+impl_qdev_prop!(u64, qdev_prop_uint64);
+impl_qdev_prop!(usize, qdev_prop_usize);
+impl_qdev_prop!(i32, qdev_prop_int32);
+impl_qdev_prop!(i64, qdev_prop_int64);
+impl_qdev_prop!(chardev::CharBackend, qdev_prop_chr);
 
 /// Trait to define device properties.
 ///
-- 
2.34.1
Re: [PATCH 08/12] rust/qdev: Support property info for more common types
Posted by Manos Pitsidianakis 1 week, 5 days ago
On Tue, Sep 16, 2025 at 11:34 AM Zhao Liu <zhao1.liu@intel.com> wrote:
>
> Add a helper macro to implement QDevProp trait for u8/u16/u32/usize/i32
> /i64.
>
> Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
> ---

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>

You can do something like this btw:

macro_rules! impl_qdev_prop {
    ($($type:ty => $info:ident),*$(,)?) => {
        $(unsafe impl $crate::qdev::QDevProp for $type {
            const BASE_INFO: *const $crate::bindings::PropertyInfo =
                addr_of!($crate::bindings::$info);
        })*
    };
 }

impl_qdev_prop!(
    bool => qdev_prop_bool,
    u8 => qdev_prop_uint8,
    ...
);


>  rust/hw/core/src/qdev.rs | 28 ++++++++++++++++------------
>  1 file changed, 16 insertions(+), 12 deletions(-)
>
> diff --git a/rust/hw/core/src/qdev.rs b/rust/hw/core/src/qdev.rs
> index d887046d8de1..b57dc05ebb0e 100644
> --- a/rust/hw/core/src/qdev.rs
> +++ b/rust/hw/core/src/qdev.rs
> @@ -134,20 +134,24 @@ pub unsafe trait QDevProp {
>      const BASE_INFO: *const bindings::PropertyInfo;
>  }
>
> -/// Use [`bindings::qdev_prop_bool`] for `bool`.
> -unsafe impl QDevProp for bool {
> -    const BASE_INFO: *const bindings::PropertyInfo = addr_of!(bindings::qdev_prop_bool);
> -}
> -
> -/// Use [`bindings::qdev_prop_uint64`] for `u64`.
> -unsafe impl QDevProp for u64 {
> -    const BASE_INFO: *const bindings::PropertyInfo = addr_of!(bindings::qdev_prop_uint64);
> +macro_rules! impl_qdev_prop {
> +    ($type:ty,$info:ident) => {
> +        unsafe impl $crate::qdev::QDevProp for $type {
> +            const BASE_INFO: *const $crate::bindings::PropertyInfo =
> +                addr_of!($crate::bindings::$info);
> +        }
> +    };
>  }
>
> -/// Use [`bindings::qdev_prop_chr`] for [`chardev::CharBackend`].
> -unsafe impl QDevProp for chardev::CharBackend {
> -    const BASE_INFO: *const bindings::PropertyInfo = addr_of!(bindings::qdev_prop_chr);
> -}
> +impl_qdev_prop!(bool, qdev_prop_bool);
> +impl_qdev_prop!(u8, qdev_prop_uint8);
> +impl_qdev_prop!(u16, qdev_prop_uint16);
> +impl_qdev_prop!(u32, qdev_prop_uint32);
> +impl_qdev_prop!(u64, qdev_prop_uint64);
> +impl_qdev_prop!(usize, qdev_prop_usize);
> +impl_qdev_prop!(i32, qdev_prop_int32);
> +impl_qdev_prop!(i64, qdev_prop_int64);
> +impl_qdev_prop!(chardev::CharBackend, qdev_prop_chr);
>
>  /// Trait to define device properties.
>  ///
> --
> 2.34.1
>
Re: [PATCH 08/12] rust/qdev: Support property info for more common types
Posted by Zhao Liu 1 week, 4 days ago
On Tue, Sep 16, 2025 at 01:13:28PM +0300, Manos Pitsidianakis wrote:
> Date: Tue, 16 Sep 2025 13:13:28 +0300
> From: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> Subject: Re: [PATCH 08/12] rust/qdev: Support property info for more common
>  types
> 
> On Tue, Sep 16, 2025 at 11:34 AM Zhao Liu <zhao1.liu@intel.com> wrote:
> >
> > Add a helper macro to implement QDevProp trait for u8/u16/u32/usize/i32
> > /i64.
> >
> > Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
> > ---
> 
> Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>

Thanks!

> You can do something like this btw:
> 
> macro_rules! impl_qdev_prop {
>     ($($type:ty => $info:ident),*$(,)?) => {
>         $(unsafe impl $crate::qdev::QDevProp for $type {
>             const BASE_INFO: *const $crate::bindings::PropertyInfo =
>                 addr_of!($crate::bindings::$info);
>         })*
>     };
>  }
> 
> impl_qdev_prop!(
>     bool => qdev_prop_bool,
>     u8 => qdev_prop_uint8,
>     ...
> );
> 

Yes, this can work! But a benefit of non-repetitive macro is that it
can also be exposed for use by other crates if necessary.

Regards,
Zhao