rust: add module to convert -errno to io::Error
Subsystems (2)
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.
Signed-off-by Paolo Bonzini <[email protected]>
--- 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
@@ -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
@@ -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
@@ -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
@@ -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