mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-27 20:33:54 -06:00
rust: provide safe wrapper for MaybeUninit::zeroed()
MaybeUninit::zeroed() is handy, but it introduces unsafe (and has a pretty heavy syntax in general). Introduce a trait that provides the same functionality while staying within safe Rust. In addition, MaybeUninit::zeroed() is not available as a "const" function until Rust 1.75.0, so this also prepares for having handwritten implementations of the trait until we can assume that version. 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:
parent
c92c447ff0
commit
6e50bde1e1
6 changed files with 35 additions and 10 deletions
|
@ -31,7 +31,7 @@ macro_rules! define_property {
|
|||
offset: ::core::mem::offset_of!($state, $field) as isize,
|
||||
set_default: true,
|
||||
defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval as u64 },
|
||||
..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
|
||||
..$crate::zeroable::Zeroable::ZERO
|
||||
}
|
||||
};
|
||||
($name:expr, $state:ty, $field:expr, $prop:expr, $type:expr$(,)*) => {
|
||||
|
@ -41,7 +41,7 @@ macro_rules! define_property {
|
|||
info: $prop,
|
||||
offset: ::core::mem::offset_of!($state, $field) as isize,
|
||||
set_default: false,
|
||||
..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() }
|
||||
..$crate::zeroable::Zeroable::ZERO
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ macro_rules! declare_properties {
|
|||
len
|
||||
}] = [
|
||||
$($prop),*,
|
||||
unsafe { ::core::mem::MaybeUninit::<$crate::bindings::Property>::zeroed().assume_init() },
|
||||
$crate::zeroable::Zeroable::ZERO,
|
||||
];
|
||||
};
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ macro_rules! vm_state_description {
|
|||
$vname.as_ptr()
|
||||
},)*
|
||||
unmigratable: true,
|
||||
..unsafe { ::core::mem::MaybeUninit::<$crate::bindings::VMStateDescription>::zeroed().assume_init() }
|
||||
..$crate::zeroable::Zeroable::ZERO
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue