* [PATCH v2] rust: time: implement `Display` for `Delta`
@ 2026-11-03 15:23 Andreas Hindborg
2026-11-04 9:08 ` FUJITA Tomonori
0 siblings, 1 reply; 3+ messages 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] 3+ messages in thread
* Re: [PATCH v2] rust: time: implement `Display` for `Delta`
2026-11-03 15:23 [PATCH v2] rust: time: implement `Display` for `Delta` Andreas Hindborg
@ 2026-11-04 9:08 ` FUJITA Tomonori
2026-11-04 10:07 ` Andreas Hindborg
0 siblings, 1 reply; 3+ messages in thread
From: FUJITA Tomonori @ 2026-11-04 9:08 UTC (permalink / raw)
To: a.hindborg
Cc: fujita.tomonori, frederic, lyude, tglx, anna-maria, jstultz,
sboyd, ojeda, gary, bjorn3_gh, lossin, aliceryhl, tmgross, dakr,
boqun, daniel.almeida, tamird, acourbot, work, rust-for-linux,
linux-kernel
On Tue, 03 Nov 2026 16:23:40 +0100
Andreas Hindborg <a.hindborg@kernel.org> wrote:
> Implement the `Display` trait for `Delta` so that it can be displayed by a
> formatter.
[...]
> - Drop the `rnull` user, which will call `Delta::as_nanos` instead (Gary).
Now that the rnull user has been dropped, I wonder if we need this
now.
If we want a readable format with a unit, Debug might be a better fit.
core::time::Duration implements only Debug, not Display, and its Debug
output includes the unit (e.g. 1.5s). Debug output is not expected to
be stable, so we could change the format later. Users that need a
fixed format can use as_nanos(), like rnull does.
What do you think?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] rust: time: implement `Display` for `Delta`
2026-11-04 9:08 ` FUJITA Tomonori
@ 2026-11-04 10:07 ` Andreas Hindborg
0 siblings, 0 replies; 3+ messages in thread
From: Andreas Hindborg @ 2026-11-04 10:07 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: fujita.tomonori, frederic, lyude, tglx, anna-maria, jstultz,
sboyd, ojeda, gary, bjorn3_gh, lossin, aliceryhl, tmgross, dakr,
boqun, daniel.almeida, tamird, acourbot, work, rust-for-linux,
linux-kernel
"FUJITA Tomonori" <tomo@flapping.org> writes:
> On Tue, 03 Nov 2026 16:23:40 +0100
> Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
>> Implement the `Display` trait for `Delta` so that it can be displayed by a
>> formatter.
>
> [...]
>
>> - Drop the `rnull` user, which will call `Delta::as_nanos` instead (Gary).
>
> Now that the rnull user has been dropped, I wonder if we need this
> now.
>
> If we want a readable format with a unit, Debug might be a better fit.
> core::time::Duration implements only Debug, not Display, and its Debug
> output includes the unit (e.g. 1.5s). Debug output is not expected to
> be stable, so we could change the format later. Users that need a
> fixed format can use as_nanos(), like rnull does.
>
> What do you think?
I think consensus on v1 was that a `Display` implementation would be
good to have even though no user, which is why I decided to send this
again.
I do think it is nice to have, but maybe we should consider doing some
kind of intelligent selection of the unit with rounding?
We can also change it to be a `Debug` implementation. I was not aware
about the stability requirement. But I guess that is mostly a user space
thing? In the kernel we can change stuff as we see fit, as long as we do
not interfere with the uapi.
Best regards,
Andreas Hindborg
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-04 10:08 UTC | newest]
Thread overview: 3+ messages (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
2026-11-04 9:08 ` FUJITA Tomonori
2026-11-04 10:07 ` 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®