Now that const_refs_to_static can be assumed, convert the members of
the DeviceImpl trait from functions to constants. This lets the
compiler know that they have a 'static lifetime, and removes the
need for the weird "Box::leak()".
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/char/pl011/src/device.rs | 8 ++------
rust/hw/timer/hpet/src/hpet.rs | 10 ++--------
rust/qemu-api/src/qdev.rs | 16 +++++-----------
rust/qemu-api/tests/tests.rs | 8 ++------
4 files changed, 11 insertions(+), 31 deletions(-)
diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index 38373f54e7c..34aa2bbabec 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -169,12 +169,8 @@ impl ObjectImpl for PL011State {
}
impl DeviceImpl for PL011State {
- fn properties() -> &'static [Property] {
- &device_class::PL011_PROPERTIES
- }
- fn vmsd() -> Option<VMStateDescription<Self>> {
- Some(device_class::VMSTATE_PL011)
- }
+ const PROPERTIES: &'static [Property] = &device_class::PL011_PROPERTIES;
+ const VMSTATE: Option<VMStateDescription<Self>> = Some(device_class::VMSTATE_PL011);
const REALIZE: Option<fn(&Self)> = Some(Self::realize);
}
diff --git a/rust/hw/timer/hpet/src/hpet.rs b/rust/hw/timer/hpet/src/hpet.rs
index be3b9afa316..48970a6f9c5 100644
--- a/rust/hw/timer/hpet/src/hpet.rs
+++ b/rust/hw/timer/hpet/src/hpet.rs
@@ -1009,14 +1009,8 @@ impl ObjectImpl for HPETState {
.build();
impl DeviceImpl for HPETState {
- fn properties() -> &'static [Property] {
- &HPET_PROPERTIES
- }
-
- fn vmsd() -> Option<VMStateDescription<Self>> {
- Some(VMSTATE_HPET)
- }
-
+ const PROPERTIES: &'static [Property] = &HPET_PROPERTIES;
+ const VMSTATE: Option<VMStateDescription<Self>> = Some(VMSTATE_HPET);
const REALIZE: Option<fn(&Self)> = Some(Self::realize);
}
diff --git a/rust/qemu-api/src/qdev.rs b/rust/qemu-api/src/qdev.rs
index 09555bbd0e7..9b2bfabdad2 100644
--- a/rust/qemu-api/src/qdev.rs
+++ b/rust/qemu-api/src/qdev.rs
@@ -113,16 +113,12 @@ pub trait DeviceImpl: ObjectImpl + ResettablePhasesImpl + IsA<DeviceState> {
/// An array providing the properties that the user can set on the
/// device. Not a `const` because referencing statics in constants
/// is unstable until Rust 1.83.0.
- fn properties() -> &'static [Property] {
- &[]
- }
+ const PROPERTIES: &'static [Property] = &[];
/// 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<VMStateDescription<Self>> {
- None
- }
+ const VMSTATE: Option<VMStateDescription<Self>> = None;
}
/// # Safety
@@ -168,12 +164,10 @@ pub fn class_init<T: DeviceImpl>(&mut self) {
if <T as DeviceImpl>::REALIZE.is_some() {
self.realize = Some(rust_realize_fn::<T>);
}
- if let Some(vmsd) = <T as DeviceImpl>::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()));
+ if let Some(ref vmsd) = <T as DeviceImpl>::VMSTATE {
+ self.vmsd = vmsd.as_ref();
}
- let prop = <T as DeviceImpl>::properties();
+ let prop = <T as DeviceImpl>::PROPERTIES;
if !prop.is_empty() {
unsafe {
bindings::device_class_set_props_n(self, prop.as_ptr(), prop.len());
diff --git a/rust/qemu-api/tests/tests.rs b/rust/qemu-api/tests/tests.rs
index 3264641d128..db0fd3de99b 100644
--- a/rust/qemu-api/tests/tests.rs
+++ b/rust/qemu-api/tests/tests.rs
@@ -67,12 +67,8 @@ impl ObjectImpl for DummyState {
impl ResettablePhasesImpl for DummyState {}
impl DeviceImpl for DummyState {
- fn properties() -> &'static [Property] {
- &DUMMY_PROPERTIES
- }
- fn vmsd() -> Option<VMStateDescription<Self>> {
- Some(VMSTATE)
- }
+ const PROPERTIES: &'static [Property] = &DUMMY_PROPERTIES;
+ const VMSTATE: Option<VMStateDescription<Self>> = Some(VMSTATE);
}
#[repr(C)]
--
2.49.0
© 2016 - 2025 Red Hat, Inc.