From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: armbru@redhat.com, marcandre.lureau@redhat.com, qemu-rust@nongnu.org
Subject: [PATCH 10/19] rust/util: replace Error::err_or_unit/err_or_else with Error::with_errp
Date: Fri, 10 Oct 2025 17:09:55 +0200 [thread overview]
Message-ID: <20251010151006.791038-11-pbonzini@redhat.com> (raw)
In-Reply-To: <20251010151006.791038-1-pbonzini@redhat.com>
Introduce a simpler function that hides the creation of the Error**.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/util/src/error.rs | 52 ++++++++++++++++--------------------------
1 file changed, 20 insertions(+), 32 deletions(-)
diff --git a/rust/util/src/error.rs b/rust/util/src/error.rs
index bdbf2634170..5a0dd7786b5 100644
--- a/rust/util/src/error.rs
+++ b/rust/util/src/error.rs
@@ -14,8 +14,7 @@
//! [`ptr_or_propagate`](crate::Error::ptr_or_propagate) can be used to build
//! a C return value while also propagating an error condition
//!
-//! * [`err_or_else`](crate::Error::err_or_else) and
-//! [`err_or_unit`](crate::Error::err_or_unit) can be used to build a `Result`
+//! * [`with_errp`](crate::Error::with_errp) can be used to build a `Result`
//!
//! This module is most commonly used at the boundary between C and Rust code;
//! other code will usually access it through the
@@ -208,35 +207,21 @@ pub unsafe fn propagate(self, errp: *mut *mut bindings::Error) {
}
}
- /// Convert a C `Error*` into a Rust `Result`, using
- /// `Ok(())` if `c_error` is NULL. Free the `Error*`.
+ /// Pass a C `Error*` to the closure, and convert the result
+ /// (either the return value of the closure, or the error)
+ /// into a Rust `Result`.
///
/// # Safety
///
- /// `c_error` must be `NULL` or valid; typically it was initialized
- /// with `ptr::null_mut()` and passed by reference to a C function.
- pub unsafe fn err_or_unit(c_error: *mut bindings::Error) -> Result<()> {
- // SAFETY: caller guarantees c_error is valid
- unsafe { Self::err_or_else(c_error, || ()) }
- }
+ /// One exit from `f`, `c_error` must be unchanged or point to a
+ /// valid C [`struct Error`](bindings::Error).
+ pub unsafe fn with_errp<T, F: FnOnce(&mut *mut bindings::Error) -> T>(f: F) -> Result<T> {
+ let mut c_error: *mut bindings::Error = ptr::null_mut();
- /// Convert a C `Error*` into a Rust `Result`, calling `f()` to
- /// obtain an `Ok` value if `c_error` is NULL. Free the `Error*`.
- ///
- /// # Safety
- ///
- /// `c_error` must be `NULL` or point to a valid C [`struct
- /// Error`](bindings::Error); typically it was initialized with
- /// `ptr::null_mut()` and passed by reference to a C function.
- pub unsafe fn err_or_else<T, F: FnOnce() -> T>(
- c_error: *mut bindings::Error,
- f: F,
- ) -> Result<T> {
- // SAFETY: caller guarantees c_error is valid
- let err = unsafe { Option::<Self>::from_foreign(c_error) };
- match err {
- None => Ok(f()),
- Some(err) => Err(err),
+ // SAFETY: guaranteed by the postcondition of `f`
+ match (f(&mut c_error), unsafe { c_error.into_native() }) {
+ (result, None) => Ok(result),
+ (_, Some(err)) => Err(err),
}
}
}
@@ -421,13 +406,16 @@ fn test_ptr_or_propagate() {
}
#[test]
- fn test_err_or_unit() {
+ fn test_with_errp() {
unsafe {
- let result = Error::err_or_unit(ptr::null_mut());
- assert_match!(result, Ok(()));
+ let result = Error::with_errp(|_errp| true);
+ assert_match!(result, Ok(true));
- let err = error_for_test(c"msg");
- let err = Error::err_or_unit(err.into_inner()).unwrap_err();
+ let err = Error::with_errp(|errp| {
+ *errp = error_for_test(c"msg").into_inner();
+ false
+ })
+ .unwrap_err();
assert_eq!(&*format!("{err}"), "msg");
}
}
--
2.51.0
next prev parent reply other threads:[~2025-10-10 15:12 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-10 15:09 [PATCH 00/19] rust: QObject and QAPI bindings Paolo Bonzini
2025-10-10 15:09 ` [PATCH 01/19] util: add ensure macro Paolo Bonzini
2025-10-10 15:09 ` [PATCH 02/19] rust/util: use anyhow's native chaining capabilities Paolo Bonzini
2025-10-10 15:09 ` [PATCH 03/19] rust: do not add qemuutil to Rust crates Paolo Bonzini
2025-10-10 15:09 ` [PATCH 04/19] rust/qobject: add basic bindings Paolo Bonzini
2025-10-10 15:09 ` [PATCH 05/19] subprojects: add serde Paolo Bonzini
2025-10-10 15:09 ` [PATCH 06/19] rust/qobject: add Serialize implementation Paolo Bonzini
2025-10-10 15:09 ` [PATCH 07/19] rust/qobject: add Serializer (to_qobject) implementation Paolo Bonzini
2025-10-10 15:09 ` [PATCH 08/19] rust/qobject: add Deserialize implementation Paolo Bonzini
2025-10-10 15:09 ` [PATCH 09/19] rust/qobject: add Deserializer (from_qobject) implementation Paolo Bonzini
2025-10-10 15:09 ` Paolo Bonzini [this message]
2025-10-10 15:09 ` [PATCH 11/19] rust/qobject: add from/to JSON bindings for QObject Paolo Bonzini
2025-10-10 15:09 ` [PATCH 12/19] rust/qobject: add Display/Debug Paolo Bonzini
2025-10-10 15:09 ` [PATCH 13/19] scripts/qapi: add QAPISchemaIfCond.rsgen() Paolo Bonzini
2025-10-10 15:09 ` [PATCH 14/19] scripts/qapi: generate high-level Rust bindings Paolo Bonzini
2025-10-10 15:10 ` [PATCH 15/19] scripts/qapi: add serde attributes Paolo Bonzini
2025-10-10 15:10 ` [PATCH 16/19] scripts/qapi: strip trailing whitespaces Paolo Bonzini
2025-10-10 15:10 ` [PATCH 17/19] scripts/rustc_args: add --no-strict-cfg Paolo Bonzini
2025-10-10 15:10 ` [PATCH 18/19] rust/util: build QAPI types Paolo Bonzini
2025-10-10 15:10 ` [PATCH 19/19] rust/tests: QAPI integration tests Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251010151006.791038-11-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=armbru@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).