rust: add module to convert between -errno and io::Error
Subsystems (2)
v2 cover rust: add module to convert between -errno and io::Error
Wrap access to errno, for use in the block layer and character device bindings.
Supersedes: <[email protected]>
v1->v2:
- use the libc crate
- provide separate From<Errno> implementation for io::ErrorKind
- hide GetErrno trait inside a submodule
- add into_neg_errno() and corresponding tests
- add more doctests
- make Errno field public, so that Errno can have other From<...> implementations (e.g. going from *mut Error to Errno).
Paolo Bonzini (2):
rust: subprojects: add libc crate
rust: add module to convert between -errno and io::Error
rust/Cargo.lock | 7 + rust/qemu-api/Cargo.toml | 1 + rust/qemu-api/meson.build | 4 + rust/qemu-api/src/assertions.rs | 28 ++ rust/qemu-api/src/errno.rs | 342 ++++++++++++++++++ rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/prelude.rs | 2 + scripts/archive-source.sh | 2 +- scripts/make-release | 2 +- subprojects/libc-0.2-rs.wrap | 7 + .../packagefiles/libc-0.2-rs/meson.build | 36 ++ 11 files changed, 430 insertions(+), 2 deletions(-) create mode 100644 rust/qemu-api/src/errno.rs create mode 100644 subprojects/libc-0.2-rs.wrap create mode 100644 subprojects/packagefiles/libc-0.2-rs/meson.build
Signature · 1 line
2.48.1
PATCH v2 1/2 rust: subprojects: add libc crate
This allows access to errno values.
--- rust/Cargo.lock | 7 ++++ rust/qemu-api/Cargo.toml | 1 + scripts/archive-source.sh | 2 +- scripts/make-release | 2 +- subprojects/libc-0.2-rs.wrap | 7 ++++ .../packagefiles/libc-0.2-rs/meson.build | 36 +++++++++++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 subprojects/libc-0.2-rs.wrap create mode 100644 subprojects/packagefiles/libc-0.2-rs/meson.build
rust/Cargo.lock +7 -0
@@ -54,6 +54,12 @@ dependencies = [ "either", ] +[[package]] +name = "libc" +version = "0.2.162" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" + [[package]] name = "pl011" version = "0.1.0"
@@ -100,6 +106,7 @@ dependencies = [ name = "qemu_api" version = "0.1.0" dependencies = [ + "libc", "qemu_api_macros", "version_check", ]
rust/qemu-api/Cargo.toml +1 -0
@@ -16,6 +16,7 @@ rust-version = "1.63.0" [dependencies] qemu_api_macros = { path = "../qemu-api-macros" } +libc = "0.2.162" [build-dependencies] version_check = "~0.9"
scripts/archive-source.sh +1 -1
@@ -28,7 +28,7 @@ sub_file="${sub_tdir}/submodule.tar" # different to the host OS. subprojects="keycodemapdb libvfio-user berkeley-softfloat-3 berkeley-testfloat-3 arbitrary-int-1-rs bilge-0.2-rs - bilge-impl-0.2-rs either-1-rs itertools-0.11-rs proc-macro2-1-rs + bilge-impl-0.2-rs either-1-rs itertools-0.11-rs libc-0.2-rs proc-macro2-1-rs proc-macro-error-1-rs proc-macro-error-attr-1-rs quote-1-rs syn-2-rs unicode-ident-1-rs" sub_deinit=""
scripts/make-release +1 -1
@@ -40,7 +40,7 @@ fi # Only include wraps that are invoked with subproject() SUBPROJECTS="libvfio-user keycodemapdb berkeley-softfloat-3 berkeley-testfloat-3 arbitrary-int-1-rs bilge-0.2-rs - bilge-impl-0.2-rs either-1-rs itertools-0.11-rs proc-macro2-1-rs + bilge-impl-0.2-rs either-1-rs itertools-0.11-rs libc-0.2-rs proc-macro2-1-rs proc-macro-error-1-rs proc-macro-error-attr-1-rs quote-1-rs syn-2-rs unicode-ident-1-rs"
subprojects/libc-0.2-rs.wrap new file +7 -0
@@ -0,0 +1,7 @@ +[wrap-file] +directory = libc-0.2.162 +source_url = https://crates.io/api/v1/crates/libc/0.2.162/download +source_filename = libc-0.2.162.tar.gz +source_hash = 18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398 +#method = cargo +patch_directory = libc-0.2-rs
subprojects/packagefiles/libc-0.2-rs/meson.build new file +37 -0
@@ -0,0 +1,37 @@ +project('libc-0.2-rs', 'rust', + meson_version: '>=1.5.0', + version: '0.2.162', + license: 'MIT OR Apache-2.0', + default_options: []) + +_libc_rs = static_library( + 'libc', + files('src/lib.rs'), + gnu_symbol_visibility: 'hidden', + override_options: ['rust_std=2015', 'build.rust_std=2015'], + rust_abi: 'rust', + rust_args: [ + '--cap-lints', 'allow', + '--cfg', 'freebsd11', + '--cfg', 'libc_priv_mod_use', + '--cfg', 'libc_union', + '--cfg', 'libc_const_size_of', + '--cfg', 'libc_align', + '--cfg', 'libc_int128', + '--cfg', 'libc_core_cvoid', + '--cfg', 'libc_packedN', + '--cfg', 'libc_cfg_target_vendor', + '--cfg', 'libc_non_exhaustive', + '--cfg', 'libc_long_array', + '--cfg', 'libc_ptr_addr_of', + '--cfg', 'libc_underscore_const_names', + '--cfg', 'libc_const_extern_fn', + ], + dependencies: [], +) + +libc_dep = declare_dependency( + link_with: _libc_rs, +) + +meson.override_dependency('libc-0.2-rs', libc_dep)
Signature · 1 line
2.48.1
Re: [PATCH 1/2] rust: subprojects: add libc crate
Quoted 8 lines · Paolo Bonzini
Date: Thu, 13 Feb 2025 15:32:15 +0100
From: Paolo Bonzini <[email protected]>
Subject: [PATCH 1/2] rust: subprojects: add libc crate
X-Mailer: git-send-email 2.48.1This allows access to errno values.
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 10 lines
--- rust/Cargo.lock | 7 ++++ rust/qemu-api/Cargo.toml | 1 + scripts/archive-source.sh | 2 +- scripts/make-release | 2 +- subprojects/libc-0.2-rs.wrap | 7 ++++ .../packagefiles/libc-0.2-rs/meson.build | 36 +++++++++++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 subprojects/libc-0.2-rs.wrap create mode 100644 subprojects/packagefiles/libc-0.2-rs/meson.build
Missing to update subprojects/.gitignore. With this nit fixed,
PATCH v2 2/2 rust: add module to convert between -errno and io::Error
In reply to Paolo BonziniIt 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; so, use the libc crate.
This is a set of utility functions that are used by both chardev and block layer bindings.
--- rust/qemu-api/meson.build | 4 + rust/qemu-api/src/assertions.rs | 28 +++ rust/qemu-api/src/errno.rs | 342 ++++++++++++++++++++++++++++++++ rust/qemu-api/src/lib.rs | 1 + rust/qemu-api/src/prelude.rs | 2 + 5 files changed, 377 insertions(+) create mode 100644 rust/qemu-api/src/errno.rs
rust/qemu-api/meson.build +4 -0
@@ -2,6 +2,8 @@ _qemu_api_cfg = run_command(rustc_args, '--config-headers', config_host_h, '--features', files('Cargo.toml'), capture: true, check: true).stdout().strip().splitlines() +libc_dep = dependency('libc-0.2-rs') + # _qemu_api_cfg += ['--cfg', 'feature="allocator"'] if rustc.version().version_compare('>=1.77.0') _qemu_api_cfg += ['--cfg', 'has_offset_of']
@@ -22,6 +24,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',
@@ -39,6 +42,7 @@ _qemu_api_rs = static_library( override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', rust_args: _qemu_api_cfg, + dependencies: libc_dep, ) rust.test('rust-qemu-api-tests', _qemu_api_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 +342 -0
342 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
Am 13.02.2025 um 15:32 hat Paolo Bonzini geschrieben:
Quoted 10 lines
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; so, use the libc crate.This is a set of utility functions that are used by both chardev and block layer bindings.
Signed-off-by Paolo Bonzini <[email protected]>
In my series, this allows a simplification of sorts:
let mapping = match qemu_co_run_future(s.map(&req)) {
Ok(mapping) => mapping,
- Err(e) => {
- return -e
- .raw_os_error()
- .unwrap_or(bindings::EIO as std::os::raw::c_int)
- }
+ Err(e) => return -i32::from(Errno::from(e).0),
};
Not really nice yet, though.
rust/qemu-api/src/errno.rs line 234
Quoted 6 lines
rust/qemu-api/src/errno.rs new file +6
-0+pub fn into_neg_errno <T :MergeErrno ,E :Into <Errno >>(value:Result <T ,E >) ->T ::Out { +match value { +Ok (x) => x.map_ok (), +Err (err) => -T ::Out ::from (err.into ().0 ), + } +}
How about making this a method of a trait that is implemented for Result<T, E> similar to what you already have, but also directly for io::Error (or actually E: Into<Errno>)?
Then the above could become:
let mapping = match qemu_co_run_future(s.map(&req)) {
Ok(mapping) => mapping,
Err(e) => return e.into_neg_errno(),
};Kevin
Am 13.02.2025 um 15:32 hat Paolo Bonzini geschrieben:
Quoted 10 lines
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; so, use the libc crate.This is a set of utility functions that are used by both chardev and block layer bindings.
Signed-off-by Paolo Bonzini <[email protected]>
'make rustdoc' fails for me with this patch applied:
error: unresolved link to io::Result
--> qemu-api/src/errno.rs:105:55
|
105 | /// A signed type that can be converted into an [`io::Result`]
| ^^^^^^^^^^ no item named `io` in scope
| = note: requested on the command line with -D rustdoc::broken-intra-doc-links
error: unresolved link to io::Result
--> qemu-api/src/errno.rs:114:46
|
114 | /// A type that can be taken out of an [`io::Result`] and converted
| ^^^^^^^^^^ no item named `io` in scopeerror: unresolved link to into_neg_errno
--> qemu-api/src/errno.rs:118:15
|
118 | /// [`into_neg_errno`].
| ^^^^^^^^^^^^^^ no item named `into_neg_errno` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`error: could not document qemu_api
Kevin