rust: time: implement `Display` for `Delta`
Subsystems (2)
Andreas Hindborg
maintainerPATCH v2 1/1 rust: time: implement `Display` for `Delta`
Implement the Display trait for Delta so that it can be displayed by a formatter.
---
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/[email protected]
--- rust/kernel/time.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
rust/kernel/time.rs +19 -0
@@ -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,
Signature · 1 line
Andreas Hindborg <[email protected]>
Quoted 2 lines · On Tue, 03 Nov 2026 16:23:40 +0100 Andreas Hindborg <[email protected]>…
Implement the
Displaytrait forDeltaso that it can be displayed by a formatter.
[...]
Quoted 1 line
- Drop the
rnulluser, which will callDelta::as_nanosinstead (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?
Andreas Hindborg
maintainerQuoted 13 lines · FUJITA Tomonori
Quoted 2 lines · On Tue, 03 Nov 2026 16:23:40 +0100 Andreas Hindborg <[email protected]>…
Implement the
Displaytrait forDeltaso that it can be displayed by a formatter.[...]
Quoted 1 line
- Drop the
rnulluser, which will callDelta::as_nanosinstead (Gary).Now that the rnull user has been dropped, I wonder if we need this now.
Quoted 7 lines
If we want a readable format with a unit, Debug might be a better fit.
core::time::Durationimplements 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 useas_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
Quoted 4 lines · On Wed, 04 Nov 2026 11:07:42 +0100 Andreas Hindborg <[email protected]>…
We can also change it to be a
Debugimplementation. 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.
Right, in the kernel we can change the format as needed.
Still, I think it is easier for users if the kernel crate follows some rule about when to implement Display and when to implement Debug.
As Miguel's suggestions in v1 show (12_345ns, 12.345ms, 12.3ms), Delta does not have a single obvious format. In the kernel crate, other types with a unit, Hertz, MicroVolt and MicroWatt, implement only Debug, not Display. Display for Delta would be the first exception.
So I would prefer Debug.
Maybe we should also write such a rule down inDocumentation/rust/coding-guidelines.rst.
Andreas Hindborg
maintainerQuoted 13 lines · FUJITA Tomonori
Quoted 4 lines · On Wed, 04 Nov 2026 11:07:42 +0100 Andreas Hindborg <[email protected]>…
We can also change it to be a
Debugimplementation. 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.Right, in the kernel we can change the format as needed.
Still, I think it is easier for users if the kernel crate follows some rule about when to implement Display and when to implement Debug.
Quoted 6 lines
As Miguel's suggestions in v1 show (12_345ns, 12.345ms, 12.3ms), Delta does not have a single obvious format. In the kernel crate, other types with a unit, Hertz, MicroVolt and MicroWatt, implement only Debug, not Display. Display for Delta would be the first exception.
So I would prefer Debug.
Ok, I can respin it as Debug.
Quoted 3 lines
Maybe we should also write such a rule down in
Documentation/rust/coding-guidelines.rst.
Good idea, I suggest you submit a patch, then general discussion can happen in that thread.
Best regards,
Andreas Hindborg
Quoted 3 lines · Andreas Hindborg
Good idea, I suggest you submit a patch, then general discussion can happen in that thread.
Yes, please!
Cheers,
Miguel