summary refs log tree commit diff stats
path: root/rust/qemu-api/src/qdev.rs
diff options
context:
space:
mode:
authorZhao Liu <zhao1.liu@intel.com>2025-09-08 12:49:39 +0200
committerPaolo Bonzini <pbonzini@redhat.com>2025-09-17 19:00:56 +0200
commit7da9ee9207c55a783567eb46c71fa89cb5b43461 (patch)
treec8c731f109dc2c0a82b4dbd2435bff9ec047ac2e /rust/qemu-api/src/qdev.rs
parenta71df7e143b57427c1f8a917654e7b0ed1ceb919 (diff)
downloadfocaccia-qemu-7da9ee9207c55a783567eb46c71fa89cb5b43461.tar.gz
focaccia-qemu-7da9ee9207c55a783567eb46c71fa89cb5b43461.zip
rust: vmstate: convert to use builder pattern
Similar to MemoryRegionOps, the builder pattern has two advantages:
1) it makes it possible to build a VMStateDescription that knows which
types it will be invoked on; 2) it provides a way to wrap the callbacks
and let devices avoid "unsafe".

Unfortunately, building a static VMStateDescription requires the
builder methods to be "const", and because the VMStateFields are
*also* static, this requires const_refs_static.  So this requires
Rust 1.83.0.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20250908105005.2119297-8-pbonzini@redhat.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'rust/qemu-api/src/qdev.rs')
-rw-r--r--rust/qemu-api/src/qdev.rs6
1 files changed, 4 insertions, 2 deletions
diff --git a/rust/qemu-api/src/qdev.rs b/rust/qemu-api/src/qdev.rs
index 6a58a00e3f..4dda8c8113 100644
--- a/rust/qemu-api/src/qdev.rs
+++ b/rust/qemu-api/src/qdev.rs
@@ -173,7 +173,7 @@ pub trait DeviceImpl:
     /// A `VMStateDescription` providing the migration format for the device
     /// Not a `const` because referencing statics in constants is unstable
     /// until Rust 1.83.0.
-    fn vmsd() -> Option<&'static VMStateDescription> {
+    fn vmsd() -> Option<VMStateDescription<Self>> {
         None
     }
 }
@@ -225,7 +225,9 @@ impl DeviceClass {
             self.realize = Some(rust_realize_fn::<T>);
         }
         if let Some(vmsd) = <T as DeviceImpl>::vmsd() {
-            self.vmsd = vmsd;
+            // Give a 'static lifetime to the return value of vmsd().
+            // Temporary until vmsd() can be changed into a const.
+            self.vmsd = Box::leak(Box::new(vmsd.get()));
         }
         let prop = <T as DevicePropertiesImpl>::properties();
         if !prop.is_empty() {