Skip to content

Commit

Permalink
rust: error: declare errors using macro
Browse files Browse the repository at this point in the history
Add a macro to declare errors, which simplifies the work needed to
add each one, avoids repetition of the code and makes it easier to
change the way they are declared.

Signed-off-by: Finn Behrens <me@kloenk.dev>
Reviewed-by: Gary Guo <gary@garyguo.net>
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
  • Loading branch information
Finn Behrens authored and Miguel Ojeda committed Dec 4, 2022
1 parent b13c988 commit 4b0c68b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rust/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ use alloc::collections::TryReserveError;

/// Contains the C-compatible error codes.
pub mod code {
/// Out of memory.
pub const ENOMEM: super::Error = super::Error(-(crate::bindings::ENOMEM as i32));
macro_rules! declare_err {
($err:tt $(,)? $($doc:expr),+) => {
$(
#[doc = $doc]
)*
pub const $err: super::Error = super::Error(-(crate::bindings::$err as i32));
};
}

declare_err!(ENOMEM, "Out of memory.");
}

/// Generic integer kernel error.
Expand Down

0 comments on commit 4b0c68b

Please sign in to comment.