rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: error: make `from_errno` use `try_from_errno`
@ 2024-11-04 18:51 Daniel Sedlak
  2024-11-04 20:18 ` Miguel Ojeda
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Sedlak @ 2024-11-04 18:51 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	Daniel Sedlak

Thanks to this simplification we are able to remove
another redundant `unsafe`.

Signed-off-by: Daniel Sedlak <daniel@sedlak.dev>
---
 rust/kernel/error.rs | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 7cd3bbab52f2..2339b1fc7353 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -101,19 +101,16 @@ 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 {
+        if let Some(error) = Self::try_from_errno(errno) {
+            error
+        } else {
             // TODO: Make it a `WARN_ONCE` once available.
             crate::pr_warn!(
                 "attempted to create `Error` with out of range `errno`: {}",
                 errno
             );
-            return code::EINVAL;
+            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) }
     }
 
     /// Creates an [`Error`] from a kernel error code.
-- 
2.47.0


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

* Re: [PATCH] rust: error: make `from_errno` use `try_from_errno`
  2024-11-04 18:51 [PATCH] rust: error: make `from_errno` use `try_from_errno` Daniel Sedlak
@ 2024-11-04 20:18 ` Miguel Ojeda
  0 siblings, 0 replies; 2+ messages in thread
From: Miguel Ojeda @ 2024-11-04 20:18 UTC (permalink / raw)
  To: Daniel Sedlak, Guilherme Augusto Martins da Silva
  Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, rust-for-linux

Hi Daniel,

On Mon, Nov 4, 2024 at 7:52 PM Daniel Sedlak <daniel@sedlak.dev> wrote:
>
> Thanks to this simplification we are able to remove
> another redundant `unsafe`.

Please describe a bit the change, i.e. the "what" (this describes the
"why", which is needed to, so that is good).

> Signed-off-by: Daniel Sedlak <daniel@sedlak.dev>

Please add the Suggested-by and the Link tags as mentioned in the issue.

> -        if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
> +        if let Some(error) = Self::try_from_errno(errno) {
> +            error
> +        } else {

Could we use `let else` here to simplify even further?

Please also see the discussion about who will finally send the patch
and whether to have you both as co-authors at:

    https://lore.kernel.org/rust-for-linux/CANiq72=fcjmDqqYT+wLKNr1qQRgcomaBcaedUxzLXmiLvU5hrQ@mail.gmail.com/

Thanks!

Cheers,
Miguel

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

end of thread, other threads:[~2024-11-04 20:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-04 18:51 [PATCH] rust: error: make `from_errno` use `try_from_errno` Daniel Sedlak
2024-11-04 20:18 ` Miguel Ojeda

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