Skip to contentSkip to conversation

docs: rust: add guidelines for Display and Debug

PATCH In review FUJITA Tomonori 4 participants 4 messages Nov 30 – Dec 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 docs: rust: add guidelines for Display and Debug

From: FUJITA Tomonori <[email protected]>

The kernel crate has no written rule about when a type should implement Display and when it should implement only Debug. This came up in the review of a patch that adds Display for Delta.

Add a section that follows the usual Rust convention, with one kernel specific point about types that correspond to C types. Also say that the output of Debug may change, and that a type should provide a method when a stable output is needed.

---
v2:
- Remove the anchor from the Display link (Miguel)
- Say that the Debug output may change (Miguel)
- Suggest a method for a stable output, e.g. Delta::as_nanos (Miguel)
v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 Documentation/rust/coding-guidelines.rst | 26 ++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
Documentation/rust/coding-guidelines.rst +26 -0
index 3198be3a6d63..e32bda74fd29 100644
@@ -333,6 +333,32 @@ That is, the equivalent of ``GPIO_LINE_DIRECTION_IN`` would be referred to as
 ``gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN``.
 
 
+Formatting traits
+-----------------
+
+Rust kernel code follows the usual Rust convention that ``Display`` should only
+be implemented when there is a single obvious way to format the values of a type
+as text:
+
+	https://doc.rust-lang.org/core/fmt/trait.Display.html
+
+For a type that corresponds to a C type, a format that the C side already uses
+also counts as such an obvious way, e.g. the output of a ``printk`` format
+specifier such as ``%pe``.
+
+On the other hand, some types have no single obvious format. For example, a
+quantity with a unit, such as ``Delta`` or ``Hertz``, can be printed in
+different units and with different precision.
+
+For such types, ``Debug`` should be implemented instead of ``Display``. Its
+output is for debugging, so any format that is useful for developers can be
+used. Usually ``#[derive(Debug)]`` is enough.
+
+The output of ``Debug`` may change, so other code should not depend on it. When
+a stable output is needed, the type should provide a method for it, and users
+should call that method instead, e.g. ``Delta::as_nanos``.
+
+
 Lints
 -----
 

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

The kernel crate has no written rule about when a type should implement Display and when it should implement only Debug. This came up in the review of a patch that adds Display for Delta.

Quoted 7 lines

Add a section that follows the usual Rust convention, with one kernel specific point about types that correspond to C types. Also say that the output of Debug may change, and that a type should provide a method when a stable output is needed.

Best regards,
Andreas Hindborg

reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to FUJITA Tomonori
Quoted 7 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

The kernel crate has no written rule about when a type should implement Display and when it should implement only Debug. This came up in the review of a patch that adds Display for Delta.

Quoted 7 lines

Add a section that follows the usual Rust convention, with one kernel specific point about types that correspond to C types. Also say that the output of Debug may change, and that a type should provide a method when a stable output is needed.

reviewer Reviewed-by on v2 1/1
#3 lore raw
In reply to FUJITA Tomonori
Quoted 6 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

The kernel crate has no written rule about when a type should implement Display and when it should implement only Debug. This came up in the review of a patch that adds Display for Delta.

Quoted 7 lines

Add a section that follows the usual Rust convention, with one kernel specific point about types that correspond to C types. Also say that the output of Debug may change, and that a type should provide a method when a stable output is needed.

Quoted 9 lines

---
v2:
- Remove the anchor from the Display link (Miguel)
- Say that the Debug output may change (Miguel)
- Suggest a method for a stable output, e.g. Delta::as_nanos (Miguel)
v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 Documentation/rust/coding-guidelines.rst | 26 ++++++++++++++++++++++++
 1 file changed, 26 insertions(+)