Skip to content

Commit

Permalink
rust/kernel: remove redundant Error::from_kernel_errno()
Browse files Browse the repository at this point in the history
It turns out that we don't need an `Error` constructor that
checks the `errno` invariant at runtime. This is because we
trust return values originating from kernel C. And any return
values originating from Rust code can be constucted using
`Error::` constants, which also do not need a runtime check:

```rust
    return Err(Error::EBUSY);
```

If we find we do require this function in the future, we may
restore it simply by reverting this commit.

Signed-off-by: Sven Van Asbroeck <[email protected]>
  • Loading branch information
Sven Van Asbroeck committed Jun 3, 2021
1 parent a21edfe commit 2f2c5ef
Showing 1 changed file with 0 additions and 19 deletions.
19 changes: 0 additions & 19 deletions rust/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,6 @@ impl Error {
/// Bad file number.
pub const EBADF: Self = Error(-(bindings::EBADF as i32));

/// Creates an [`Error`] from a kernel error code.
///
/// It is a bug to pass an out-of-range `errno`. `EINVAL` would
/// be returned in such a case.
pub(crate) fn from_kernel_errno(errno: c_types::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 Error::EINVAL;
}

// INVARIANT: the check above ensures the type invariant
// will hold.
Error(errno)
}

/// Creates an [`Error`] from a kernel error code.
///
/// # Safety
Expand Down

0 comments on commit 2f2c5ef

Please sign in to comment.