Return a Result<()> from the method, and "unwrap" it into error_fatal
in the caller.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/hw/char/pl011/src/device.rs | 4 ++--
rust/hw/core/src/sysbus.rs | 13 ++++++-------
rust/util/src/error.rs | 31 ++++++++++++++++++++++++++++++-
rust/util/src/lib.rs | 2 +-
4 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/device.rs
index 5e9b13fdf92..26be6ef57f2 100644
--- a/rust/hw/char/pl011/src/device.rs
+++ b/rust/hw/char/pl011/src/device.rs
@@ -17,7 +17,7 @@
};
use qom::{prelude::*, ObjectImpl, Owned, ParentField, ParentInit};
use system::{hwaddr, MemoryRegion, MemoryRegionOps, MemoryRegionOpsBuilder};
-use util::{log::Log, log_mask_ln};
+use util::{ResultExt, log::Log, log_mask_ln};
use crate::registers::{self, Interrupt, RegisterOffset};
@@ -697,7 +697,7 @@ pub fn post_load(&self, _version_id: u8) -> Result<(), migration::InvalidError>
let chr = unsafe { Owned::<Chardev>::from(&*chr) };
dev.prop_set_chr("chardev", &chr);
}
- dev.sysbus_realize();
+ dev.sysbus_realize().unwrap_fatal();
dev.mmio_map(0, addr);
dev.connect_irq(0, &irq);
diff --git a/rust/hw/core/src/sysbus.rs b/rust/hw/core/src/sysbus.rs
index 282315fce99..68165e89295 100644
--- a/rust/hw/core/src/sysbus.rs
+++ b/rust/hw/core/src/sysbus.rs
@@ -4,12 +4,13 @@
//! Bindings to access `sysbus` functionality from Rust.
-use std::{ffi::CStr, ptr::addr_of_mut};
+use std::ffi::CStr;
pub use bindings::SysBusDeviceClass;
use common::Opaque;
use qom::{prelude::*, Owned};
use system::MemoryRegion;
+use util::{Error, Result};
use crate::{
bindings,
@@ -107,14 +108,12 @@ fn connect_irq(&self, id: u32, irq: &Owned<IRQState>) {
}
}
- fn sysbus_realize(&self) {
- // TODO: return an Error
+ fn sysbus_realize(&self) -> Result<()> {
assert!(bql::is_locked());
unsafe {
- bindings::sysbus_realize(
- self.upcast().as_mut_ptr(),
- addr_of_mut!(util::bindings::error_fatal),
- );
+ Error::with_errp(|errp| {
+ bindings::sysbus_realize(self.upcast().as_mut_ptr(), errp);
+ })
}
}
}
diff --git a/rust/util/src/error.rs b/rust/util/src/error.rs
index 346577e2e53..4edceff42f3 100644
--- a/rust/util/src/error.rs
+++ b/rust/util/src/error.rs
@@ -38,7 +38,8 @@
ffi::{c_char, c_int, c_void, CStr},
fmt::{self, Display},
ops::Deref,
- panic, ptr,
+ panic,
+ ptr::{self, addr_of_mut},
};
use foreign::{prelude::*, OwnedPointer};
@@ -231,6 +232,34 @@ pub unsafe fn with_errp<T, F: FnOnce(&mut *mut bindings::Error) -> T>(f: F) -> R
}
}
+/// Extension trait for `std::result::Result`, providing extra
+/// methods when the error type can be converted into a QEMU
+/// Error.
+pub trait ResultExt {
+ /// The success type `T` in `Result<T, E>`.
+ type OkType;
+
+ /// Report a fatal error and exit QEMU, or return the success value.
+ /// Note that, unlike [`unwrap()`](std::result::Result::unwrap), this
+ /// is not an abort and can be used for user errors.
+ fn unwrap_fatal(self) -> Self::OkType;
+}
+
+impl<T, E> ResultExt for std::result::Result<T, E>
+where
+ Error: From<E>,
+{
+ type OkType = T;
+
+ fn unwrap_fatal(self) -> T {
+ // SAFETY: errp is valid
+ self.map_err(|err| unsafe {
+ Error::from(err).propagate(addr_of_mut!(bindings::error_fatal))
+ })
+ .unwrap()
+ }
+}
+
impl FreeForeign for Error {
type Foreign = bindings::Error;
diff --git a/rust/util/src/lib.rs b/rust/util/src/lib.rs
index 16c89b95174..d14aa14ca77 100644
--- a/rust/util/src/lib.rs
+++ b/rust/util/src/lib.rs
@@ -6,4 +6,4 @@
pub mod module;
pub mod timer;
-pub use error::{Error, Result};
+pub use error::{Error, Result, ResultExt};
--
2.51.1
On Fri, Oct 31, 2025 at 04:25:39PM +0100, Paolo Bonzini wrote: > Date: Fri, 31 Oct 2025 16:25:39 +0100 > From: Paolo Bonzini <pbonzini@redhat.com> > Subject: [PATCH 4/4] rust: pull error_fatal out of > SysbusDeviceMethods::sysbus_realize > X-Mailer: git-send-email 2.51.1 > > Return a Result<()> from the method, and "unwrap" it into error_fatal > in the caller. > > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> > --- > rust/hw/char/pl011/src/device.rs | 4 ++-- > rust/hw/core/src/sysbus.rs | 13 ++++++------- > rust/util/src/error.rs | 31 ++++++++++++++++++++++++++++++- > rust/util/src/lib.rs | 2 +- > 4 files changed, 39 insertions(+), 11 deletions(-) Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
© 2016 - 2025 Red Hat, Inc.