Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH v2] docs: rust: add guidelines for Display and Debug
@ 2026-11-30 23:43 ` FUJITA Tomonori
  2026-12-01 10:08   ` Andreas Hindborg
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: FUJITA Tomonori @ 2026-11-30 23:43 UTC (permalink / raw)
  To: corbet, ojeda
  Cc: a.hindborg, acourbot, aliceryhl, bjorn3_gh, boqun, dakr,
	daniel.almeida, gary, lossin, rdunlap, skhan, tamird, tmgross,
	work, linux-doc, rust-for-linux, FUJITA Tomonori

From: FUJITA Tomonori <fujita.tomonori@gmail.com>

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.

Link: https://lore.kernel.org/rust-for-linux/87se19qgeu.fsf@t14s.mail-host-address-is-not-set/
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
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/20261115214025.2122718-1-tomo@flapping.org/
---
 Documentation/rust/coding-guidelines.rst | 26 ++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/Documentation/rust/coding-guidelines.rst b/Documentation/rust/coding-guidelines.rst
index 3198be3a6d63..e32bda74fd29 100644
--- a/Documentation/rust/coding-guidelines.rst
+++ b/Documentation/rust/coding-guidelines.rst
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] docs: rust: add guidelines for Display and Debug
  2026-11-30 23:43 ` [PATCH v2] docs: rust: add guidelines for Display and Debug FUJITA Tomonori
@ 2026-12-01 10:08   ` Andreas Hindborg
  2026-12-01 13:34   ` Alice Ryhl
  2026-12-01 13:44   ` Gary Guo
  2 siblings, 0 replies; 4+ messages in thread
From: Andreas Hindborg @ 2026-12-01 10:08 UTC (permalink / raw)
  To: FUJITA Tomonori, corbet, ojeda
  Cc: acourbot, aliceryhl, bjorn3_gh, boqun, dakr, daniel.almeida, gary,
	lossin, rdunlap, skhan, tamird, tmgross, work, linux-doc,
	rust-for-linux, FUJITA Tomonori

"FUJITA Tomonori" <tomo@flapping.org> writes:

> From: FUJITA Tomonori <fujita.tomonori@gmail.com>
>
> 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.
>
> Link: https://lore.kernel.org/rust-for-linux/87se19qgeu.fsf@t14s.mail-host-address-is-not-set/
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>

Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>

Best regards,
Andreas Hindborg



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] docs: rust: add guidelines for Display and Debug
  2026-11-30 23:43 ` [PATCH v2] docs: rust: add guidelines for Display and Debug FUJITA Tomonori
  2026-12-01 10:08   ` Andreas Hindborg
@ 2026-12-01 13:34   ` Alice Ryhl
  2026-12-01 13:44   ` Gary Guo
  2 siblings, 0 replies; 4+ messages in thread
From: Alice Ryhl @ 2026-12-01 13:34 UTC (permalink / raw)
  To: FUJITA Tomonori
  Cc: corbet, ojeda, a.hindborg, acourbot, bjorn3_gh, boqun, dakr,
	daniel.almeida, gary, lossin, rdunlap, skhan, tamird, tmgross,
	work, linux-doc, rust-for-linux, FUJITA Tomonori

On Tue, Dec 1, 2026 at 12:43 AM FUJITA Tomonori <tomo@flapping.org> wrote:
>
> From: FUJITA Tomonori <fujita.tomonori@gmail.com>
>
> 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.
>
> Link: https://lore.kernel.org/rust-for-linux/87se19qgeu.fsf@t14s.mail-host-address-is-not-set/
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] docs: rust: add guidelines for Display and Debug
  2026-11-30 23:43 ` [PATCH v2] docs: rust: add guidelines for Display and Debug FUJITA Tomonori
  2026-12-01 10:08   ` Andreas Hindborg
  2026-12-01 13:34   ` Alice Ryhl
@ 2026-12-01 13:44   ` Gary Guo
  2 siblings, 0 replies; 4+ messages in thread
From: Gary Guo @ 2026-12-01 13:44 UTC (permalink / raw)
  To: FUJITA Tomonori, corbet, ojeda
  Cc: a.hindborg, acourbot, aliceryhl, bjorn3_gh, boqun, dakr,
	daniel.almeida, gary, lossin, rdunlap, skhan, tamird, tmgross,
	work, linux-doc, rust-for-linux, FUJITA Tomonori

On Mon Nov 30, 2026 at 11:43 PM GMT, FUJITA Tomonori wrote:
> From: FUJITA Tomonori <fujita.tomonori@gmail.com>
> 
> 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.
> 
> Link: https://lore.kernel.org/rust-for-linux/87se19qgeu.fsf@t14s.mail-host-address-is-not-set/
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
> 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/20261115214025.2122718-1-tomo@flapping.org/
> ---
>  Documentation/rust/coding-guidelines.rst | 26 ++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 13:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <SPsuLLzxgxO7PaZCjQrJslthAe9jYAU5mRDRgypaT3BVUAG4QEuuGTFfiDVZT42xuKDHHNXOuT-dT0Y-_I9LFA==@protonmail.internalid>
2026-11-30 23:43 ` [PATCH v2] docs: rust: add guidelines for Display and Debug FUJITA Tomonori
2026-12-01 10:08   ` Andreas Hindborg
2026-12-01 13:34   ` Alice Ryhl
2026-12-01 13:44   ` Gary Guo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox