From: Daniel Sedlak <daniel@sedlak.dev>
To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org,
"Guilherme Augusto Martins da Silva"
<guilhermev2huehue@gmail.com>,
"Daniel Sedlak" <daniel@sedlak.dev>
Subject: [PATCH v3] rust: error: modify `from_errno` to use `try_from_errno`
Date: Sat, 23 Nov 2024 11:48:57 +0100 [thread overview]
Message-ID: <20241123104857.49385-2-daniel@sedlak.dev> (raw)
In-Reply-To: <20241123104857.49385-1-daniel@sedlak.dev>
Modify the from_errno function to use try_from_errno to
reduce code duplication while still maintaining all existing
behavior and error handling and also reduces unsafe code.
Link: https://github.com/Rust-for-Linux/linux/issues/1125
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Co-developed-by: Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com>
Signed-off-by: Guilherme Augusto Martins da Silva <guilhermev2huehue@gmail.com>
Signed-off-by: Daniel Sedlak <daniel@sedlak.dev>
---
rust/kernel/error.rs | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 52c502432447..236b3e4719e6 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -100,20 +100,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: crate::ffi::c_int) -> Error {
- if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
+ pub fn from_errno(errno: core::ffi::c_int) -> Error {
+ 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
next prev parent reply other threads:[~2024-11-23 10:49 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-23 10:48 [PATCH v3 0/1] make `from_errno` use `try_from_errno` Daniel Sedlak
2024-11-23 10:48 ` Daniel Sedlak [this message]
2024-11-23 13:45 ` [PATCH v3] rust: error: modify `from_errno` to " Miguel Ojeda
2024-11-23 19:08 ` Daniel Sedlak
2024-11-23 19:12 ` Miguel Ojeda
2024-11-25 9:01 ` Alice Ryhl
2024-11-25 9:27 ` Miguel Ojeda
2024-11-25 11:31 ` Daniel Sedlak
2024-11-25 11:42 ` Alice Ryhl
2024-11-25 15:03 ` Miguel Ojeda
2024-11-23 13:37 ` [PATCH v3 0/1] make `from_errno` " Miguel Ojeda
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=20241123104857.49385-2-daniel@sedlak.dev \
--to=daniel@sedlak.dev \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=guilhermev2huehue@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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