rust: clean up define_property macro

Use the "struct update" syntax to initialize most of the fields to zero,
and simplify the handmade type-checking of $name.

Reviewed-by: Junjie Mao <junjie.mao@hotmail.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2024-10-18 10:45:00 +02:00
parent e90d470733
commit 03a573b960

View file

@ -29,44 +29,27 @@ macro_rules! device_class_init {
macro_rules! define_property { macro_rules! define_property {
($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr, default = $defval:expr$(,)*) => { ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr, default = $defval:expr$(,)*) => {
$crate::bindings::Property { $crate::bindings::Property {
name: { // use associated function syntax for type checking
#[used] name: ::core::ffi::CStr::as_ptr($name),
static _TEMP: &::core::ffi::CStr = $name;
_TEMP.as_ptr()
},
info: $prop, info: $prop,
offset: ::core::mem::offset_of!($state, $field) offset: ::core::mem::offset_of!($state, $field)
.try_into() .try_into()
.expect("Could not fit offset value to type"), .expect("Could not fit offset value to type"),
bitnr: 0,
bitmask: 0,
set_default: true, set_default: true,
defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval.into() }, defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval as u64 },
arrayoffset: 0, ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
arrayinfo: ::core::ptr::null(),
arrayfieldsize: 0,
link_type: ::core::ptr::null(),
} }
}; };
($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr$(,)*) => { ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr$(,)*) => {
$crate::bindings::Property { $crate::bindings::Property {
name: { // use associated function syntax for type checking
#[used] name: ::core::ffi::CStr::as_ptr($name),
static _TEMP: &::core::ffi::CStr = $name;
_TEMP.as_ptr()
},
info: $prop, info: $prop,
offset: ::core::mem::offset_of!($state, $field) offset: ::core::mem::offset_of!($state, $field)
.try_into() .try_into()
.expect("Could not fit offset value to type"), .expect("Could not fit offset value to type"),
bitnr: 0,
bitmask: 0,
set_default: false, set_default: false,
defval: $crate::bindings::Property__bindgen_ty_1 { i: 0 }, ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
arrayoffset: 0,
arrayinfo: ::core::ptr::null(),
arrayfieldsize: 0,
link_type: ::core::ptr::null(),
} }
}; };
} }