Commit 3d5bef5d authored by Miguel Ojeda's avatar Miguel Ojeda
Browse files

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: default avatarDanilo Krummrich <dakr@kernel.org>
Reviewed-by: default avatarAlice 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: default avatarMiguel Ojeda <ojeda@kernel.org>
parent f7819f79
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -203,6 +203,23 @@ or:
	/// [`struct mutex`]: srctree/include/linux/mutex.h


C FFI types
-----------

Rust kernel code refers to C types, such as ``int``, using type aliases such as
``c_int``, which are readily available from the ``kernel`` prelude. Please do
not use the aliases from ``core::ffi`` -- they may not map to the correct types.

These aliases should generally be referred directly by their identifier, i.e.
as a single segment path. For instance:

.. code-block:: rust

	fn f(p: *const c_char) -> c_int {
	    // ...
	}


Naming
------

+5 −0
Original line number Diff line number Diff line
@@ -14,6 +14,11 @@
#[doc(no_inline)]
pub use core::pin::Pin;

pub use ::ffi::{
    c_char, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint, c_ulong, c_ulonglong,
    c_ushort, c_void,
};

pub use crate::alloc::{flags::*, Box, KBox, KVBox, KVVec, KVec, VBox, VVec, Vec};

#[doc(no_inline)]