Rust-for-linux archive mirror
 help / color / mirror / Atom feed
From: Dirk Behme <dirk.behme@de.bosch.com>
To: <rust-for-linux@vger.kernel.org>
Cc: <dirk.behme@de.bosch.com>
Subject: [PATCH] [RFC] rust: error: Convert 0 being an error to Result
Date: Thu, 11 Jan 2024 07:44:15 +0100	[thread overview]
Message-ID: <20240111064415.908487-1-dirk.behme@de.bosch.com> (raw)

The existing to_result() takes a (signed) integer from a kernel C function
and converts it to an error if it's negative. Additionally, there are
kernel C functions returning an unsigned integer where 0 is the error case.
For example gen_pool_alloc() and friends. Provide a mechanism to convert
this to Result too.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
---
 rust/kernel/error.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)

I'm unsure if something like this is acceptable. Therefore the RFC.
But I want at least ask ;) In the end this is a slightly modified
copy of the existing to_result().

diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 376280b6a745a..3306b8f590866 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -248,6 +248,16 @@ pub fn to_result(err: core::ffi::c_int) -> Result {
     }
 }
 
+/// Converts an unsigned integer as returned by a C kernel function to EINVAL if it's zero,
+/// and `Ok(u64)` otherwise.
+pub fn to_result_zero(val: core::ffi::c_ulong) -> Result<u64> {
+    if val == 0 {
+        Err(code::EINVAL)
+    } else {
+        Ok(val)
+    }
+}
+
 /// Transform a kernel "error pointer" to a normal pointer.
 ///
 /// Some kernel C API functions return an "error pointer" which optionally
-- 
2.28.0


             reply	other threads:[~2024-01-11  6:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  6:44 Dirk Behme [this message]
2024-01-11  6:59 ` [PATCH] [RFC] rust: error: Convert 0 being an error to Result Greg KH
2024-01-11  7:29   ` Behme Dirk (CM/ESO2)
2024-01-11  7:54     ` Greg KH
2024-01-11  8:14       ` Behme Dirk (CM/ESO2)
2024-01-11  9:46         ` Trevor Gross
2024-01-11 12:17         ` Benno Lossin
2024-01-11 20:52           ` Trevor Gross
2024-01-11  6:59 ` Michael Büsch
2024-01-11 14:14 ` Alice Ryhl
2024-01-12  7:42   ` Behme Dirk (CM/ESO2)

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=20240111064415.908487-1-dirk.behme@de.bosch.com \
    --to=dirk.behme@de.bosch.com \
    --cc=rust-for-linux@vger.kernel.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 read-only IMAP folder(s) and NNTP newsgroup(s).