Skip to content

Commit 91a7d3b

Browse files
authored
Merge pull request #35 from Rust-for-Linux/rust-doc
Doc improvements
2 parents c28ec6f + b2b428e commit 91a7d3b

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

Documentation/rust/coding.rst

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. _rust_coding:
2+
3+
Coding
4+
======
5+
6+
This document describes how to write Rust code in the kernel.
7+
8+
9+
Coding style
10+
------------
11+
12+
For the moment, we are following the idiomatic Rust style. For instance,
13+
we use 4 spaces for indentation rather than tabs.
14+
15+
16+
Abstractions vs. bindings
17+
-------------------------
18+
19+
We don't have abstractions for all the kernel internal APIs and concepts,
20+
but we would like to expand coverage as time goes on. Unless there is
21+
a good reason not to, always use the abstractions instead of calling
22+
the C bindings directly.
23+
24+
If you are writing some code that requires a call to an internal kernel API
25+
or concept that isn't abstracted yet, consider providing an (ideally safe)
26+
abstraction for everyone to use.
27+
28+
29+
Conditional compilation
30+
-----------------------
31+
32+
Rust code has access to conditional compilation based on the kernel
33+
configuration:
34+
35+
.. code-block:: rust
36+
37+
#[cfg(CONFIG_X)] // `CONFIG_X` is enabled (`y` or `m`)
38+
#[cfg(CONFIG_X="y")] // `CONFIG_X` is enabled as a built-in (`y`)
39+
#[cfg(CONFIG_X="m")] // `CONFIG_X` is enabled as a module (`m`)
40+
#[cfg(not(CONFIG_X))] // `CONFIG_X` is disabled
41+

Documentation/rust/index.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
Rust
22
====
33

4-
Documentation related to Rust within the kernel. If you are starting out, read the :ref:`Documentation/rust/quick-start.rst <rust_quick_start>` guide.
4+
Documentation related to Rust within the kernel. If you are starting out,
5+
read the :ref:`Documentation/rust/quick-start.rst <rust_quick_start>` guide.
56

67
.. toctree::
78
:maxdepth: 1
89

910
quick-start
11+
coding
1012

1113
.. only:: subproject and html
1214

Documentation/rust/quick-start.rst

+19-6
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,56 @@ Quick Start
55

66
This document describes how to get started with Rust kernel development.
77

8+
89
Requirements
910
------------
1011

11-
A recent nightly Rust toolchain (at least ``rustc``, ``cargo`` and ``rustfmt``) is required, e.g. `nightly-2020-08-27`. In the future, this restriction will be lifted. If you are using ``rustup``, run::
12+
A recent nightly Rust toolchain (at least ``rustc``, ``cargo`` and ``rustfmt``)
13+
is required, e.g. ``nightly-2020-08-27``. In the future, this restriction
14+
will be lifted. If you are using ``rustup``, run::
1215

1316
rustup toolchain install nightly
1417

1518
Otherwise, fetch a standalone installer from:
1619

1720
https://www.rust-lang.org
1821

19-
The sources for the compiler are required to be available. If you are using ``rustup``, run::
22+
The sources for the compiler are required to be available. If you are using
23+
``rustup``, run::
2024

2125
rustup component add rust-src
2226

23-
Otherwise, if you used a standalone installer, you can clone the Rust compiler sources and create a symlink to them in the installation folder of your nightly toolchain::
27+
Otherwise, if you used a standalone installer, you can clone the Rust compiler
28+
sources and create a symlink to them in the installation folder of
29+
your nightly toolchain::
2430

2531
git clone https://github.com/rust-lang/rust
2632
ln -s rust .../rust-nightly/lib/rustlib/src
2733

34+
2835
Testing a simple driver
2936
-----------------------
3037

31-
If the kernel configuration system is able to find ``rustc`` and ``cargo``, it will enable Rust support (``CONFIG_HAS_RUST``). In turn, this will make visible the rest of options that depend on Rust.
38+
If the kernel configuration system is able to find ``rustc`` and ``cargo``,
39+
it will enable Rust support (``CONFIG_HAS_RUST``). In turn, this will make
40+
visible the rest of options that depend on Rust.
3241

33-
A simple driver you can compile to test things out is at ``drivers/char/rust_example`` (``CONFIG_RUST_EXAMPLE``). Enable it and compile the kernel with:
42+
A simple driver you can compile to test things out is at
43+
``drivers/char/rust_example`` (``CONFIG_RUST_EXAMPLE``). Enable it and compile
44+
the kernel with:
3445

3546
make LLVM=1
3647

3748
TODO: drop LLVM=1, allowing to mix GCC with LLVM
3849

50+
3951
Avoiding the network
4052
--------------------
4153

4254
You can prefetch the dependencies that ``cargo`` will download by running::
4355

4456
cargo fetch
4557

46-
in the kernel sources root. After this step, a network connection is not needed anymore.
58+
in the kernel sources root. After this step, a network connection is
59+
not needed anymore.
4760

0 commit comments

Comments
 (0)