rust/migration: Check name field in VMStateDescriptionBuilder

The name field is necessary for VMStateDescription, so that it's
necessary to check if it is set when build VMStateDescription.

Since is_null()/as_ref() become rustc v1.84 and pointer cannot cast to
integer in const, use Option<> to check name with a new field in
VMStateDescriptionBuilder instead.

This can be simplified in future when QEMU bumps up rustc version.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20251113051937.4017675-4-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Zhao Liu
2025-11-13 13:19:18 +08:00
committed by Paolo Bonzini
parent bb6e3e743a
commit febdab3d5d

View File

@@ -531,7 +531,11 @@ pub struct VMStateDescription<T>(bindings::VMStateDescription, PhantomData<fn(&T
unsafe impl<T: Sync> Sync for VMStateDescription<T> {}
#[derive(Clone)]
pub struct VMStateDescriptionBuilder<T>(bindings::VMStateDescription, PhantomData<fn(&T)>);
pub struct VMStateDescriptionBuilder<T>(
bindings::VMStateDescription,
Option<*const std::os::raw::c_char>, // the name of VMStateDescription
PhantomData<fn(&T)>,
);
#[derive(Debug)]
pub struct InvalidError;
@@ -592,7 +596,7 @@ unsafe extern "C" fn vmstate_dev_unplug_pending_cb<T, F: for<'a> FnCall<(&'a T,)
impl<T> VMStateDescriptionBuilder<T> {
#[must_use]
pub const fn name(mut self, name_str: &CStr) -> Self {
self.0.name = ::std::ffi::CStr::as_ptr(name_str);
self.1 = Some(::std::ffi::CStr::as_ptr(name_str));
self
}
@@ -718,13 +722,16 @@ impl<T> VMStateDescriptionBuilder<T> {
}
#[must_use]
pub const fn build(self) -> VMStateDescription<T> {
pub const fn build(mut self) -> VMStateDescription<T> {
// FIXME: is_null()/as_ref() become const since v1.84.
assert!(self.1.is_some(), "VMStateDescription requires name field!");
self.0.name = self.1.unwrap();
VMStateDescription::<T>(self.0, PhantomData)
}
#[must_use]
pub const fn new() -> Self {
Self(bindings::VMStateDescription::ZERO, PhantomData)
Self(bindings::VMStateDescription::ZERO, None, PhantomData)
}
}