Use the "struct update" syntax to initialize most of the fields to zero,
and simplify the handmade type-checking of $name.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api/src/device_class.rs | 29 ++++++-----------------------
1 file changed, 6 insertions(+), 23 deletions(-)
diff --git a/rust/qemu-api/src/device_class.rs b/rust/qemu-api/src/device_class.rs
index 2219b9f73d0..5aba426d243 100644
--- a/rust/qemu-api/src/device_class.rs
+++ b/rust/qemu-api/src/device_class.rs
@@ -29,44 +29,27 @@ macro_rules! device_class_init {
macro_rules! define_property {
($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr, default = $defval:expr$(,)*) => {
$crate::bindings::Property {
- name: {
- #[used]
- static _TEMP: &::core::ffi::CStr = $name;
- _TEMP.as_ptr()
- },
+ // use associated function syntax for type checking
+ name: ::core::ffi::CStr::as_ptr($name),
info: $prop,
offset: ::core::mem::offset_of!($state, $field)
.try_into()
.expect("Could not fit offset value to type"),
- bitnr: 0,
- bitmask: 0,
set_default: true,
defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval.into() },
- arrayoffset: 0,
- arrayinfo: ::core::ptr::null(),
- arrayfieldsize: 0,
- link_type: ::core::ptr::null(),
+ ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
}
};
($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr$(,)*) => {
$crate::bindings::Property {
- name: {
- #[used]
- static _TEMP: &::core::ffi::CStr = $name;
- _TEMP.as_ptr()
- },
+ // use associated function syntax for type checking
+ name: ::core::ffi::CStr::as_ptr($name),
info: $prop,
offset: ::core::mem::offset_of!($state, $field)
.try_into()
.expect("Could not fit offset value to type"),
- bitnr: 0,
- bitmask: 0,
set_default: false,
- defval: $crate::bindings::Property__bindgen_ty_1 { i: 0 },
- arrayoffset: 0,
- arrayinfo: ::core::ptr::null(),
- arrayfieldsize: 0,
- link_type: ::core::ptr::null(),
+ ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
}
};
}
--
2.46.2
Paolo Bonzini <pbonzini@redhat.com> writes:
> Use the "struct update" syntax to initialize most of the fields to zero,
> and simplify the handmade type-checking of $name.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> rust/qemu-api/src/device_class.rs | 29 ++++++-----------------------
> 1 file changed, 6 insertions(+), 23 deletions(-)
>
> diff --git a/rust/qemu-api/src/device_class.rs b/rust/qemu-api/src/device_class.rs
> index 2219b9f73d0..5aba426d243 100644
> --- a/rust/qemu-api/src/device_class.rs
> +++ b/rust/qemu-api/src/device_class.rs
> @@ -29,44 +29,27 @@ macro_rules! device_class_init {
> macro_rules! define_property {
> ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr, default = $defval:expr$(,)*) => {
> $crate::bindings::Property {
> - name: {
> - #[used]
> - static _TEMP: &::core::ffi::CStr = $name;
> - _TEMP.as_ptr()
> - },
> + // use associated function syntax for type checking
> + name: ::core::ffi::CStr::as_ptr($name),
> info: $prop,
> offset: ::core::mem::offset_of!($state, $field)
> .try_into()
> .expect("Could not fit offset value to type"),
> - bitnr: 0,
> - bitmask: 0,
> set_default: true,
> defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval.into() },
> - arrayoffset: 0,
> - arrayinfo: ::core::ptr::null(),
> - arrayfieldsize: 0,
> - link_type: ::core::ptr::null(),
> + ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
zeroed() is const only since 1.75.0 [1]. Is there any alternative for
older Rust versions?
[1] https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#method.zeroed
--
Best Regards
Junjie Mao
Il lun 21 ott 2024, 12:39 Junjie Mao <junjie.mao@hotmail.com> ha scritto:
>
> Paolo Bonzini <pbonzini@redhat.com> writes:
>
> > Use the "struct update" syntax to initialize most of the fields to zero,
> > and simplify the handmade type-checking of $name.
> >
> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> > ---
> > rust/qemu-api/src/device_class.rs | 29 ++++++-----------------------
> > 1 file changed, 6 insertions(+), 23 deletions(-)
> >
> > diff --git a/rust/qemu-api/src/device_class.rs b/rust/qemu-api/src/
> device_class.rs
> > index 2219b9f73d0..5aba426d243 100644
> > --- a/rust/qemu-api/src/device_class.rs
> > +++ b/rust/qemu-api/src/device_class.rs
> > @@ -29,44 +29,27 @@ macro_rules! device_class_init {
> > macro_rules! define_property {
> > ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr,
> default = $defval:expr$(,)*) => {
> > $crate::bindings::Property {
> > - name: {
> > - #[used]
> > - static _TEMP: &::core::ffi::CStr = $name;
> > - _TEMP.as_ptr()
> > - },
> > + // use associated function syntax for type checking
> > + name: ::core::ffi::CStr::as_ptr($name),
> > info: $prop,
> > offset: ::core::mem::offset_of!($state, $field)
> > .try_into()
> > .expect("Could not fit offset value to type"),
> > - bitnr: 0,
> > - bitmask: 0,
> > set_default: true,
> > defval: $crate::bindings::Property__bindgen_ty_1 { u:
> $defval.into() },
> > - arrayoffset: 0,
> > - arrayinfo: ::core::ptr::null(),
> > - arrayfieldsize: 0,
> > - link_type: ::core::ptr::null(),
> > + ..unsafe {
> ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init()
> }
>
> zeroed() is const only since 1.75.0 [1]. Is there any alternative for
> older Rust versions?
>
Yes: manual implementation of a Zeroable trait, as in the series I sent
before one. For now I am not worrying about the MSRV, since the
hacks/workarounds from those patches do not become any worse.
Paolo
>
> [1] https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#method.zeroed
>
> --
> Best Regards
> Junjie Mao
>
>
Paolo Bonzini <pbonzini@redhat.com> writes:
> Il lun 21 ott 2024, 12:39 Junjie Mao <junjie.mao@hotmail.com> ha scritto:
>
> Paolo Bonzini <pbonzini@redhat.com> writes:
>
> > Use the "struct update" syntax to initialize most of the fields to zero,
> > and simplify the handmade type-checking of $name.
> >
> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> > ---
> > rust/qemu-api/src/device_class.rs | 29 ++++++-----------------------
> > 1 file changed, 6 insertions(+), 23 deletions(-)
> >
> > diff --git a/rust/qemu-api/src/device_class.rs b/rust/qemu-api/src/device_class.rs
> > index 2219b9f73d0..5aba426d243 100644
> > --- a/rust/qemu-api/src/device_class.rs
> > +++ b/rust/qemu-api/src/device_class.rs
> > @@ -29,44 +29,27 @@ macro_rules! device_class_init {
> > macro_rules! define_property {
> > ($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr, default = $defval:expr$(,)*) => {
> > $crate::bindings::Property {
> > - name: {
> > - #[used]
> > - static _TEMP: &::core::ffi::CStr = $name;
> > - _TEMP.as_ptr()
> > - },
> > + // use associated function syntax for type checking
> > + name: ::core::ffi::CStr::as_ptr($name),
> > info: $prop,
> > offset: ::core::mem::offset_of!($state, $field)
> > .try_into()
> > .expect("Could not fit offset value to type"),
> > - bitnr: 0,
> > - bitmask: 0,
> > set_default: true,
> > defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval.into() },
> > - arrayoffset: 0,
> > - arrayinfo: ::core::ptr::null(),
> > - arrayfieldsize: 0,
> > - link_type: ::core::ptr::null(),
> > + ..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
>
> zeroed() is const only since 1.75.0 [1]. Is there any alternative for
> older Rust versions?
>
> Yes: manual implementation of a Zeroable trait, as in the series I sent before one. For now I am not worrying about the MSRV, since the hacks/workarounds from those patches do not become any worse.
Ah, yes, I should have recalled seeing that earlier.
Reviewed-by: Junjie Mao <junjie.mao@hotmail.com>
--
Best Regards
Junjie Mao
© 2016 - 2026 Red Hat, Inc.