Function to_result

Source
pub fn to_result(err: c_int) -> Result
Expand description

Converts an integer as returned by a C kernel function to a Result.

If the integer is negative, an Err with an Error as given by Error::from_errno is returned. This means the integer must be >= -MAX_ERRNO.

Otherwise, it returns Ok.

It is a bug to pass an out-of-range negative integer. Err(EINVAL) is returned in such a case.

ยงExamples

This function may be used to easily perform early returns with the ? operator when working with C APIs within Rust abstractions:

fn f() -> Result {
    // SAFETY: ...
    to_result(unsafe { bindings::f1() })?;

    // SAFETY: ...
    to_result(unsafe { bindings::f2() })?;

    // ...

    Ok(())
}