Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8cd2ce8

Browse files
author
Sven Van Asbroeck
committedJun 4, 2021
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 <thesven73@gmail.com>
1 parent 0b9bcdf commit 8cd2ce8

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed
 

‎rust/helpers.c

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/gfp.h>
88
#include <linux/highmem.h>
99
#include <linux/uio.h>
10+
#include <linux/errname.h>
1011

1112
void rust_helper_BUG(void)
1213
{
@@ -117,6 +118,11 @@ long rust_helper_ptr_err(__force const void *ptr)
117118
}
118119
EXPORT_SYMBOL_GPL(rust_helper_ptr_err);
119120

121+
const char *rust_helper_errname(int err)
122+
{
123+
return errname(err);
124+
}
125+
120126
/* We use bindgen's --size_t-is-usize option to bind the C size_t type
121127
* as the Rust usize type, so we can use it in contexts where Rust
122128
* expects a usize like slice (array) indices. usize is defined to be

‎rust/kernel/error.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ impl Error {
9898

9999
impl fmt::Debug for Error {
100100
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101+
extern "C" {
102+
fn rust_helper_errname(err: c_types::c_int) -> *const c_types::c_char;
103+
}
101104
// SAFETY: FFI call.
102-
#[cfg(CONFIG_SYMBOLIC_ERRNAME)]
103-
let name = unsafe { crate::bindings::errname(-self.0) };
104-
#[cfg(not(CONFIG_SYMBOLIC_ERRNAME))]
105-
let name: *const c_types::c_char = core::ptr::null();
105+
let name = unsafe { rust_helper_errname(-self.0) };
106106

107107
if name.is_null() {
108108
// Print out number if no name can be found.

0 commit comments

Comments
 (0)
Failed to load comments.