[PATCH v2] rust: time: implement `Display` for `Delta`
From: Andreas Hindborg
Date: Tue Nov 03 2026 - 11:01:18 EST
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@xxxxxxxxxx>
---
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@xxxxxxxxxx
---
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@xxxxxxxxxx>