mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] rust: time: implement `Display` for `Delta`
@ 2026-11-03 15:23 Andreas Hindborg
  0 siblings, 0 replies; only message in thread
From: Andreas Hindborg @ 2026-11-03 15:23 UTC (permalink / raw)
  To: FUJITA Tomonori, Frederic Weisbecker, Lyude Paul,
	Thomas Gleixner, Anna-Maria Behnsen, John Stultz, Stephen Boyd,
	Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Boqun Feng,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, Boqun Feng
  Cc: rust-for-linux, linux-kernel, Andreas Hindborg

Implement the `Display` trait for `Delta` so that it can be displayed by a
formatter.

Assisted-by: LLM
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
Changes in v2:
- Rebased on v7.4-rc1.
- Suffix the formatted value with `ns` (Alice, Gary, Miguel).
- Write the value directly rather than via `write_fmt` and `fmt!` (Alice).
- Document the format with a rustdoc example.
- Drop the `rnull` user, which will call `Delta::as_nanos` instead (Gary).
- Link to v1: https://msgid.link/20260215-time-delta-display-v1-1-bdd2d6e98ebf@kernel.org
---
 rust/kernel/time.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index b6a2aab12a50..eac581e845a6 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -669,6 +669,25 @@ pub fn rem_nanos(self, dividend: i32) -> Self {
     }
 }
 
+impl kernel::fmt::Display for Delta {
+    /// Formats the span as a number of nanoseconds, suffixed with `ns`.
+    ///
+    /// ```
+    /// # use kernel::{prelude::fmt, str::CString, time::Delta};
+    /// let delta = Delta::from_micros(5);
+    /// let formatted = CString::try_from_fmt(fmt!("{delta}"))?;
+    /// assert_eq!(formatted.to_bytes(), b"5000ns");
+    ///
+    /// let negative = Delta::from_millis(-2);
+    /// let formatted = CString::try_from_fmt(fmt!("{negative}"))?;
+    /// assert_eq!(formatted.to_bytes(), b"-2000000ns");
+    /// # Ok::<(), kernel::error::Error>(())
+    /// ```
+    fn fmt(&self, f: &mut kernel::fmt::Formatter<'_>) -> kernel::fmt::Result {
+        write!(f, "{}ns", self.as_nanos())
+    }
+}
+
 #[cfg(CONFIG_RUST_TIME_KUNIT_TEST)]
 #[macros::kunit_tests(rust_kernel_time)]
 mod tests {

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260215-time-delta-display-d702e7788d2d

Best regards,
--  
Andreas Hindborg <a.hindborg@kernel.org>



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-03 15:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 15:23 [PATCH v2] rust: time: implement `Display` for `Delta` Andreas Hindborg

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

all inboxes | Powered by JetHome®