Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] A fallible from_kernel_errno with Result<Error> return #347

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions rust/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,16 @@ impl Error {

/// Creates an [`Error`] from a kernel error code.
///
/// It is a bug to pass an out-of-range `errno`. `EINVAL` would
/// It is a bug to pass an out-of-range `errno`. Err(`EINVAL`) would
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// It is a bug to pass an out-of-range `errno`. Err(`EINVAL`) would
/// It is a bug to pass an out-of-range `errno`. `Err(EINVAL)` would

/// be returned in such a case.
pub(crate) fn from_kernel_errno(errno: c_types::c_int) -> Error {
pub(crate) fn from_kernel_errno(errno: c_types::c_int) -> Result<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;
return Err(Error::EINVAL);
}

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

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