From 8cd2ce8edd7af8178d1115a8270539353d29980c Mon Sep 17 00:00:00 2001 From: Sven Van Asbroeck Date: Fri, 4 Jun 2021 08:58:50 -0400 Subject: [PATCH] rust/kernel: remove config `#ifdef` in Error.rs file The use of `#ifdef CONFIG_` statements in .c/.rs files is deprecated: it makes the code much more unmaintainable over time. See: https://lkml.org/lkml/2021/6/3/564 Use a Rust-C helper instead to leverage automatic `CONFIG` selection in C kernel headers. Signed-off-by: Sven Van Asbroeck --- rust/helpers.c | 6 ++++++ rust/kernel/error.rs | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/rust/helpers.c b/rust/helpers.c index 8c4541091a15ae..3526129f41c79a 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -7,6 +7,7 @@ #include #include #include +#include void rust_helper_BUG(void) { @@ -117,6 +118,11 @@ long rust_helper_ptr_err(__force const void *ptr) } EXPORT_SYMBOL_GPL(rust_helper_ptr_err); +const char *rust_helper_errname(int err) +{ + return errname(err); +} + /* We use bindgen's --size_t-is-usize option to bind the C size_t type * as the Rust usize type, so we can use it in contexts where Rust * expects a usize like slice (array) indices. usize is defined to be diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 6ac8630c8fb203..6841adaa31beb6 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -98,11 +98,11 @@ impl Error { impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + extern "C" { + fn rust_helper_errname(err: c_types::c_int) -> *const c_types::c_char; + } // SAFETY: FFI call. - #[cfg(CONFIG_SYMBOLIC_ERRNAME)] - let name = unsafe { crate::bindings::errname(-self.0) }; - #[cfg(not(CONFIG_SYMBOLIC_ERRNAME))] - let name: *const c_types::c_char = core::ptr::null(); + let name = unsafe { rust_helper_errname(-self.0) }; if name.is_null() { // Print out number if no name can be found.