Re: [PATCH 08/12] rust: qdev: support returning errors from realize

2025-05-29 Thread Zhao Liu
On Mon, May 26, 2025 at 04:24:51PM +0200, Paolo Bonzini wrote:
> Date: Mon, 26 May 2025 16:24:51 +0200
> From: Paolo Bonzini 
> Subject: [PATCH 08/12] rust: qdev: support returning errors from realize
> X-Mailer: git-send-email 2.49.0
> 
> Signed-off-by: Paolo Bonzini 
> ---
>  rust/hw/char/pl011/src/device.rs |  5 +++--
>  rust/hw/timer/hpet/src/hpet.rs   |  5 +++--
>  rust/qemu-api/src/qdev.rs| 10 ++
>  3 files changed, 12 insertions(+), 8 deletions(-)

Reviewed-by: Zhao Liu 




[PATCH 08/12] rust: qdev: support returning errors from realize

2025-05-26 Thread Paolo Bonzini
Signed-off-by: Paolo Bonzini 
---
 rust/hw/char/pl011/src/device.rs |  5 +++--
 rust/hw/timer/hpet/src/hpet.rs   |  5 +++--
 rust/qemu-api/src/qdev.rs| 10 ++
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index bde3be65c5b..95fe9a34cd2 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -175,7 +175,7 @@ fn properties() -> &'static [Property] {
 fn vmsd() -> Option<&'static VMStateDescription> {
 Some(&device_class::VMSTATE_PL011)
 }
-const REALIZE: Option = Some(Self::realize);
+const REALIZE: Option qemu_api::Result<()>> = 
Some(Self::realize);
 }
 
 impl ResettablePhasesImpl for PL011State {
@@ -619,9 +619,10 @@ fn event(&self, event: Event) {
 }
 }
 
-fn realize(&self) {
+fn realize(&self) -> qemu_api::Result<()> {
 self.char_backend
 .enable_handlers(self, Self::can_receive, Self::receive, 
Self::event);
+Ok(())
 }
 
 fn reset_hold(&self, _type: ResetType) {
diff --git a/rust/hw/timer/hpet/src/hpet.rs b/rust/hw/timer/hpet/src/hpet.rs
index 779681d6509..55eef669a9d 100644
--- a/rust/hw/timer/hpet/src/hpet.rs
+++ b/rust/hw/timer/hpet/src/hpet.rs
@@ -724,7 +724,7 @@ fn post_init(&self) {
 }
 }
 
-fn realize(&self) {
+fn realize(&self) -> qemu_api::Result<()> {
 if self.int_route_cap == 0 {
 // TODO: Add error binding: warn_report()
 println!("Hpet's hpet-intcap property not initialized");
@@ -751,6 +751,7 @@ fn realize(&self) {
 
 self.init_gpio_in(2, HPETState::handle_legacy_irq);
 self.init_gpio_out(from_ref(&self.pit_enabled));
+Ok(())
 }
 
 fn reset_hold(&self, _type: ResetType) {
@@ -1042,7 +1043,7 @@ fn vmsd() -> Option<&'static VMStateDescription> {
 Some(&VMSTATE_HPET)
 }
 
-const REALIZE: Option = Some(Self::realize);
+const REALIZE: Option qemu_api::Result<()>> = 
Some(Self::realize);
 }
 
 impl ResettablePhasesImpl for HPETState {
diff --git a/rust/qemu-api/src/qdev.rs b/rust/qemu-api/src/qdev.rs
index 1279d7a58d5..81052097071 100644
--- a/rust/qemu-api/src/qdev.rs
+++ b/rust/qemu-api/src/qdev.rs
@@ -12,10 +12,11 @@
 pub use bindings::{ClockEvent, DeviceClass, Property, ResetType};
 
 use crate::{
-bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out, Error, 
ResettableClass},
+bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out, ResettableClass},
 callbacks::FnCall,
 cell::{bql_locked, Opaque},
 chardev::Chardev,
+error::{Error, Result},
 irq::InterruptSource,
 prelude::*,
 qom::{ObjectClass, ObjectImpl, Owned},
@@ -108,7 +109,7 @@ pub trait DeviceImpl: ObjectImpl + ResettablePhasesImpl + 
IsA {
 ///
 /// If not `None`, the parent class's `realize` method is overridden
 /// with the function pointed to by `REALIZE`.
-const REALIZE: Option = None;
+const REALIZE: Option Result<()>> = None;
 
 /// An array providing the properties that the user can set on the
 /// device.  Not a `const` because referencing statics in constants
@@ -134,10 +135,11 @@ fn vmsd() -> Option<&'static VMStateDescription> {
 /// readable/writeable from one thread at any time.
 unsafe extern "C" fn rust_realize_fn(
 dev: *mut bindings::DeviceState,
-_errp: *mut *mut Error,
+errp: *mut *mut bindings::Error,
 ) {
 let state = NonNull::new(dev).unwrap().cast::();
-T::REALIZE.unwrap()(unsafe { state.as_ref() });
+let result = T::REALIZE.unwrap()(unsafe { state.as_ref() });
+unsafe { Error::ok_or_propagate(result, errp); }
 }
 
 unsafe impl InterfaceType for ResettableClass {
-- 
2.49.0