qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-rust@nongnu.org, qemu-block@nongnu.org
Subject: Re: [PATCH 2/2] rust: add module to convert between -errno and io::Error
Date: Tue, 18 Feb 2025 14:57:12 +0100	[thread overview]
Message-ID: <Z7SRuD9Wob8wQK1z@redhat.com> (raw)
In-Reply-To: <20250213143216.3910163-3-pbonzini@redhat.com>

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



  reply	other threads:[~2025-02-18 13:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-13 14:32 [PATCH v2 0/2] rust: add module to convert between -errno and io::Error Paolo Bonzini
2025-02-13 14:32 ` [PATCH 1/2] rust: subprojects: add libc crate Paolo Bonzini
2025-02-19 15:27   ` Zhao Liu
2025-02-13 14:32 ` [PATCH 2/2] rust: add module to convert between -errno and io::Error Paolo Bonzini
2025-02-18 13:57   ` Kevin Wolf [this message]
2025-02-18 17:57   ` Kevin Wolf

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=Z7SRuD9Wob8wQK1z@redhat.com \
    --to=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --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).