-
Notifications
You must be signed in to change notification settings - Fork 11
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
Start of Kobj support: Semaphores and Threads #12
Changes from 4 commits
fa00bea
823b48a
45192f3
ed875c2
e1196a0
594bbc4
3e362a7
67e456b
d8fbb57
aed43fd
6598cd1
a9ea9a1
59907d7
6b3e831
e71bd6a
096d34b
32b8fbf
907c044
3d2477d
3fd043a
c88307f
1b92ea5
1971feb
97b217f
2ccb628
e8eebb2
a34507f
91d6f33
177a932
1cd9299
266a7a7
fdd97ae
d01153a
7375e44
8bbade3
98a15fe
861cb83
057e169
d11c5d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2024 Linaro LTD | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! # Zephyr errors | ||
//! | ||
//! This module contains an `Error` and `Result` type for use in wrapped Zephyr calls. Many | ||
//! operations in Zephyr return an int result where negative values correspond with errnos. | ||
//! Convert those to a `Result` type where the `Error` condition maps to errnos. | ||
//! | ||
//! Initially, this will just simply wrap the numeric error code, but it might make sense to make | ||
//! this an enum itself, however, it would probably be better to auto-generate this enum instead of | ||
//! trying to maintain the list manually. | ||
|
||
use core::ffi::c_int; | ||
use core::fmt; | ||
|
||
// This is a little messy because the constants end up as u32 from bindgen, although the values are | ||
// negative. | ||
|
||
/// A Zephyr error. | ||
/// | ||
/// Represents an error result returned within Zephyr. | ||
pub struct Error(pub u32); | ||
|
||
impl core::error::Error for Error {} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "zephyr error errno:{}", self.0) | ||
} | ||
} | ||
|
||
impl fmt::Debug for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "zephyr error errno:{}", self.0) | ||
} | ||
} | ||
|
||
/// Wraps a value with a possible Zephyr error. | ||
pub type Result<T> = core::result::Result<T, Error>; | ||
|
||
/// Map a return result from Zephyr into an Result. | ||
/// | ||
/// Negative return results being considered errors. | ||
pub fn to_result(code: c_int) -> Result<c_int> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the c_int in the return needed for compatibility here? As this function already checks for negative values, I would find a I would have also expected a convert to non FFI types, given that we are using the Rust Result, too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is to just keep the type as much like the underlying type, and any conversions can happen by the wrapper that is using this. |
||
if code < 0 { | ||
Err(Error(-code as u32)) | ||
} else { | ||
Ok(code) | ||
} | ||
} | ||
|
||
/// Map a return result, with a void result. | ||
pub fn to_result_void(code: c_int) -> Result<()> { | ||
to_result(code).map(|_| ()) | ||
} | ||
Comment on lines
+42
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder: Do these have to be pub? They seem to be mostly internal helpers? I find it to be slightly awkward API to be exposed to the outside world. So should this maybe be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely a consideration. I still think they are useful for an app user that wants to use a binding that we haven't wrapped yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reducing the API may make it easier for people to use. This extra API seems to be a convenience API, but, at least for me, it just adds extra API for people who will use the crate to learn. One possibility here would be to add a |
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.
This reads like it snuck into an unrelated commit? It does not seem to have anything to do with documentation?
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.
It didn't sneak, it is very intentional. I needed to move the
pub mod kconfig {
up intolib.rs
so that I could add both doc comments and directives to not fail on missing docs on the entries. To do that, I had to remove that from the generated code, and I unindented it as well.