€•ÌéŒsphinx.addnodes”Œdocument”“”)�”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)�”}”(hhh]”(hŒ pending_xref”“”)�”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…”�”}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ*/translations/zh_CN/rust/coding-guidelines”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)�”}”(hhh]”hŒChinese (Traditional)”…”�”}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ*/translations/zh_TW/rust/coding-guidelines”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ*/translations/it_IT/rust/coding-guidelines”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ*/translations/ja_JP/rust/coding-guidelines”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ*/translations/ko_KR/rust/coding-guidelines”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒPortuguese (Brazilian)”…”�”}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ*/translations/pt_BR/rust/coding-guidelines”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒSpanish”…”�”}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ*/translations/sp_SP/rust/coding-guidelines”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒcomment”“”)�”}”(hŒ SPDX-License-Identifier: GPL-2.0”h]”hŒ SPDX-License-Identifier: GPL-2.0”…”�”}”hh·sbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1hµhhh²hh³ŒD/var/lib/git/docbuild/linux/Documentation/rust/coding-guidelines.rst”h´KubhŒsection”“”)�”}”(hhh]”(hŒtitle”“”)�”}”(hŒCoding Guidelines”h]”hŒCoding Guidelines”…”�”}”(hhÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhÊh²hh³hÇh´KubhŒ paragraph”“”)�”}”(hŒ=This document describes how to write Rust code in the kernel.”h]”hŒ=This document describes how to write Rust code in the kernel.”…”�”}”(hhßh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KhhÊh²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒStyle & formatting”h]”hŒStyle & formatting”…”�”}”(hhðh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhíh²hh³hÇh´K ubhÞ)�”}”(hXTThe code should be formatted using ``rustfmt``. In this way, a person contributing from time to time to the kernel does not need to learn and remember one more style guide. More importantly, reviewers and maintainers do not need to spend time pointing out style issues anymore, and thus less patch roundtrips may be needed to land a change.”h]”(hŒ#The code should be formatted using ”…”�”}”(hhþh²hh³Nh´NubhŒliteral”“”)�”}”(hŒ ``rustfmt``”h]”hŒrustfmt”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhþubhX&. In this way, a person contributing from time to time to the kernel does not need to learn and remember one more style guide. More importantly, reviewers and maintainers do not need to spend time pointing out style issues anymore, and thus less patch roundtrips may be needed to land a change.”…”�”}”(hhþh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K hhíh²hubhŒnote”“”)�”}”(hŒzConventions on comments and documentation are not checked by ``rustfmt``. Thus those are still needed to be taken care of.”h]”hÞ)�”}”(hŒzConventions on comments and documentation are not checked by ``rustfmt``. Thus those are still needed to be taken care of.”h]”(hŒ=Conventions on comments and documentation are not checked by ”…”�”}”(hj&h²hh³Nh´Nubj)�”}”(hŒ ``rustfmt``”h]”hŒrustfmt”…”�”}”(hj.h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj&ubhŒ2. Thus those are still needed to be taken care of.”…”�”}”(hj&h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Khj"ubah}”(h]”h ]”h"]”h$]”h&]”uh1j hhíh²hh³hÇh´NubhÞ)�”}”(hŒ The default settings of ``rustfmt`` are used. This means the idiomatic Rust style is followed. For instance, 4 spaces are used for indentation rather than tabs.”h]”(hŒThe default settings of ”…”�”}”(hjLh²hh³Nh´Nubj)�”}”(hŒ ``rustfmt``”h]”hŒrustfmt”…”�”}”(hjTh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjLubhŒ} are used. This means the idiomatic Rust style is followed. For instance, 4 spaces are used for indentation rather than tabs.”…”�”}”(hjLh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Khhíh²hubhÞ)�”}”(hŒÜIt is convenient to instruct editors/IDEs to format while typing, when saving or at commit time. However, if for some reason reformatting the entire kernel Rust sources is needed at some point, the following can be run::”h]”hŒÛIt is convenient to instruct editors/IDEs to format while typing, when saving or at commit time. However, if for some reason reformatting the entire kernel Rust sources is needed at some point, the following can be run:”…”�”}”(hjlh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Khhíh²hubhŒ literal_block”“”)�”}”(hŒmake LLVM=1 rustfmt”h]”hŒmake LLVM=1 rustfmt”…”�”}”hj|sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jzh³hÇh´Khhíh²hubhÞ)�”}”(hŒrIt is also possible to check if everything is formatted (printing a diff otherwise), for instance for a CI, with::”h]”hŒqIt is also possible to check if everything is formatted (printing a diff otherwise), for instance for a CI, with:”…”�”}”(hjŠh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K hhíh²hubj{)�”}”(hŒmake LLVM=1 rustfmtcheck”h]”hŒmake LLVM=1 rustfmtcheck”…”�”}”hj˜sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jzh³hÇh´K#hhíh²hubhÞ)�”}”(hŒ²Like ``clang-format`` for the rest of the kernel, ``rustfmt`` works on individual files, and does not require a kernel configuration. Sometimes it may even work with broken code.”h]”(hŒLike ”…”�”}”(hj¦h²hh³Nh´Nubj)�”}”(hŒ``clang-format``”h]”hŒ clang-format”…”�”}”(hj®h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¦ubhŒ for the rest of the kernel, ”…”�”}”(hj¦h²hh³Nh´Nubj)�”}”(hŒ ``rustfmt``”h]”hŒrustfmt”…”�”}”(hjÀh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¦ubhŒu works on individual files, and does not require a kernel configuration. Sometimes it may even work with broken code.”…”�”}”(hj¦h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K%hhíh²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒImports”h]”hŒImports”…”�”}”(hjÛh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjØh²hh³hÇh´K*ubhÞ)�”}”(hŒ¹``rustfmt``, by default, formats imports in a way that is prone to conflicts while merging and rebasing, since in some cases it condenses several items into the same line. For instance:”h]”(j)�”}”(hŒ ``rustfmt``”h]”hŒrustfmt”…”�”}”(hjíh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjéubhŒ®, by default, formats imports in a way that is prone to conflicts while merging and rebasing, since in some cases it condenses several items into the same line. For instance:”…”�”}”(hjéh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K,hjØh²hubj{)�”}”(hŒ•// Do not use this style. use crate::{ example1, example2::{example3, example4, example5}, example6, example7, example8::example9, };”h]”hŒ•// Do not use this style. use crate::{ example1, example2::{example3, example4, example5}, example6, example7, example8::example9, };”…”�”}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆŒforce”‰Œlanguage”Œrust”Œhighlight_args”}”uh1jzh³hÇh´K0hjØh²hubhÞ)�”}”(hŒ@Instead, the kernel uses a vertical layout that looks like this:”h]”hŒ@Instead, the kernel uses a vertical layout that looks like this:”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K:hjØh²hubj{)�”}”(hŒ¤use crate::{ example1, example2::{ example3, example4, example5, // }, example6, example7, example8::example9, // };”h]”hŒ¤use crate::{ example1, example2::{ example3, example4, example5, // }, example6, example7, example8::example9, // };”…”�”}”hj&sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´K Foo { // ... }”h]”hŒ¾/// Returns a new [`Foo`]. /// /// # Examples /// // TODO: Find a better example. /// ``` /// let foo = f(42); /// ``` // FIXME: Use fallible approach. pub fn f(x: i32) -> Foo { // ... }”…”�”}”hj·sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´K”hjðh²hubhÞ)�”}”(hX\This applies to both public and private items. This increases consistency with public items, allows changes to visibility with less changes involved and will allow us to potentially generate the documentation for private items as well. In other words, if documentation is written for a private item, then ``///`` should still be used. For instance:”h]”(hX1This applies to both public and private items. This increases consistency with public items, allows changes to visibility with less changes involved and will allow us to potentially generate the documentation for private items as well. In other words, if documentation is written for a private item, then ”…”�”}”(hjÇh²hh³Nh´Nubj)�”}”(hŒ``///``”h]”hŒ///”…”�”}”(hjÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÇubhŒ$ should still be used. For instance:”…”�”}”(hjÇh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K£hjðh²hubj{)�”}”(hŒ//// My private function. // TODO: ... fn f() {}”h]”hŒ//// My private function. // TODO: ... fn f() {}”…”�”}”hjçsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´K©hjðh²hubhÞ)�”}”(hŒðOne special kind of comments are the ``// SAFETY:`` comments. These must appear before every ``unsafe`` block, and they explain why the code inside the block is correct/sound, i.e. why it cannot trigger undefined behavior in any case, e.g.:”h]”(hŒ%One special kind of comments are the ”…”�”}”(hj÷h²hh³Nh´Nubj)�”}”(hŒ``// SAFETY:``”h]”hŒ // SAFETY:”…”�”}”(hjÿh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj÷ubhŒ* comments. These must appear before every ”…”�”}”(hj÷h²hh³Nh´Nubj)�”}”(hŒ ``unsafe``”h]”hŒunsafe”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj÷ubhŒ‰ block, and they explain why the code inside the block is correct/sound, i.e. why it cannot trigger undefined behavior in any case, e.g.:”…”�”}”(hj÷h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K¯hjðh²hubj{)�”}”(hŒF// SAFETY: `p` is valid by the safety requirements. unsafe { *p = 0; }”h]”hŒF// SAFETY: `p` is valid by the safety requirements. unsafe { *p = 0; }”…”�”}”hj)sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´K³hjðh²hubhÞ)�”}”(hX’``// SAFETY:`` comments are not to be confused with the ``# Safety`` sections in code documentation. ``# Safety`` sections specify the contract that callers (for functions) or implementors (for traits) need to abide by. ``// SAFETY:`` comments show why a call (for functions) or implementation (for traits) actually respects the preconditions stated in a ``# Safety`` section or the language reference.”h]”(j)�”}”(hŒ``// SAFETY:``”h]”hŒ // SAFETY:”…”�”}”(hj=h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj9ubhŒ* comments are not to be confused with the ”…”�”}”(hj9h²hh³Nh´Nubj)�”}”(hŒ ``# Safety``”h]”hŒ# Safety”…”�”}”(hjOh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj9ubhŒ! sections in code documentation. ”…”�”}”(hj9h²hh³Nh´Nubj)�”}”(hŒ ``# Safety``”h]”hŒ# Safety”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj9ubhŒk sections specify the contract that callers (for functions) or implementors (for traits) need to abide by. ”…”�”}”(hj9h²hh³Nh´Nubj)�”}”(hŒ``// SAFETY:``”h]”hŒ // SAFETY:”…”�”}”(hjsh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj9ubhŒy comments show why a call (for functions) or implementation (for traits) actually respects the preconditions stated in a ”…”�”}”(hj9h²hh³Nh´Nubj)�”}”(hŒ ``# Safety``”h]”hŒ# Safety”…”�”}”(hj…h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj9ubhŒ# section or the language reference.”…”�”}”(hj9h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K¸hjðh²hubeh}”(h]”Œcomments”ah ]”h"]”Œcomments”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KvubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒCode documentation”h]”hŒCode documentation”…”�”}”(hj¨h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj¥h²hh³hÇh´KÁubhÞ)�”}”(hŒÔRust kernel code is not documented like C kernel code (i.e. via kernel-doc). Instead, the usual system for documenting Rust code is used: the ``rustdoc`` tool, which uses Markdown (a lightweight markup language).”h]”(hŒŽRust kernel code is not documented like C kernel code (i.e. via kernel-doc). Instead, the usual system for documenting Rust code is used: the ”…”�”}”(hj¶h²hh³Nh´Nubj)�”}”(hŒ ``rustdoc``”h]”hŒrustdoc”…”�”}”(hj¾h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¶ubhŒ; tool, which uses Markdown (a lightweight markup language).”…”�”}”(hj¶h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÃhj¥h²hubhÞ)�”}”(hŒWTo learn Markdown, there are many guides available out there. For instance, the one at:”h]”hŒWTo learn Markdown, there are many guides available out there. For instance, the one at:”…”�”}”(hjÖh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÇhj¥h²hubhŒ block_quote”“”)�”}”(hŒhttps://commonmark.org/help/ ”h]”hÞ)�”}”(hŒhttps://commonmark.org/help/”h]”hŒ reference”“”)�”}”(hjìh]”hŒhttps://commonmark.org/help/”…”�”}”(hjðh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”jìuh1jîhjêubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÊhjæubah}”(h]”h ]”h"]”h$]”h&]”uh1jäh³hÇh´KÊhj¥h²hubhÞ)�”}”(hŒ:This is how a well-documented Rust function may look like:”h]”hŒ:This is how a well-documented Rust function may look like:”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÌhj¥h²hubj{)�”}”(hXŠ/// Returns the contained [`Some`] value, consuming the `self` value, /// without checking that the value is not [`None`]. /// /// # Safety /// /// Calling this method on [`None`] is *[undefined behavior]*. /// /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html /// /// # Examples /// /// ``` /// let x = Some("air"); /// assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); /// ``` pub unsafe fn unwrap_unchecked(self) -> T { match self { Some(val) => val, // SAFETY: The safety contract must be upheld by the caller. None => unsafe { hint::unreachable_unchecked() }, } }”h]”hXŠ/// Returns the contained [`Some`] value, consuming the `self` value, /// without checking that the value is not [`None`]. /// /// # Safety /// /// Calling this method on [`None`] is *[undefined behavior]*. /// /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html /// /// # Examples /// /// ``` /// let x = Some("air"); /// assert_eq!(unsafe { x.unwrap_unchecked() }, "air"); /// ``` pub unsafe fn unwrap_unchecked(self) -> T { match self { Some(val) => val, // SAFETY: The safety contract must be upheld by the caller. None => unsafe { hint::unreachable_unchecked() }, } }”…”�”}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´KÎhj¥h²hubhÞ)�”}”(hŒ^This example showcases a few ``rustdoc`` features and some conventions followed in the kernel:”h]”(hŒThis example showcases a few ”…”�”}”(hj(h²hh³Nh´Nubj)�”}”(hŒ ``rustdoc``”h]”hŒrustdoc”…”�”}”(hj0h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj(ubhŒ6 features and some conventions followed in the kernel:”…”�”}”(hj(h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kèhj¥h²hubhŒ bullet_list”“”)�”}”(hhh]”(hŒ list_item”“”)�”}”(hŒ’The first paragraph must be a single sentence briefly describing what the documented item does. Further explanations must go in extra paragraphs. ”h]”hÞ)�”}”(hŒ‘The first paragraph must be a single sentence briefly describing what the documented item does. Further explanations must go in extra paragraphs.”h]”hŒ‘The first paragraph must be a single sentence briefly describing what the documented item does. Further explanations must go in extra paragraphs.”…”�”}”(hjSh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KëhjOubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhjJh²hh³hÇh´NubjN)�”}”(hŒXUnsafe functions must document their safety preconditions under a ``# Safety`` section. ”h]”hÞ)�”}”(hŒWUnsafe functions must document their safety preconditions under a ``# Safety`` section.”h]”(hŒBUnsafe functions must document their safety preconditions under a ”…”�”}”(hjkh²hh³Nh´Nubj)�”}”(hŒ ``# Safety``”h]”hŒ# Safety”…”�”}”(hjsh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjkubhŒ section.”…”�”}”(hjkh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kîhjgubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhjJh²hh³hÇh´NubjN)�”}”(hX4While not shown here, if a function may panic, the conditions under which that happens must be described under a ``# Panics`` section. Please note that panicking should be very rare and used only with a good reason. In almost all cases, a fallible approach should be used, typically returning a ``Result``. ”h]”(hÞ)�”}”(hŒ†While not shown here, if a function may panic, the conditions under which that happens must be described under a ``# Panics`` section.”h]”(hŒqWhile not shown here, if a function may panic, the conditions under which that happens must be described under a ”…”�”}”(hj•h²hh³Nh´Nubj)�”}”(hŒ ``# Panics``”h]”hŒ# Panics”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj•ubhŒ section.”…”�”}”(hj•h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kñhj‘ubhÞ)�”}”(hŒ«Please note that panicking should be very rare and used only with a good reason. In almost all cases, a fallible approach should be used, typically returning a ``Result``.”h]”(hŒ Please note that panicking should be very rare and used only with a good reason. In almost all cases, a fallible approach should be used, typically returning a ”…”�”}”(hjµh²hh³Nh´Nubj)�”}”(hŒ ``Result``”h]”hŒResult”…”�”}”(hj½h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjµubhŒ.”…”�”}”(hjµh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kôhj‘ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jMhjJh²hh³hÇh´NubjN)�”}”(hŒlIf providing examples of usage would help readers, they must be written in a section called ``# Examples``. ”h]”hÞ)�”}”(hŒkIf providing examples of usage would help readers, they must be written in a section called ``# Examples``.”h]”(hŒ\If providing examples of usage would help readers, they must be written in a section called ”…”�”}”(hjßh²hh³Nh´Nubj)�”}”(hŒ``# Examples``”h]”hŒ # Examples”…”�”}”(hjçh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjßubhŒ.”…”�”}”(hjßh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KøhjÛubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhjJh²hh³hÇh´NubjN)�”}”(hŒyRust items (functions, types, constants...) must be linked appropriately (``rustdoc`` will create a link automatically). ”h]”hÞ)�”}”(hŒxRust items (functions, types, constants...) must be linked appropriately (``rustdoc`` will create a link automatically).”h]”(hŒJRust items (functions, types, constants...) must be linked appropriately (”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ ``rustdoc``”h]”hŒrustdoc”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ# will create a link automatically).”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kûhjubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhjJh²hh³hÇh´NubjN)�”}”(hXqAny ``unsafe`` block must be preceded by a ``// SAFETY:`` comment describing why the code inside is sound. While sometimes the reason might look trivial and therefore unneeded, writing these comments is not just a good way of documenting what has been taken into account, but most importantly, it provides a way to know that there are no *extra* implicit constraints. ”h]”(hÞ)�”}”(hŒjAny ``unsafe`` block must be preceded by a ``// SAFETY:`` comment describing why the code inside is sound.”h]”(hŒAny ”…”�”}”(hj3h²hh³Nh´Nubj)�”}”(hŒ ``unsafe``”h]”hŒunsafe”…”�”}”(hj;h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj3ubhŒ block must be preceded by a ”…”�”}”(hj3h²hh³Nh´Nubj)�”}”(hŒ``// SAFETY:``”h]”hŒ // SAFETY:”…”�”}”(hjMh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj3ubhŒ1 comment describing why the code inside is sound.”…”�”}”(hj3h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kþhj/ubhÞ)�”}”(hXWhile sometimes the reason might look trivial and therefore unneeded, writing these comments is not just a good way of documenting what has been taken into account, but most importantly, it provides a way to know that there are no *extra* implicit constraints.”h]”(hŒçWhile sometimes the reason might look trivial and therefore unneeded, writing these comments is not just a good way of documenting what has been taken into account, but most importantly, it provides a way to know that there are no ”…”�”}”(hjeh²hh³Nh´NubhŒemphasis”“”)�”}”(hŒ*extra*”h]”hŒextra”…”�”}”(hjoh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jmhjeubhŒ implicit constraints.”…”�”}”(hjeh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mhj/ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jMhjJh²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œbullet”Œ-”uh1jHh³hÇh´Këhj¥h²hubhÞ)�”}”(hŒzTo learn more about how to write documentation for Rust and extra features, please take a look at the ``rustdoc`` book at:”h]”(hŒfTo learn more about how to write documentation for Rust and extra features, please take a look at the ”…”�”}”(hj•h²hh³Nh´Nubj)�”}”(hŒ ``rustdoc``”h]”hŒrustdoc”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj•ubhŒ book at:”…”�”}”(hj•h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mhj¥h²hubjå)�”}”(hŒBhttps://doc.rust-lang.org/rustdoc/how-to-write-documentation.html ”h]”hÞ)�”}”(hŒAhttps://doc.rust-lang.org/rustdoc/how-to-write-documentation.html”h]”jï)�”}”(hj»h]”hŒAhttps://doc.rust-lang.org/rustdoc/how-to-write-documentation.html”…”�”}”(hj½h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”j»uh1jîhj¹ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M hjµubah}”(h]”h ]”h"]”h$]”h&]”uh1jäh³hÇh´M hj¥h²hubhÞ)�”}”(hŒŽIn addition, the kernel supports creating links relative to the source tree by prefixing the link destination with ``srctree/``. For instance:”h]”(hŒsIn addition, the kernel supports creating links relative to the source tree by prefixing the link destination with ”…”�”}”(hj×h²hh³Nh´Nubj)�”}”(hŒ ``srctree/``”h]”hŒsrctree/”…”�”}”(hjßh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj×ubhŒ. For instance:”…”�”}”(hj×h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M hj¥h²hubj{)�”}”(hŒH//! C header: [`include/linux/printk.h`](srctree/include/linux/printk.h)”h]”hŒH//! C header: [`include/linux/printk.h`](srctree/include/linux/printk.h)”…”�”}”hj÷sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´Mhj¥h²hubhÞ)�”}”(hŒor:”h]”hŒor:”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mhj¥h²hubj{)�”}”(hŒ3/// [`struct mutex`]: srctree/include/linux/mutex.h”h]”hŒ3/// [`struct mutex`]: srctree/include/linux/mutex.h”…”�”}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´Mhj¥h²hubeh}”(h]”Œcode-documentation”ah ]”h"]”Œcode documentation”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KÁubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ C FFI types”h]”hŒ C FFI types”…”�”}”(hj0h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj-h²hh³hÇh´MubhÞ)�”}”(hŒî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.”h]”(hŒ,Rust kernel code refers to C types, such as ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ``int``”h]”hŒint”…”�”}”(hjFh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒ, using type aliases such as ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ ``c_int``”h]”hŒc_int”…”�”}”(hjXh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒ', which are readily available from the ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ ``kernel``”h]”hŒkernel”…”�”}”(hjjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒ- prelude. Please do not use the aliases from ”…”�”}”(hj>h²hh³Nh´Nubj)�”}”(hŒ ``core::ffi``”h]”hŒ core::ffi”…”�”}”(hj|h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj>ubhŒ* -- they may not map to the correct types.”…”�”}”(hj>h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mhj-h²hubhÞ)�”}”(hŒuThese aliases should generally be referred directly by their identifier, i.e. as a single segment path. For instance:”h]”hŒuThese aliases should generally be referred directly by their identifier, i.e. as a single segment path. For instance:”…”�”}”(hj”h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M hj-h²hubj{)�”}”(hŒ.fn f(p: *const c_char) -> c_int { // ... }”h]”hŒ.fn f(p: *const c_char) -> c_int { // ... }”…”�”}”hj¢sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´M#hj-h²hubeh}”(h]”Œ c-ffi-types”ah ]”h"]”Œ c ffi types”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´MubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒNaming”h]”hŒNaming”…”�”}”(hj½h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjºh²hh³hÇh´M+ubhÞ)�”}”(hŒ;Rust kernel code follows the usual Rust naming conventions:”h]”hŒ;Rust kernel code follows the usual Rust naming conventions:”…”�”}”(hjËh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M-hjºh²hubjå)�”}”(hŒ7https://rust-lang.github.io/api-guidelines/naming.html ”h]”hÞ)�”}”(hŒ6https://rust-lang.github.io/api-guidelines/naming.html”h]”jï)�”}”(hjßh]”hŒ6https://rust-lang.github.io/api-guidelines/naming.html”…”�”}”(hjáh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”jßuh1jîhjÝubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M/hjÙubah}”(h]”h ]”h"]”h$]”h&]”uh1jäh³hÇh´M/hjºh²hubhÞ)�”}”(hXnWhen existing C concepts (e.g. macros, functions, objects...) are wrapped into a Rust abstraction, a name as close as reasonably possible to the C side should be used in order to avoid confusion and to improve readability when switching back and forth between the C and Rust sides. For instance, macros such as ``pr_info`` from C are named the same in the Rust side.”h]”(hX7When existing C concepts (e.g. macros, functions, objects...) are wrapped into a Rust abstraction, a name as close as reasonably possible to the C side should be used in order to avoid confusion and to improve readability when switching back and forth between the C and Rust sides. For instance, macros such as ”…”�”}”(hjûh²hh³Nh´Nubj)�”}”(hŒ ``pr_info``”h]”hŒpr_info”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjûubhŒ, from C are named the same in the Rust side.”…”�”}”(hjûh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M1hjºh²hubhÞ)�”}”(hŒØHaving said that, casing should be adjusted to follow the Rust naming conventions, and namespacing introduced by modules and types should not be repeated in the item names. For instance, when wrapping constants like:”h]”hŒØHaving said that, casing should be adjusted to follow the Rust naming conventions, and namespacing introduced by modules and types should not be repeated in the item names. For instance, when wrapping constants like:”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M7hjºh²hubj{)�”}”(hŒC#define GPIO_LINE_DIRECTION_IN 0 #define GPIO_LINE_DIRECTION_OUT 1”h]”hŒC#define GPIO_LINE_DIRECTION_IN 0 #define GPIO_LINE_DIRECTION_OUT 1”…”�”}”hj)sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒc”j}”uh1jzh³hÇh´M;hjºh²hubhÞ)�”}”(hŒ>The equivalent in Rust may look like (ignoring documentation):”h]”hŒ>The equivalent in Rust may look like (ignoring documentation):”…”�”}”(hj9h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M@hjºh²hubj{)�”}”(hŒ�pub mod gpio { pub enum LineDirection { In = bindings::GPIO_LINE_DIRECTION_IN as _, Out = bindings::GPIO_LINE_DIRECTION_OUT as _, } }”h]”hŒ�pub mod gpio { pub enum LineDirection { In = bindings::GPIO_LINE_DIRECTION_IN as _, Out = bindings::GPIO_LINE_DIRECTION_OUT as _, } }”…”�”}”hjGsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´MBhjºh²hubhÞ)�”}”(hŒÇThat is, the equivalent of ``GPIO_LINE_DIRECTION_IN`` would be referred to as ``gpio::LineDirection::In``. In particular, it should not be named ``gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN``.”h]”(hŒThat is, the equivalent of ”…”�”}”(hjWh²hh³Nh´Nubj)�”}”(hŒ``GPIO_LINE_DIRECTION_IN``”h]”hŒGPIO_LINE_DIRECTION_IN”…”�”}”(hj_h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjWubhŒ would be referred to as ”…”�”}”(hjWh²hh³Nh´Nubj)�”}”(hŒ``gpio::LineDirection::In``”h]”hŒgpio::LineDirection::In”…”�”}”(hjqh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjWubhŒ(. In particular, it should not be named ”…”�”}”(hjWh²hh³Nh´Nubj)�”}”(hŒ5``gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN``”h]”hŒ1gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN”…”�”}”(hjƒh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjWubhŒ.”…”�”}”(hjWh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MKhjºh²hubeh}”(h]”Œnaming”ah ]”h"]”Œnaming”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´M+ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒLints”h]”hŒLints”…”�”}”(hj¦h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj£h²hh³hÇh´MQubhÞ)�”}”(hŒ»In Rust, it is possible to ``allow`` particular warnings (diagnostics, lints) locally, making the compiler ignore instances of a given warning within a given function, module, block, etc.”h]”(hŒIn Rust, it is possible to ”…”�”}”(hj´h²hh³Nh´Nubj)�”}”(hŒ ``allow``”h]”hŒallow”…”�”}”(hj¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj´ubhŒ— particular warnings (diagnostics, lints) locally, making the compiler ignore instances of a given warning within a given function, module, block, etc.”…”�”}”(hj´h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MShj£h²hubhÞ)�”}”(hŒSIt is similar to ``#pragma GCC diagnostic push`` + ``ignored`` + ``pop`` in C [#]_:”h]”(hŒIt is similar to ”…”�”}”(hjÔh²hh³Nh´Nubj)�”}”(hŒ``#pragma GCC diagnostic push``”h]”hŒ#pragma GCC diagnostic push”…”�”}”(hjÜh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÔubhŒ + ”…”�”}”(hjÔh²hh³Nh´Nubj)�”}”(hŒ ``ignored``”h]”hŒignored”…”�”}”(hjîh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÔubhŒ + ”…”�”}”hjÔsbj)�”}”(hŒ``pop``”h]”hŒpop”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÔubhŒ in C ”…”�”}”(hjÔh²hh³Nh´NubhŒfootnote_reference”“”)�”}”(hŒ[#]_”h]”hŒ1”…”�”}”(hj h²hh³Nh´Nubah}”(h]”Œid1”ah ]”h"]”h$]”h&]”Œauto”KŒrefid”Œid2”Œdocname”Œrust/coding-guidelines”uh1j hjÔŒresolved”KubhŒ:”…”�”}”(hjÔh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MWhj£h²hubj{)�”}”(hŒ€#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" static void f(void) {} #pragma GCC diagnostic pop”h]”hŒ€#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" static void f(void) {} #pragma GCC diagnostic pop”…”�”}”hj3 sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jj7j}”uh1jzh³hÇh´MZhj£h²hubhŒfootnote”“”)�”}”(hŒÒIn this particular case, the kernel's ``__{always,maybe}_unused`` attributes (C23's ``[[maybe_unused]]``) may be used; however, the example is meant to reflect the equivalent lint in Rust discussed afterwards. ”h]”(hŒlabel”“”)�”}”(hhh]”hŒ1”…”�”}”(hjJ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jH hjD h²hh³Nh´NubhÞ)�”}”(hŒÑIn this particular case, the kernel's ``__{always,maybe}_unused`` attributes (C23's ``[[maybe_unused]]``) may be used; however, the example is meant to reflect the equivalent lint in Rust discussed afterwards.”h]”(hŒ(In this particular case, the kernel’s ”…”�”}”(hjW h²hh³Nh´Nubj)�”}”(hŒ``__{always,maybe}_unused``”h]”hŒ__{always,maybe}_unused”…”�”}”(hj_ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjW ubhŒ attributes (C23’s ”…”�”}”(hjW h²hh³Nh´Nubj)�”}”(hŒ``[[maybe_unused]]``”h]”hŒ[[maybe_unused]]”…”�”}”(hjq h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjW ubhŒi) may be used; however, the example is meant to reflect the equivalent lint in Rust discussed afterwards.”…”�”}”(hjW h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MahjD ubeh}”(h]”j% ah ]”h"]”Œ1”ah$]”h&]”j aj# Kj& j' uh1jB h³hÇh´Mahj£h²hubhÞ)�”}”(hŒBut way less verbose:”h]”hŒBut way less verbose:”…”�”}”(hj� h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mehj£h²hubj{)�”}”(hŒ#[allow(dead_code)] fn f() {}”h]”hŒ#[allow(dead_code)] fn f() {}”…”�”}”hjž sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´Mghj£h²hubhÞ)�”}”(hŒýBy that virtue, it makes it possible to comfortably enable more diagnostics by default (i.e. outside ``W=`` levels). In particular, those that may have some false positives but that are otherwise quite useful to keep enabled to catch potential mistakes.”h]”(hŒeBy that virtue, it makes it possible to comfortably enable more diagnostics by default (i.e. outside ”…”�”}”(hj® h²hh³Nh´Nubj)�”}”(hŒ``W=``”h]”hŒW=”…”�”}”(hj¶ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj® ubhŒ’ levels). In particular, those that may have some false positives but that are otherwise quite useful to keep enabled to catch potential mistakes.”…”�”}”(hj® h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mlhj£h²hubhÞ)�”}”(hXOn top of that, Rust provides the ``expect`` attribute which takes this further. It makes the compiler warn if the warning was not produced. For instance, the following will ensure that, when ``f()`` is called somewhere, we will have to remove the attribute:”h]”(hŒ"On top of that, Rust provides the ”…”�”}”(hjÎ h²hh³Nh´Nubj)�”}”(hŒ ``expect``”h]”hŒexpect”…”�”}”(hjÖ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÎ ubhŒ” attribute which takes this further. It makes the compiler warn if the warning was not produced. For instance, the following will ensure that, when ”…”�”}”(hjÎ h²hh³Nh´Nubj)�”}”(hŒ``f()``”h]”hŒf()”…”�”}”(hjè h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÎ ubhŒ; is called somewhere, we will have to remove the attribute:”…”�”}”(hjÎ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mqhj£h²hubj{)�”}”(hŒ#[expect(dead_code)] fn f() {}”h]”hŒ#[expect(dead_code)] fn f() {}”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´Mvhj£h²hubhÞ)�”}”(hŒ2If we do not, we get a warning from the compiler::”h]”hŒ1If we do not, we get a warning from the compiler:”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M{hj£h²hubj{)�”}”(hŒµwarning: this lint expectation is unfulfilled --> x.rs:3:10 | 3 | #[expect(dead_code)] | ^^^^^^^^^ | = note: `#[warn(unfulfilled_lint_expectations)]` on by default”h]”hŒµwarning: this lint expectation is unfulfilled --> x.rs:3:10 | 3 | #[expect(dead_code)] | ^^^^^^^^^ | = note: `#[warn(unfulfilled_lint_expectations)]` on by default”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jzh³hÇh´M}hj£h²hubhÞ)�”}”(hŒzThis means that ``expect``\ s do not get forgotten when they are not needed, which may happen in several situations, e.g.:”h]”(hŒThis means that ”…”�”}”(hj, h²hh³Nh´Nubj)�”}”(hŒ ``expect``”h]”hŒexpect”…”�”}”(hj4 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj, ubhŒ` s do not get forgotten when they are not needed, which may happen in several situations, e.g.:”…”�”}”(hj, h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M…hj£h²hubjI)�”}”(hhh]”(jN)�”}”(hŒ-Temporary attributes added while developing. ”h]”hÞ)�”}”(hŒ,Temporary attributes added while developing.”h]”hŒ,Temporary attributes added while developing.”…”�”}”(hjS h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MˆhjO ubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhjL h²hh³hÇh´NubjN)�”}”(hŒaImprovements in lints in the compiler, Clippy or custom tools which may remove a false positive. ”h]”hÞ)�”}”(hŒ`Improvements in lints in the compiler, Clippy or custom tools which may remove a false positive.”h]”hŒ`Improvements in lints in the compiler, Clippy or custom tools which may remove a false positive.”…”�”}”(hjk h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MŠhjg ubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhjL h²hh³hÇh´NubjN)�”}”(hŒ�When the lint is not needed anymore because it was expected that it would be removed at some point, such as the ``dead_code`` example above. ”h]”hÞ)�”}”(hŒŒWhen the lint is not needed anymore because it was expected that it would be removed at some point, such as the ``dead_code`` example above.”h]”(hŒpWhen the lint is not needed anymore because it was expected that it would be removed at some point, such as the ”…”�”}”(hjƒ h²hh³Nh´Nubj)�”}”(hŒ ``dead_code``”h]”hŒ dead_code”…”�”}”(hj‹ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjƒ ubhŒ example above.”…”�”}”(hjƒ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M�hj ubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhjL h²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”j“j”uh1jHh³hÇh´Mˆhj£h²hubhÞ)�”}”(hŒiIt also increases the visibility of the remaining ``allow``\ s and reduces the chance of misapplying one.”h]”(hŒ2It also increases the visibility of the remaining ”…”�”}”(hj¯ h²hh³Nh´Nubj)�”}”(hŒ ``allow``”h]”hŒallow”…”�”}”(hj· h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¯ ubhŒ. s and reduces the chance of misapplying one.”…”�”}”(hj¯ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M�hj£h²hubhÞ)�”}”(hŒ-Thus prefer ``expect`` over ``allow`` unless:”h]”(hŒ Thus prefer ”…”�”}”(hjÏ h²hh³Nh´Nubj)�”}”(hŒ ``expect``”h]”hŒexpect”…”�”}”(hj× h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÏ ubhŒ over ”…”�”}”(hjÏ h²hh³Nh´Nubj)�”}”(hŒ ``allow``”h]”hŒallow”…”�”}”(hjé h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÏ ubhŒ unless:”…”�”}”(hjÏ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M“hj£h²hubjI)�”}”(hhh]”(jN)�”}”(hXUConditional compilation triggers the warning in some cases but not others. If there are only a few cases where the warning triggers (or does not trigger) compared to the total number of cases, then one may consider using a conditional ``expect`` (i.e. ``cfg_attr(..., expect(...))``). Otherwise, it is likely simpler to just use ``allow``. ”h]”(hÞ)�”}”(hŒJConditional compilation triggers the warning in some cases but not others.”h]”hŒJConditional compilation triggers the warning in some cases but not others.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M•hj ubhÞ)�”}”(hXIf there are only a few cases where the warning triggers (or does not trigger) compared to the total number of cases, then one may consider using a conditional ``expect`` (i.e. ``cfg_attr(..., expect(...))``). Otherwise, it is likely simpler to just use ``allow``.”h]”(hŒ If there are only a few cases where the warning triggers (or does not trigger) compared to the total number of cases, then one may consider using a conditional ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ ``expect``”h]”hŒexpect”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ (i.e. ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ``cfg_attr(..., expect(...))``”h]”hŒcfg_attr(..., expect(...))”…”�”}”(hj0 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ/). Otherwise, it is likely simpler to just use ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ ``allow``”h]”hŒallow”…”�”}”(hjB h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M—hj ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jMhj h²hh³hÇh´NubjN)�”}”(hŒ‚Inside macros, when the different invocations may create expanded code that triggers the warning in some cases but not in others. ”h]”hÞ)�”}”(hŒ�Inside macros, when the different invocations may create expanded code that triggers the warning in some cases but not in others.”h]”hŒ�Inside macros, when the different invocations may create expanded code that triggers the warning in some cases but not in others.”…”�”}”(hjd h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mœhj` ubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhj h²hh³hÇh´NubjN)�”}”(hŒoWhen code may trigger a warning for some architectures but not others, such as an ``as`` cast to a C FFI type. ”h]”hÞ)�”}”(hŒnWhen code may trigger a warning for some architectures but not others, such as an ``as`` cast to a C FFI type.”h]”(hŒRWhen code may trigger a warning for some architectures but not others, such as an ”…”�”}”(hj| h²hh³Nh´Nubj)�”}”(hŒ``as``”h]”hŒas”…”�”}”(hj„ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj| ubhŒ cast to a C FFI type.”…”�”}”(hj| h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MŸhjx ubah}”(h]”h ]”h"]”h$]”h&]”uh1jMhj h²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”j“j”uh1jHh³hÇh´M•hj£h²hubhÞ)�”}”(hŒ@As a more developed example, consider for instance this program:”h]”hŒ@As a more developed example, consider for instance this program:”…”�”}”(hj¨ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M¢hj£h²hubj{)�”}”(hŒ6fn g() {} fn main() { #[cfg(CONFIG_X)] g(); }”h]”hŒ6fn g() {} fn main() { #[cfg(CONFIG_X)] g(); }”…”�”}”hj¶ sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´M¤hj£h²hubhÞ)�”}”(hŒ[Here, function ``g()`` is dead code if ``CONFIG_X`` is not set. Can we use ``expect`` here?”h]”(hŒHere, function ”…”�”}”(hjÆ h²hh³Nh´Nubj)�”}”(hŒ``g()``”h]”hŒg()”…”�”}”(hjÎ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÆ ubhŒ is dead code if ”…”�”}”(hjÆ h²hh³Nh´Nubj)�”}”(hŒ ``CONFIG_X``”h]”hŒCONFIG_X”…”�”}”(hjà h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÆ ubhŒ is not set. Can we use ”…”�”}”(hjÆ h²hh³Nh´Nubj)�”}”(hŒ ``expect``”h]”hŒexpect”…”�”}”(hjò h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjÆ ubhŒ here?”…”�”}”(hjÆ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M­hj£h²hubj{)�”}”(hŒK#[expect(dead_code)] fn g() {} fn main() { #[cfg(CONFIG_X)] g(); }”h]”hŒK#[expect(dead_code)] fn g() {} fn main() { #[cfg(CONFIG_X)] g(); }”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´M°hj£h²hubhÞ)�”}”(hŒžThis would emit a lint if ``CONFIG_X`` is set, since it is not dead code in that configuration. Therefore, in cases like this, we cannot use ``expect`` as-is.”h]”(hŒThis would emit a lint if ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ ``CONFIG_X``”h]”hŒCONFIG_X”…”�”}”(hj" h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒg is set, since it is not dead code in that configuration. Therefore, in cases like this, we cannot use ”…”�”}”(hj h²hh³Nh´Nubj)�”}”(hŒ ``expect``”h]”hŒexpect”…”�”}”(hj4 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj ubhŒ as-is.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mºhj£h²hubhÞ)�”}”(hŒ(A simple possibility is using ``allow``:”h]”(hŒA simple possibility is using ”…”�”}”(hjL h²hh³Nh´Nubj)�”}”(hŒ ``allow``”h]”hŒallow”…”�”}”(hjT h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjL ubhŒ:”…”�”}”(hjL h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´M½hj£h²hubj{)�”}”(hŒJ#[allow(dead_code)] fn g() {} fn main() { #[cfg(CONFIG_X)] g(); }”h]”hŒJ#[allow(dead_code)] fn g() {} fn main() { #[cfg(CONFIG_X)] g(); }”…”�”}”hjl sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´M¿hj£h²hubhÞ)�”}”(hŒ7An alternative would be using a conditional ``expect``:”h]”(hŒ,An alternative would be using a conditional ”…”�”}”(hj| h²hh³Nh´Nubj)�”}”(hŒ ``expect``”h]”hŒexpect”…”�”}”(hj„ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj| ubhŒ:”…”�”}”(hj| h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MÉhj£h²hubj{)�”}”(hŒd#[cfg_attr(not(CONFIG_X), expect(dead_code))] fn g() {} fn main() { #[cfg(CONFIG_X)] g(); }”h]”hŒd#[cfg_attr(not(CONFIG_X), expect(dead_code))] fn g() {} fn main() { #[cfg(CONFIG_X)] g(); }”…”�”}”hjœ sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆj‰jŒrust”j}”uh1jzh³hÇh´MËhj£h²hubhÞ)�”}”(hŒåThis would ensure that, if someone introduces another call to ``g()`` somewhere (e.g. unconditionally), then it would be spotted that it is not dead code anymore. However, the ``cfg_attr`` is more complex than a simple ``allow``.”h]”(hŒ>This would ensure that, if someone introduces another call to ”…”�”}”(hj¬ h²hh³Nh´Nubj)�”}”(hŒ``g()``”h]”hŒg()”…”�”}”(hj´ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¬ ubhŒk somewhere (e.g. unconditionally), then it would be spotted that it is not dead code anymore. However, the ”…”�”}”(hj¬ h²hh³Nh´Nubj)�”}”(hŒ ``cfg_attr``”h]”hŒcfg_attr”…”�”}”(hjÆ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¬ ubhŒ is more complex than a simple ”…”�”}”(hj¬ h²hh³Nh´Nubj)�”}”(hŒ ``allow``”h]”hŒallow”…”�”}”(hjØ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhj¬ ubhŒ.”…”�”}”(hj¬ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MÕhj£h²hubhÞ)�”}”(hŒÖTherefore, it is likely that it is not worth using conditional ``expect``\ s when more than one or two configurations are involved or when the lint may be triggered due to non-local changes (such as ``dead_code``).”h]”(hŒ?Therefore, it is likely that it is not worth using conditional ”…”�”}”(hjð h²hh³Nh´Nubj)�”}”(hŒ ``expect``”h]”hŒexpect”…”�”}”(hjø h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjð ubhŒ~ s when more than one or two configurations are involved or when the lint may be triggered due to non-local changes (such as ”…”�”}”(hjð h²hh³Nh´Nubj)�”}”(hŒ ``dead_code``”h]”hŒ dead_code”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jhjð ubhŒ).”…”�”}”(hjð h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MÙhj£h²hubhÞ)�”}”(hŒ;For more information about diagnostics in Rust, please see:”h]”hŒ;For more information about diagnostics in Rust, please see:”…”�”}”(hj" h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MÝhj£h²hubjå)�”}”(hŒGhttps://doc.rust-lang.org/stable/reference/attributes/diagnostics.html ”h]”hÞ)�”}”(hŒFhttps://doc.rust-lang.org/stable/reference/attributes/diagnostics.html”h]”jï)�”}”(hj6 h]”hŒFhttps://doc.rust-lang.org/stable/reference/attributes/diagnostics.html”…”�”}”(hj8 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”j6 uh1jîhj4 ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mßhj0 ubah}”(h]”h ]”h"]”h$]”h&]”uh1jäh³hÇh´Mßhj£h²hubeh}”(h]”Œlints”ah ]”h"]”Œlints”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´MQubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒError handling”h]”hŒError handling”…”�”}”(hj] h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjZ h²hh³hÇh´MâubhÞ)�”}”(hŒ\For some background and guidelines about Rust for Linux specific error handling, please see:”h]”hŒ\For some background and guidelines about Rust for Linux specific error handling, please see:”…”�”}”(hjk h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´MähjZ h²hubjå)�”}”(hŒThttps://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust”h]”hÞ)�”}”(hj{ h]”jï)�”}”(hj{ h]”hŒThttps://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust”…”�”}”(hj€ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”j{ uh1jîhj} ubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Mçhjy ubah}”(h]”h ]”h"]”h$]”h&]”uh1jäh³hÇh´MçhjZ h²hubeh}”(h]”Œerror-handling”ah ]”h"]”Œerror handling”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´Mâubeh}”(h]”Œcoding-guidelines”ah ]”h"]”Œcoding guidelines”ah$]”h&]”uh1hÈhhh²hh³hÇh´Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÇuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)�”}”(hÍNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”jÍ Œerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÇŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”j% ]”j asŒnameids”}”(j§ j¤ jíjêjåjâj¢jŸj*j'j·j´j j�jW jT jŸ jœ j� j% uŒ nametypes”}”(j§ ‰jí‰jå‰j¢‰j*‰j·‰j ‰jW ‰jŸ ‰j� ˆuh}”(j¤ hÊjêhíjâjØjŸjðj'j¥j´j-j�jºjT j£j j j% jD jœ jZ uŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”jD aŒautofootnote_refs”]”j aŒsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”jÛ Ks…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.