qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC PATCH 6/9] rust: vmstate: add public utility macros to implemen


From: Zhao Liu
Subject: Re: [RFC PATCH 6/9] rust: vmstate: add public utility macros to implement VMState
Date: Wed, 8 Jan 2025 16:15:45 +0800

> +/// This macro can be used (by just passing it a type) to forward the 
> `VMState`
> +/// trait to the first field of a tuple.  This is a workaround for lack of
> +/// support of nested [`offset_of`](core::mem::offset_of) until Rust 1.82.0.
> +///
> +/// # Examples
> +///
> +/// ```
> +/// # use qemu_api::vmstate::impl_vmstate_forward;
> +/// pub struct Fifo([u8; 16]);
> +/// impl_vmstate_forward!(Fifo);
> +/// ```
> +#[macro_export]
> +macro_rules! impl_vmstate_forward {
> +    // This is similar to impl_vmstate_transparent below, but it
> +    // uses the same trick as vmstate_of! to obtain the type of
> +    // the first field of the tuple
> +    ($tuple:ty) => {
> +        unsafe impl $crate::vmstate::VMState for $tuple {
> +            const SCALAR_TYPE: $crate::vmstate::VMStateFieldType =
> +                
> $crate::call_func_with_field!($crate::vmstate::vmstate_scalar_type, $tuple, 
> 0);
> +            const BASE: $crate::bindings::VMStateField =
> +                $crate::call_func_with_field!($crate::vmstate::vmstate_base, 
> $tuple, 0);
> +        }
> +    };
> +}
> +

Powerful!

>  // Transparent wrappers: just use the internal type
>  
>  macro_rules! impl_vmstate_transparent {
> @@ -283,6 +318,26 @@ unsafe impl<$base> VMState for $type where $base: 
> VMState $($where)* {
>  impl_vmstate_transparent!(crate::cell::BqlCell<T> where T: VMState);
>  impl_vmstate_transparent!(crate::cell::BqlRefCell<T> where T: VMState);
>  
> +#[macro_export]
> +macro_rules! impl_vmstate_bitsized {
> +    ($type:ty) => {
> +        unsafe impl $crate::vmstate::VMState for $type {
> +            const SCALAR_TYPE: $crate::vmstate::VMStateFieldType =
> +                                        <<<$type as 
> ::bilge::prelude::Bitsized>::ArbitraryInt
> +                                          as 
> ::bilge::prelude::Number>::UnderlyingType
> +                                         as 
> $crate::vmstate::VMState>::SCALAR_TYPE;
> +            const BASE: $crate::bindings::VMStateField =
> +                                        <<<$type as 
> ::bilge::prelude::Bitsized>::ArbitraryInt
> +                                          as 
> ::bilge::prelude::Number>::UnderlyingType
> +                                         as $crate::vmstate::VMState>::BASE;
> +            const VARRAY_FLAG: $crate::bindings::VMStateFlags =
> +                                        <<<$type as 
> ::bilge::prelude::Bitsized>::ArbitraryInt
> +                                          as 
> ::bilge::prelude::Number>::UnderlyingType
> +                                         as 
> $crate::vmstate::VMState>::VARRAY_FLAG;
> +        }
> +    };
> +}
> +
>  // Scalar types using predefined VMStateInfos
>  
>  macro_rules! impl_vmstate_scalar {

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>





reply via email to

[Prev in Thread] Current Thread [Next in Thread]