-
Notifications
You must be signed in to change notification settings - Fork 430
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
rust/kernel: move from_kernel_result! macro to error.rs #266
rust/kernel: move from_kernel_result! macro to error.rs #266
Conversation
rust/kernel/error.rs
Outdated
} | ||
} | ||
|
||
/// Transforms a `crate::error::Result<T>` to a kernel `C` integer result. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C is not code, i.e.:
/// Transforms a `crate::error::Result<T>` to a kernel `C` integer result. | |
/// Transforms a `crate::error::Result<T>` to a kernel C integer result. |
Does an intra-doc link work here?
/// Transforms a `crate::error::Result<T>` to a kernel `C` integer result. | |
/// Transforms a [`crate::error::Result<T>`] to a kernel C integer result. |
rust/kernel/error.rs
Outdated
|
||
/// Transforms a `crate::error::Result<T>` to a kernel `C` integer result. | ||
/// | ||
/// This is useful when calling Rust functions that return `Result<T>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intra-doc link perhaps?
This macro is well-suited for use elsewhere in the Rust core. Move it out to a suitable place, error.rs. Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com>
This is required to pass the CI. Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com>
5b4b7a4
to
3c009a7
Compare
@@ -106,15 +106,20 @@ impl From<AllocError> for Error { | |||
} | |||
} | |||
|
|||
// # Invariant: `-bindings::MAX_ERRNO` fits in an `i16`. | |||
crate::static_assert!(bindings::MAX_ERRNO <= -(i16::MIN as i32) as u32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the nagging feeling that this method is too contrived. Is there an idiomatic way to verify whether a value fits in a type? I tried Try
or TryFrom
but that doesn't work, because those types can't run inside a static_assert!
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have taken a look at this -- it is indeed a bit painful. After having tried several approaches, I am preparing a PR with a fun one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am grabbing the popcorn !! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See if you like #269 :)
It would allow you to do:
static_assert!((-(bindings::MAX_ERRNO as i128)) fits in i16);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it a lot !! Will definitely rebase to that after it gets merged !! 💯
`from_kernel_result!` could panic if the return integer type (`T`) is unable to hold the negative `errno`. Since `errno`s range from `1` to `4095`, functions returning integer types unable to hold values in the [-4095, -1] range could potentially panic. This includes all unsigned integers, and signed integers with insufficient bits, such as `c_char`. Fix by making sure that the return integer type is always suitable to hold the negative `errno`. Use the Rust type system to verify this at build time. Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com>
3c009a7
to
eb4c6e8
Compare
Corrected minor nit in commit message to restart CI (which failed due to inability to download packages). |
Going to merge this now, and we can improve the static_assert in a follow-up PR. |
This macro is well-suited for use elsewhere in the Rust core.
Move it out to a suitable place, error.rs.
(I'd like to use this in #254)
v1 -> v2