rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] [RFC] rust: error: Convert 0 being an error to Result
@ 2024-01-11  6:44 Dirk Behme
  2024-01-11  6:59 ` Greg KH
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Dirk Behme @ 2024-01-11  6:44 UTC (permalink / raw)
  To: rust-for-linux; +Cc: dirk.behme

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


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-01-12  7:42 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-11  6:44 [PATCH] [RFC] rust: error: Convert 0 being an error to Result Dirk Behme
2024-01-11  6:59 ` 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)

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).