Skip to content

Commit 33eca68

Browse files
authored
Merge pull request #580 from ojeda/doc
docs: rust: documentation review
2 parents 930452e + 0f8d584 commit 33eca68

File tree

5 files changed

+144
-159
lines changed

5 files changed

+144
-159
lines changed

Documentation/rust/arch-support.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Arch Support
44
============
55

66
Currently, the Rust compiler (``rustc``) uses LLVM for code generation,
7-
which limits the supported architectures we can target. In addition, support
8-
for building the kernel with LLVM/Clang varies (see :ref:`kbuild_llvm`),
9-
which ``bindgen`` relies on through ``libclang``.
7+
which limits the supported architectures that can be targeted. In addition,
8+
support for building the kernel with LLVM/Clang varies (see :ref:`kbuild_llvm`).
9+
This support is needed for ``bindgen`` which uses ``libclang``.
1010

1111
Below is a general summary of architectures that currently work. Level of
1212
support corresponds to ``S`` values in the ``MAINTAINERS`` file.

Documentation/rust/coding.rst

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,29 @@ This document describes how to write Rust code in the kernel.
99
Coding style
1010
------------
1111

12-
The code is automatically formatted using the ``rustfmt`` tool. This is very
13-
good news!
14-
15-
- If you contribute from time to time to the kernel, you do not need to learn
16-
and remember one more style guide. You will also need less patch roundtrips
17-
to land a change.
18-
19-
- If you are a reviewer or a maintainer, you will not need to spend time on
20-
pointing out style issues anymore.
12+
The code should be formatted using ``rustfmt``. In this way, a person
13+
contributing from time to time to the kernel does not need to learn and
14+
remember one more style guide. More importantly, reviewers and maintainers
15+
do not need to spend time pointing out style issues anymore, and thus
16+
less patch roundtrips may be needed to land a change.
2117

2218
.. note:: Conventions on comments and documentation are not checked by
23-
``rustfmt``. Thus we still need to take care of those: please see
19+
``rustfmt``. Thus those are still needed to be taken care of: please see
2420
:ref:`Documentation/rust/docs.rst <rust_docs>`.
2521

26-
We use the tool's default settings. This means we are following the idiomatic
27-
Rust style. For instance, we use 4 spaces for indentation rather than tabs.
22+
The default settings of ``rustfmt`` are used. This means the idiomatic Rust
23+
style is followed. For instance, 4 spaces are used for indentation rather
24+
than tabs.
2825

29-
Typically, you will want to instruct your editor/IDE to format while you type,
30-
when you save or at commit time. However, if for some reason you want
31-
to reformat the entire kernel Rust sources at some point, you may run::
26+
It is convenient to instruct editors/IDEs to format while typing,
27+
when saving or at commit time. However, if for some reason reformatting
28+
the entire kernel Rust sources is needed at some point, the following can be
29+
run::
3230

3331
make LLVM=1 rustfmt
3432

35-
To check if everything is formatted (printing a diff otherwise), e.g. if you
36-
have configured a CI for a tree as a maintainer, you may run::
33+
It is also possible to check if everything is formatted (printing a diff
34+
otherwise), for instance for a CI, with::
3735

3836
make LLVM=1 rustfmtcheck
3937

@@ -45,31 +43,32 @@ even work with broken code.
4543
Extra lints
4644
-----------
4745

48-
While ``rustc`` is a very helpful compiler, some extra lints and analysis are
46+
While ``rustc`` is a very helpful compiler, some extra lints and analyses are
4947
available via ``clippy``, a Rust linter. To enable it, pass ``CLIPPY=1`` to
50-
the same invocation you use for compilation, e.g.::
48+
the same invocation used for compilation, e.g.::
5149

5250
make LLVM=1 CLIPPY=1
5351

54-
At the moment, we do not enforce a "clippy-free" compilation, so you can treat
55-
the output the same way as the extra warning levels for C, e.g. like ``W=2``.
56-
Still, we use the default configuration, which is relatively conservative, so
57-
it is a good idea to read any output it may produce from time to time and fix
58-
the pointed out issues. The list of enabled lists will be likely tweaked over
59-
time, and extra levels may end up being introduced, e.g. ``CLIPPY=2``.
52+
Please note that Clippy may change code generation, thus it should not be
53+
enabled while building a production kernel.
6054

6155

6256
Abstractions vs. bindings
6357
-------------------------
6458

65-
We don't have abstractions for all the kernel internal APIs and concepts,
66-
but we would like to expand coverage as time goes on. Unless there is
67-
a good reason not to, always use the abstractions instead of calling
68-
the C bindings directly.
59+
Abstractions are Rust code wrapping kernel functionality from the C side.
60+
61+
In order to use functions and types from the C side, bindings are created.
62+
Bindings are the declarations for Rust of those functions and types from
63+
the C side.
64+
65+
For instance, one may write a ``Mutex`` abstraction in Rust which wraps
66+
a ``struct mutex`` from the C side and calls its functions through the bindings.
6967

70-
If you are writing some code that requires a call to an internal kernel API
71-
or concept that isn't abstracted yet, consider providing an (ideally safe)
72-
abstraction for everyone to use.
68+
Abstractions are not available for all the kernel internal APIs and concepts,
69+
but it is intended that coverage is expanded as time goes on. "Leaf" modules
70+
(e.g. drivers) should not use the C bindings directly. Instead, subsystems
71+
should provide as-safe-as-possible abstractions as needed.
7372

7473

7574
Conditional compilation
@@ -80,10 +79,10 @@ configuration:
8079

8180
.. code-block:: rust
8281
83-
#[cfg(CONFIG_X)] // `CONFIG_X` is enabled (`y` or `m`)
84-
#[cfg(CONFIG_X="y")] // `CONFIG_X` is enabled as a built-in (`y`)
85-
#[cfg(CONFIG_X="m")] // `CONFIG_X` is enabled as a module (`m`)
86-
#[cfg(not(CONFIG_X))] // `CONFIG_X` is disabled
82+
#[cfg(CONFIG_X)] // Enabled (`y` or `m`)
83+
#[cfg(CONFIG_X="y")] // Enabled as a built-in (`y`)
84+
#[cfg(CONFIG_X="m")] // Enabled as a module (`m`)
85+
#[cfg(not(CONFIG_X))] // Disabled
8786
8887
8988
Documentation

Documentation/rust/docs.rst

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,43 @@
33
Docs
44
====
55

6-
Rust kernel code is not documented like C kernel code (i.e. via kernel-doc).
7-
Instead, we use the usual system for documenting Rust code: the ``rustdoc``
8-
tool, which uses Markdown (a *very* lightweight markup language).
9-
106
This document describes how to make the most out of the kernel documentation
117
for Rust.
128

9+
Rust kernel code is not documented like C kernel code (i.e. via kernel-doc).
10+
Instead, the usual system for documenting Rust code is used: the ``rustdoc``
11+
tool, which uses Markdown (a lightweight markup language).
12+
13+
To learn Markdown, there are many guides available out there. For instance,
14+
the one at:
15+
16+
https://commonmark.org/help/
17+
1318

1419
Reading the docs
1520
----------------
1621

17-
An advantage of using Markdown is that it attempts to make text look almost as
18-
you would have written it in plain text. This makes the documentation quite
19-
pleasant to read even in its source form.
20-
21-
However, the generated HTML docs produced by ``rustdoc`` provide a *very* nice
22-
experience, including integrated instant search, clickable items (types,
23-
functions, constants, etc. -- including to all the standard Rust library ones
24-
that we use in the kernel, e.g. ``core``), categorization, links to the source
25-
code, etc.
22+
The generated HTML docs produced by ``rustdoc`` include integrated search,
23+
linked items (e.g. types, functions, constants), source code, etc.
2624

27-
Like for the rest of the kernel documentation, pregenerated HTML docs for
28-
the libraries (crates) inside ``rust/`` that are used by the rest of the kernel
29-
are available at `kernel.org`_ (TODO: link when in mainline and generated
30-
alongside the rest of the documentation).
25+
The generated docs may be read at (TODO: link when in mainline and generated
26+
alongside the rest of the documentation):
3127

32-
.. _kernel.org: http://kernel.org/
28+
http://kernel.org/
3329

34-
Otherwise, you can generate them locally. This is quite fast (same order as
35-
compiling the code itself) and you do not need any special tools or environment.
36-
This has the added advantage that they will be tailored to your particular
37-
kernel configuration. To generate them, simply use the ``rustdoc`` target with
38-
the same invocation you use for compilation, e.g.::
30+
The docs can also be easily generated and read locally. This is quite fast
31+
(same order as compiling the code itself) and no special tools or environment
32+
are needed. This has the added advantage that they will be tailored to
33+
the particular kernel configuration used. To generate them, use the ``rustdoc``
34+
target with the same invocation used for compilation, e.g.::
3935

4036
make LLVM=1 rustdoc
4137

4238

4339
Writing the docs
4440
----------------
4541

46-
If you already know Markdown, learning how to write Rust documentation will be
47-
a breeze. If not, understanding the basics is a matter of minutes reading other
48-
code. There are also many guides available out there, a particularly nice one
49-
is at `GitHub`_.
50-
51-
.. _GitHub: https://guides.github.com/features/mastering-markdown/#syntax
52-
53-
This is how a well-documented Rust function may look like (derived from the Rust
54-
standard library)::
42+
This is how a well-documented Rust function may look like::
5543

5644
/// Returns the contained [`Some`] value, consuming the `self` value,
5745
/// without checking that the value is not [`None`].
@@ -77,32 +65,35 @@ standard library)::
7765
}
7866
}
7967

80-
This example showcases a few ``rustdoc`` features and some common conventions
81-
(that we also follow in the kernel):
68+
This example showcases a few ``rustdoc`` features and some conventions followed
69+
in the kernel:
70+
71+
- The first paragraph must be a single sentence briefly describing what
72+
the documented item does. Further explanations must go in extra paragraphs.
8273

83-
* The first paragraph must be a single sentence briefly describing what
84-
the documented item does. Further explanations must go in extra paragraphs.
74+
- Unsafe functions must document their safety preconditions under
75+
a ``# Safety`` section.
8576

86-
* ``unsafe`` functions must document the preconditions needed for a call to be
87-
safe under a ``Safety`` section.
77+
- While not shown here, if a function may panic, the conditions under which
78+
that happens must be described under a ``# Panics`` section.
8879

89-
* While not shown here, if a function may panic, the conditions under which
90-
that happens must be described under a ``Panics`` section. Please note that
91-
panicking should be very rare and used only with a good reason. In almost
92-
all cases, you should use a fallible approach, returning a `Result`.
80+
Please note that panicking should be very rare and used only with a good
81+
reason. In almost all cases, a fallible approach should be used, typically
82+
returning a ``Result``.
9383

94-
* If providing examples of usage would help readers, they must be written in
95-
a section called ``Examples``.
84+
- If providing examples of usage would help readers, they must be written in
85+
a section called ``# Examples``.
9686

97-
* Rust items (functions, types, constants...) will be automatically linked
98-
(``rustdoc`` will find out the URL for you).
87+
- Rust items (functions, types, constants...) must be linked appropriately
88+
(``rustdoc`` will create a link automatically).
9989

100-
* Following the Rust standard library conventions, any ``unsafe`` block must be
101-
preceded by a ``SAFETY`` comment describing why the code inside is sound.
90+
- Any ``unsafe`` block must be preceded by a ``// SAFETY:`` comment
91+
describing why the code inside is sound.
10292

103-
While sometimes the reason might look trivial and therefore unneeded, writing
104-
these comments is not just a good way of documenting what has been taken into
105-
account, but also that there are no *extra* implicit constraints.
93+
While sometimes the reason might look trivial and therefore unneeded, writing
94+
these comments is not just a good way of documenting what has been taken into
95+
account, but most importantly, it provides a way to know that there are
96+
no *extra* implicit constraints.
10697

10798
To learn more about how to write documentation for Rust and extra features,
10899
please take a look at the ``rustdoc`` `book`_.

Documentation/rust/index.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Rust
22
====
33

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.
4+
Documentation related to Rust within the kernel. To start using Rust
5+
in the kernel, please read the
6+
:ref:`Documentation/rust/quick-start.rst <rust_quick_start>` guide.
67

78
.. toctree::
89
:maxdepth: 1

0 commit comments

Comments
 (0)