Skip to contentSkip to conversation

rust: add module to convert -errno to io::Error

PATCH Superseded Paolo Bonzini 1 participant 1 message Feb 12, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: add module to convert -errno to io::Error

This is an initial, minimal part of the chardev bindings. It is a common convention in QEMU to return a positive value in case of success, and a negated errno value in case of error. Unfortunately, using errno portably in Rust is a bit complicated; on Unix the errno values are supported natively by io::Error, but on Windows they are not and one would have to use the libc crate. Provide a small module that interprets QEMU's convention and does a best-effort translation to io::Error on Windows.

---
 rust/qemu-api/meson.build       |   1 +
 rust/qemu-api/src/assertions.rs |  28 ++++++
 rust/qemu-api/src/errno.rs      | 161 ++++++++++++++++++++++++++++++++
 rust/qemu-api/src/lib.rs        |   1 +
 rust/qemu-api/src/prelude.rs    |   2 +
 5 files changed, 193 insertions(+)
 create mode 100644 rust/qemu-api/src/errno.rs
rust/qemu-api/meson.build +1 -0
index 26b8500e7d8..607c3010d1d 100644
@@ -22,6 +22,7 @@ _qemu_api_rs = static_library(
       'src/cell.rs',
       'src/chardev.rs',
       'src/c_str.rs',
+      'src/errno.rs',
       'src/irq.rs',
       'src/memory.rs',
       'src/module.rs',
rust/qemu-api/src/assertions.rs +28 -0
index fa1a18de6fe..104dec39774 100644
@@ -92,3 +92,31 @@ fn types_must_be_equal<T, U>(_: T)
         };
     };
 }
+
+/// Assert that an expression matches a pattern.  This can also be
+/// useful to compare enums that do not implement `Eq`.
+///
+/// # Examples
+///
+/// ```
+/// # use qemu_api::assert_match;
+/// // JoinHandle does not implement `Eq`, therefore the result
+/// // does not either.
+/// let result: Result<std::thread::JoinHandle<()>, u32> = Err(42);
+/// assert_match!(result, Err(42));
+/// ```
+#[macro_export]
+macro_rules! assert_match {
+    ($a:expr, $b:pat) => {
+        assert!(
+            match $a {
+                $b => true,
+                _ => false,
+            },
+            "{} = {:?} does not match {}",
+            stringify!($a),
+            $a,
+            stringify!($b)
+        );
+    };
+}
rust/qemu-api/src/errno.rs new file +161 -0

161 lines not on this page · raw message

rust/qemu-api/src/lib.rs +1 -0
index ed1a8f9a2b4..05f38b51d30 100644
@@ -19,6 +19,7 @@
 pub mod callbacks;
 pub mod cell;
 pub mod chardev;
+pub mod errno;
 pub mod irq;
 pub mod memory;
 pub mod module;
rust/qemu-api/src/prelude.rs +2 -0
index fbf0ee23e0b..634acf37a85 100644
@@ -9,6 +9,8 @@
 pub use crate::cell::BqlCell;
 pub use crate::cell::BqlRefCell;
 
+pub use crate::errno;
+
 pub use crate::qdev::DeviceMethods;
 
 pub use crate::qom::InterfaceType;
Signature · 1 line
2.48.1