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

Error representation #195

Open
Diggsey opened this issue Apr 14, 2021 · 4 comments
Open

Error representation #195

Diggsey opened this issue Apr 14, 2021 · 4 comments
Labels
• lib Related to the `rust/` library.

Comments

@Diggsey
Copy link

Diggsey commented Apr 14, 2021

Kernel error numbers are non-zero and positive, so I was wondering if it would make sense to constrain the Error type in that way (making the from c_int conversion fallible).

It could use the NonZero wrapper for now, and in the future might be able to have a niche for negative numbers as well. This would allow a return type of KernelResult<()> to be exactly equivalent to c_int.

@ojeda ojeda added prio: normal • lib Related to the `rust/` library. labels Apr 15, 2021
@jabedude
Copy link

Would this prevent rust code from following the kernel's negative return value convention? (like -ENOMEM, etc)

@alex
Copy link
Member

alex commented Apr 15, 2021 via email

@matthiasbeyer
Copy link

I'm not sure I asked this before: Why isn't there a From<c_int> impl for Error? Because arbitrary integers could then be converted to an Error instance and that is actually not feasible?

@kornelski
Copy link

It's possible to add a bit of type safety and Rust-convenience to FFI functions that return integers as errors:

/// Newtype on int that makes it safe for use in FFI
#[repr(transparent)]
ZeroOnSuccess(c_int)

extern "C" {
   /// FFI definitions can type-safely document error handling convention
   fn may_return_int_on_error() -> ZeroOnSuccess;
}

/// The newtype can have helpers for checking it
impl ZeroOnSuccess {
   fn to_result(&self) -> Result<(), Error> {
      if self.0 == 0 {
        Ok(())
      } else {}
   }
}

may_return_int_on_error().to_result()?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
• lib Related to the `rust/` library.
Development

No branches or pull requests

6 participants