Skip to content

Commit 3d5bef5

Browse files
committed
rust: add C FFI types to the prelude
Rust kernel code is supposed to use the custom mapping of C FFI types, i.e. those from the `ffi` crate, rather than the ones coming from `core`. Thus, to minimize mistakes and to simplify the code everywhere, just provide them in the `kernel` prelude and ask in the Coding Guidelines to use them directly, i.e. as a single segment path. After this lands, we can start cleaning up the existing users. Ideally, we would use something like Clippy's `disallowed-types` to prevent the use of the `core` ones, but that one sees through aliases. Link: https://lore.kernel.org/rust-for-linux/CANiq72kc4gzfieD-FjuWfELRDXXD2vLgPv4wqk3nt4pjdPQ=qg@mail.gmail.com/ Reviewed-by: Danilo Krummrich <dakr@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20250413005650.1745894-1-ojeda@kernel.org [ Reworded content of the documentation to focus on how to use the aliases first. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent f7819f7 commit 3d5bef5

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Documentation/rust/coding-guidelines.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ or:
203203
/// [`struct mutex`]: srctree/include/linux/mutex.h
204204
205205
206+
C FFI types
207+
-----------
208+
209+
Rust kernel code refers to C types, such as ``int``, using type aliases such as
210+
``c_int``, which are readily available from the ``kernel`` prelude. Please do
211+
not use the aliases from ``core::ffi`` -- they may not map to the correct types.
212+
213+
These aliases should generally be referred directly by their identifier, i.e.
214+
as a single segment path. For instance:
215+
216+
.. code-block:: rust
217+
218+
fn f(p: *const c_char) -> c_int {
219+
// ...
220+
}
221+
222+
206223
Naming
207224
------
208225

rust/kernel/prelude.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#[doc(no_inline)]
1515
pub use core::pin::Pin;
1616

17+
pub use ::ffi::{
18+
c_char, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint, c_ulong, c_ulonglong,
19+
c_ushort, c_void,
20+
};
21+
1722
pub use crate::alloc::{flags::*, Box, KBox, KVBox, KVVec, KVec, VBox, VVec, Vec};
1823

1924
#[doc(no_inline)]

0 commit comments

Comments
 (0)