|
| 1 | +.. _rust_docs: |
| 2 | + |
| 3 | +Docs |
| 4 | +==== |
| 5 | + |
| 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 | + |
| 10 | +This document describes how to make the most out of the kernel documentation |
| 11 | +for Rust. |
| 12 | + |
| 13 | + |
| 14 | +Reading the docs |
| 15 | +---------------- |
| 16 | + |
| 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. |
| 26 | + |
| 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`_. |
| 30 | + |
| 31 | +// TODO: link when ready |
| 32 | + |
| 33 | +.. _kernel.org: http://kernel.org/ |
| 34 | + |
| 35 | +Otherwise, you can generate them locally. This is quite fast (same order as |
| 36 | +compiling the code itself) and you do not need any special tools or environment. |
| 37 | +This has the added advantage that they will be tailored to your particular |
| 38 | +kernel configuration. To generate them, simply use the ``rustdoc`` target with |
| 39 | +the same invocation you use for compilation, e.g.:: |
| 40 | + |
| 41 | + make ARCH=... CROSS_COMPILE=... CC=... -j... rustdoc |
| 42 | + |
| 43 | + |
| 44 | +Writing the docs |
| 45 | +---------------- |
| 46 | + |
| 47 | +If you already know Markdown, learning how to write Rust documentation will be |
| 48 | +a breeze. If not, understanding the basics is a matter of minutes reading other |
| 49 | +code. There are also many guides available out there, a particularly nice one |
| 50 | +is at `GitHub`_. |
| 51 | + |
| 52 | +.. _GitHub: https://guides.github.com/features/mastering-markdown/#syntax |
| 53 | + |
| 54 | +This is how a well-documented Rust function may look like (derived from the Rust |
| 55 | +standard library):: |
| 56 | + |
| 57 | + /// Returns the contained [`Some`] value, consuming the `self` value, |
| 58 | + /// without checking that the value is not [`None`]. |
| 59 | + /// |
| 60 | + /// # Safety |
| 61 | + /// |
| 62 | + /// Calling this method on [`None`] is *[undefined behavior]*. |
| 63 | + /// |
| 64 | + /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html |
| 65 | + /// |
| 66 | + /// # Examples |
| 67 | + /// |
| 68 | + /// ``` |
| 69 | + /// let x = Some("air"); |
| 70 | + /// assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); |
| 71 | + /// ``` |
| 72 | + pub unsafe fn unwrap_unchecked(self) -> T { |
| 73 | + match self { |
| 74 | + Some(val) => val, |
| 75 | + |
| 76 | + // SAFETY: the safety contract must be upheld by the caller. |
| 77 | + None => unsafe { hint::unreachable_unchecked() }, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +This example showcases a few ``rustdoc`` features and some common conventions |
| 82 | +(that we also follow in the kernel): |
| 83 | + |
| 84 | +* The first paragraph must be a single sentence briefly describing what |
| 85 | + the documented item does. Further explanations must go in extra paragraphs. |
| 86 | + |
| 87 | +* ``unsafe`` functions must document the preconditions needed for a call to be |
| 88 | + safe under a ``Safety`` section. |
| 89 | + |
| 90 | +* While not shown here, if a function may panic, the conditions under which |
| 91 | + that happens must be described under a ``Panics`` section. |
| 92 | + |
| 93 | +* If providing examples of usage would help readers, they must be written in |
| 94 | + a section called ``Examples``. |
| 95 | + |
| 96 | +* Rust items (functions, types, constants...) will be automatically linked |
| 97 | + (``rustdoc`` will find out the URL for you). |
| 98 | + |
| 99 | +* Following the Rust standard library conventions, any ``unsafe`` block must be |
| 100 | + preceded by a ``SAFETY`` comment describing why the code inside is sound. |
| 101 | + |
| 102 | + While sometimes the reason might look trivial and therefore unneeded, writing |
| 103 | + these comments is not just a good way of documenting what has been taken into |
| 104 | + account, but also that there are no *extra* implicit constraints. |
| 105 | + |
| 106 | +To learn more about how to write documentation for Rust and extra features, |
| 107 | +please take a look at the ``rustdoc`` `book`_. |
| 108 | + |
| 109 | +.. _book: https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html |
0 commit comments