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 <[email protected]>
---
 rust/migration/src/vmstate.rs | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/rust/migration/src/vmstate.rs b/rust/migration/src/vmstate.rs
index 267f9c8e053a..8087f10832f1 100644
--- a/rust/migration/src/vmstate.rs
+++ b/rust/migration/src/vmstate.rs
@@ -530,7 +530,11 @@ macro_rules! vmstate_subsections {
 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;
@@ -591,7 +595,7 @@ fn from(_value: InvalidError) -> Errno {
 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
     }
 
@@ -717,13 +721,16 @@ pub const fn subsections(mut self, subs: &'static 
VMStateSubsections) -> Self {
     }
 
     #[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)
     }
 }
 
-- 
2.34.1


Reply via email to