Skip to content

Commit 1648677

Browse files
authored
Merge pull request #626 from ojeda/doc
Documentation improvements
2 parents 27fe495 + 2040dd3 commit 1648677

File tree

6 files changed

+185
-146
lines changed

6 files changed

+185
-146
lines changed

Documentation/process/changes.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ is optional) needs to be installed to build the kernel. Other components are
9393
useful for developing.
9494

9595
Please see :ref:`Documentation/rust/quick-start.rst <rust_quick_start>` for
96-
more information.
96+
instructions on how to satify the build requirements of Rust support. In
97+
particular, the Makefile target 'rustavailable' is useful to check why the Rust
98+
toolchain may not be detected.
9799

98100
bindgen (optional)
99101
------------------
@@ -366,8 +368,9 @@ for details about Sphinx requirements.
366368
rustdoc
367369
-------
368370

369-
``rustdoc`` is used to generate Rust documentation. Please see
370-
:ref:`Documentation/rust/docs.rst <rust_docs>` for more information.
371+
``rustdoc`` is used to generate the documentation for Rust code. Please see
372+
:ref:`Documentation/rust/general-information.rst <rust_general_information>`
373+
for more information.
371374

372375
Getting updated software
373376
========================
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
.. _rust_coding_guidelines:
2+
3+
Coding Guidelines
4+
=================
5+
6+
This document describes how to write Rust code in the kernel.
7+
8+
9+
Style & formatting
10+
------------------
11+
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.
17+
18+
.. note:: Conventions on comments and documentation are not checked by
19+
``rustfmt``. Thus those are still needed to be taken care of.
20+
21+
The default settings of ``rustfmt`` are used. This means the idiomatic Rust
22+
style is followed. For instance, 4 spaces are used for indentation rather
23+
than tabs.
24+
25+
It is convenient to instruct editors/IDEs to format while typing,
26+
when saving or at commit time. However, if for some reason reformatting
27+
the entire kernel Rust sources is needed at some point, the following can be
28+
run::
29+
30+
make LLVM=1 rustfmt
31+
32+
It is also possible to check if everything is formatted (printing a diff
33+
otherwise), for instance for a CI, with::
34+
35+
make LLVM=1 rustfmtcheck
36+
37+
Like ``clang-format`` for the rest of the kernel, ``rustfmt`` works on
38+
individual files, and does not require a kernel configuration. Sometimes it may
39+
even work with broken code.
40+
41+
42+
Code documentation
43+
------------------
44+
45+
Rust kernel code is not documented like C kernel code (i.e. via kernel-doc).
46+
Instead, the usual system for documenting Rust code is used: the ``rustdoc``
47+
tool, which uses Markdown (a lightweight markup language).
48+
49+
To learn Markdown, there are many guides available out there. For instance,
50+
the one at:
51+
52+
https://commonmark.org/help/
53+
54+
This is how a well-documented Rust function may look like::
55+
56+
/// Returns the contained [`Some`] value, consuming the `self` value,
57+
/// without checking that the value is not [`None`].
58+
///
59+
/// # Safety
60+
///
61+
/// Calling this method on [`None`] is *[undefined behavior]*.
62+
///
63+
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
64+
///
65+
/// # Examples
66+
///
67+
/// ```
68+
/// let x = Some("air");
69+
/// assert_eq!(unsafe { x.unwrap_unchecked() }, "air");
70+
/// ```
71+
pub unsafe fn unwrap_unchecked(self) -> T {
72+
match self {
73+
Some(val) => val,
74+
75+
// SAFETY: the safety contract must be upheld by the caller.
76+
None => unsafe { hint::unreachable_unchecked() },
77+
}
78+
}
79+
80+
This example showcases a few ``rustdoc`` features and some conventions followed
81+
in the kernel:
82+
83+
- The first paragraph must be a single sentence briefly describing what
84+
the documented item does. Further explanations must go in extra paragraphs.
85+
86+
- Unsafe functions must document their safety preconditions under
87+
a ``# Safety`` section.
88+
89+
- While not shown here, if a function may panic, the conditions under which
90+
that happens must be described under a ``# Panics`` section.
91+
92+
Please note that panicking should be very rare and used only with a good
93+
reason. In almost all cases, a fallible approach should be used, typically
94+
returning a ``Result``.
95+
96+
- If providing examples of usage would help readers, they must be written in
97+
a section called ``# Examples``.
98+
99+
- Rust items (functions, types, constants...) must be linked appropriately
100+
(``rustdoc`` will create a link automatically).
101+
102+
- Any ``unsafe`` block must be preceded by a ``// SAFETY:`` comment
103+
describing why the code inside is sound.
104+
105+
While sometimes the reason might look trivial and therefore unneeded, writing
106+
these comments is not just a good way of documenting what has been taken into
107+
account, but most importantly, it provides a way to know that there are
108+
no *extra* implicit constraints.
109+
110+
To learn more about how to write documentation for Rust and extra features,
111+
please take a look at the ``rustdoc`` `book`_.
112+
113+
.. _book: https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html
114+
115+
116+
Naming
117+
------
118+
119+
Rust kernel code follows the usual Rust naming conventions:
120+
121+
https://rust-lang.github.io/api-guidelines/naming.html
122+
123+
When existing C concepts (e.g. macros, functions, objects...) are wrapped into
124+
a Rust abstraction, a name as close as reasonably possible to the C side should
125+
be used in order to avoid confusion and to improve readability when switching
126+
back and forth between the C and Rust sides. For instance, macros such as
127+
``pr_info`` from C are named the same in the Rust side.
128+
129+
Having said that, casing should be adjusted to follow the Rust naming
130+
conventions, and namespacing introduced by modules and types should not be
131+
repeated in the item names. For instance, when wrapping constants like:
132+
133+
.. code-block:: c
134+
135+
#define GPIO_LINE_DIRECTION_IN 0
136+
#define GPIO_LINE_DIRECTION_OUT 1
137+
138+
The equivalent in Rust may look like (ignoring documentation):
139+
140+
.. code-block:: rust
141+
142+
pub mod gpio {
143+
pub enum LineDirection {
144+
In = bindings::GPIO_LINE_DIRECTION_IN as _,
145+
Out = bindings::GPIO_LINE_DIRECTION_OUT as _,
146+
}
147+
}
148+
149+
That is, the equivalent of ``GPIO_LINE_DIRECTION_IN`` would be referred to as
150+
``gpio::LineDirection::In``. In particular, it should not be named
151+
``gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN``.

Documentation/rust/docs.rst

Lines changed: 0 additions & 104 deletions
This file was deleted.

Documentation/rust/coding.rst renamed to Documentation/rust/general-information.rst

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
1-
.. _rust_coding:
1+
.. _rust_general_information:
22

3-
Coding
4-
======
3+
General Information
4+
===================
55

6-
This document describes how to write Rust code in the kernel.
6+
This document contains useful information to know when working with
7+
the Rust support in the kernel.
78

89

9-
Coding style
10-
------------
10+
Code documentation
11+
------------------
1112

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.
13+
Rust kernel code is documented using ``rustdoc``, its built-in documentation
14+
generator.
1715

18-
.. note:: Conventions on comments and documentation are not checked by
19-
``rustfmt``. Thus those are still needed to be taken care of: please see
20-
:ref:`Documentation/rust/docs.rst <rust_docs>`.
16+
The generated HTML docs include integrated search, linked items (e.g. types,
17+
functions, constants), source code, etc. They may be read at (TODO: link when
18+
in mainline and generated alongside the rest of the documentation):
2119

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.
20+
http://kernel.org/
2521

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::
22+
The docs can also be easily generated and read locally. This is quite fast
23+
(same order as compiling the code itself) and no special tools or environment
24+
are needed. This has the added advantage that they will be tailored to
25+
the particular kernel configuration used. To generate them, use the ``rustdoc``
26+
target with the same invocation used for compilation, e.g.::
3027

31-
make LLVM=1 rustfmt
28+
make LLVM=1 rustdoc
3229

33-
It is also possible to check if everything is formatted (printing a diff
34-
otherwise), for instance for a CI, with::
30+
To read the docs locally in your web browser, run e.g.::
3531

36-
make LLVM=1 rustfmtcheck
32+
xdg-open rust/doc/kernel/index.html
3733

38-
Like ``clang-format`` for the rest of the kernel, ``rustfmt`` works on
39-
individual files, and does not require a kernel configuration. Sometimes it may
40-
even work with broken code.
34+
To learn about how to write the documentation, please see the coding guidelines
35+
at :ref:`Documentation/rust/coding-guidelines.rst <rust_coding_guidelines>`.
4136

4237

4338
Extra lints
@@ -83,9 +78,3 @@ configuration:
8378
#[cfg(CONFIG_X="y")] // Enabled as a built-in (`y`)
8479
#[cfg(CONFIG_X="m")] // Enabled as a module (`m`)
8580
#[cfg(not(CONFIG_X))] // Disabled
86-
87-
88-
Documentation
89-
-------------
90-
91-
Please see :ref:`Documentation/rust/docs.rst <rust_docs>`.

Documentation/rust/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ in the kernel, please read the
99
:maxdepth: 1
1010

1111
quick-start
12-
coding
13-
docs
12+
general-information
13+
coding-guidelines
1414
arch-support
1515

1616
.. only:: subproject and html

Documentation/rust/quick-start.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ rustfmt
113113

114114
The ``rustfmt`` tool is used to automatically format all the Rust kernel code,
115115
including the generated C bindings (for details, please see
116-
:ref:`Documentation/rust/coding.rst <rust_coding>`).
116+
:ref:`Documentation/rust/coding-guidelines.rst <rust_coding_guidelines>`).
117117

118118
If ``rustup`` is being used, its ``default`` profile already installs the tool,
119119
thus nothing needs to be done. If another profile is being used, the component
@@ -129,7 +129,7 @@ clippy
129129

130130
``clippy`` is a Rust linter. Running it provides extra warnings for Rust code.
131131
It can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see
132-
:ref:`Documentation/rust/coding.rst <rust_coding>`).
132+
:ref:`Documentation/rust/general-information.rst <rust_general_information>`).
133133

134134
If ``rustup`` is being used, its ``default`` profile already installs the tool,
135135
thus nothing needs to be done. If another profile is being used, the component
@@ -159,7 +159,7 @@ rustdoc
159159

160160
``rustdoc`` is the documentation tool for Rust. It generates pretty HTML
161161
documentation for Rust code (for details, please see
162-
:ref:`Documentation/rust/docs.rst <rust_docs>`).
162+
:ref:`Documentation/rust/general-information.rst <rust_general_information>`).
163163

164164
``rustdoc`` is also used to test the examples provided in documented Rust code
165165
(called doctests or documentation tests). The ``rusttest`` Make target uses

0 commit comments

Comments
 (0)