[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 2/2] rust: add module to convert between -errno and io::Error
From: |
Kevin Wolf |
Subject: |
Re: [PATCH 2/2] rust: add module to convert between -errno and io::Error |
Date: |
Tue, 18 Feb 2025 14:57:12 +0100 |
Am 13.02.2025 um 15:32 hat Paolo Bonzini geschrieben:
> 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 <pbonzini@redhat.com>
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.
> +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