rust: error: modify `from_errno` to use `try_from_errno`
PATCH v4 1/1 rust: error: modify `from_errno` to use `try_from_errno`
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.
---
v3 -> v4:
- Now everything should be rebased correctly.
- Link to previous version [4]
v1 -> v3:
Currently, there are three patches for this issue. There was a race condition between me [1] and Guilherme [2] because we both picked it up as a "good first issue" without knowing that one of us had started working on it. We both agreed that one of us would be the main author and the other one would be noted in "Codeveloped-by." However, Guilherme has kept radio silence since [3]. So, I am resubmitting the patch, which builds on top of [3].
rust/kernel/error.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
rust/kernel/error.rs +4 -7
@@ -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:crate ::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.
---
base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37
Daniel
Signature · 1 line
2.47.1
Quoted 9 lines · Daniel Sedlak
Modify the
from_errnofunction to usetry_from_errnoto reduce code duplication while still maintaining all existing behavior and error handling and also reduces unsafe code.Suggested-by Miguel Ojeda <[email protected]>Co-developed-by Guilherme Augusto Martins da Silva <[email protected]>Signed-off-by Guilherme Augusto Martins da Silva <[email protected]>Signed-off-by Daniel Sedlak <[email protected]>
Quoted 57 lines
---
v3 -> v4:
- Now everything should be rebased correctly.
- Link to previous version [4]v1 -> v3:
Currently, there are three patches for this issue. There was a race condition between me [1] and Guilherme [2] because we both picked it up as a "good first issue" without knowing that one of us had started working on it. We both agreed that one of us would be the main author and the other one would be noted in "Codeveloped-by." However, Guilherme has kept radio silence since [3]. So, I am resubmitting the patch, which builds on top of [3].
rust/kernel/error.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)rust/kernel/error.rs +4
-8@@ -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:crate ::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. ---base-commit:
40384c840ea1944d7c5a392e8975ed088ecf0b37Daniel
Signature · 1 line
2.47.1
Miguel Ojeda
AppliedQuoted 10 lines · Daniel Sedlak
Modify the
from_errnofunction to usetry_from_errnoto reduce code duplication while still maintaining all existing behavior and error handling and also reduces unsafe code.Suggested-by Miguel Ojeda <[email protected]>Co-developed-by Guilherme Augusto Martins da Silva <[email protected]>Signed-off-by Guilherme Augusto Martins da Silva <[email protected]>Signed-off-by Daniel Sedlak <[email protected]>
Applied to rust-next -- thanks everyone!
Cheers,
Miguel