rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust-next: make from_errno use try_from_errno
@ 2024-11-04 20:00 Guilherme Augusto Martins da Silva
  2024-11-04 20:14 ` Miguel Ojeda
  0 siblings, 1 reply; 5+ messages in thread
From: Guilherme Augusto Martins da Silva @ 2024-11-04 20:00 UTC (permalink / raw)
  To: rust-for-linux; +Cc: Guilherme Augusto Martins da Silva

Make from_errno use try_from_errno (available in rust-next).  

This also allows to remove unsafe code and a comment or two too.
 this project or the open source license(s) involved.

Signed-off-by: Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com>
---
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 7cd3bbab52f2..077f95cbb6ae 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -101,19 +101,17 @@ impl Error {
     /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
     /// be returned in such a case.
     pub fn from_errno(errno: core::ffi::c_int) -> Error {
-        if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
-            // TODO: Make it a `WARN_ONCE` once available.
-            crate::pr_warn!(
-                "attempted to create `Error` with out of range `errno`: {}",
-                errno
-            );
-            return code::EINVAL;
+        match Error::try_from_errno(errno) {
+            Some(error) => error,
+            None => {
+                // TODO: Make it a `WARN_ONCE` once available.
+                crate::pr_warn!(
+                    "attempted to create `Error` with out of range `errno`: {}",
+                    errno
+                );
+                code::EINVAL
+            }
         }
-
-        // INVARIANT: The check above ensures the type invariant
-        // will hold.
-        // SAFETY: `errno` is checked above to be in a valid range.
-        unsafe { Error::from_errno_unchecked(errno) }
     }

^ permalink raw reply related	[flat|nested] 5+ messages in thread
* [PATCH] rust-next: make from_errno use try_from_errno
@ 2024-11-05 11:48 Guilherme Augusto Martins da Silva
  2024-11-07 12:26 ` Rasmus Villemoes
  2024-11-14 19:42 ` Daniel Sedlak
  0 siblings, 2 replies; 5+ messages in thread
From: Guilherme Augusto Martins da Silva @ 2024-11-05 11:48 UTC (permalink / raw)
  To: rust-for-linux
  Cc: Daniel Sedlak, Guilherme Augusto Martins da Silva, Miguel Ojeda

Modified the from_errno function to use try_from_errno to reduce code duplication while still maintaning all existing behavior and error handling and also reduces unsafe code.

Co-developed-by: Daniel Sedlak <daniel@sedlak.dev>
Signed-off-by: Daniel Sedlak <daniel@sedlak.dev>
Signed-off-by: Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com>
Suggested-by:  Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1125
---
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 7cd3bbab52f2..077f95cbb6ae 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -101,19 +101,17 @@ impl Error {
     /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
     /// be returned in such a case.
     pub fn from_errno(errno: core::ffi::c_int) -> Error {
-        if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
-            // TODO: Make it a `WARN_ONCE` once available.
-            crate::pr_warn!(
-                "attempted to create `Error` with out of range `errno`: {}",
-                errno
-            );
-            return code::EINVAL;
+        match Error::try_from_errno(errno) {
+            Some(error) => error,
+            None => {
+                // TODO: Make it a `WARN_ONCE` once available.
+                crate::pr_warn!(
+                    "attempted to create `Error` with out of range `errno`: {}",
+                    errno
+                );
+                code::EINVAL
+            }
         }
-
-        // INVARIANT: The check above ensures the type invariant
-        // will hold.
-        // SAFETY: `errno` is checked above to be in a valid range.
-        unsafe { Error::from_errno_unchecked(errno) }
     }

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

end of thread, other threads:[~2024-11-14 19:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-04 20:00 [PATCH] rust-next: make from_errno use try_from_errno Guilherme Augusto Martins da Silva
2024-11-04 20:14 ` Miguel Ojeda
  -- strict thread matches above, loose matches on Subject: below --
2024-11-05 11:48 Guilherme Augusto Martins da Silva
2024-11-07 12:26 ` Rasmus Villemoes
2024-11-14 19:42 ` Daniel Sedlak

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