rust: time: implement `Display` for `Delta`
Subsystems (2)
Andreas Hindborg
maintainerPATCH 1/1 rust: time: implement `Display` for `Delta`
Implement the Display trait for Delta so that it can be displayed by a formatter.
--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)
rust/kernel/time.rs +6 -0
@@ -474,3 +474,9 @@ pub fn rem_nanos(self, dividend: i32) -> Self { } } } + +impl kernel::fmt::Display for Delta { +fn fmt (&self, f: &mut kernel::fmt::Formatter <'_>) -> kernel::fmt::Result { + f.write_fmt (kernel::prelude::fmt!("{}" , self.as_nanos ())) + } +}
---
base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-time-delta-display-d702e7788d2d
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>
Alice Ryhl
reviewerQuoted 13 lines · Andreas Hindborg
Implement the
Displaytrait forDeltaso that it can be displayed by a formatter.Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/time.rs +0
-0
rust/kernel/time.rs pub fn rem_nanos(self, dividend: i32) -> Self { · line 480
Quoted 7 lines
rust/kernel/time.rs +4
-0@@ -474,3 +474,9 @@ pub fn rem_nanos(self, dividend: i32) -> Self { } } } + +impl kernel::fmt::Display for Delta { +fn fmt (&self, f: &mut kernel::fmt::Formatter <'_>) -> kernel::fmt::Result { + f.write_fmt (kernel::prelude::fmt!("{}" , self.as_nanos ()))
I think this can just be
self.as_nanos().fmt(f)
Though I think it would be nicer to include an 'ns' suffix here so it's not just the number?
Alice
Andreas Hindborg
maintainerQuoted 24 lines · Alice Ryhl
Quoted 13 lines · Andreas Hindborg
Implement the
Displaytrait forDeltaso that it can be displayed by a formatter.Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/time.rs +0
-0rust/kernel/time.rs pub fn rem_nanos(self, dividend: i32) -> Self { · line 480
Quoted 7 lines
rust/kernel/time.rs +4
-0@@ -474,3 +474,9 @@ pub fn rem_nanos(self, dividend: i32) -> Self { } } } + +impl kernel::fmt::Display for Delta { +fn fmt (&self, f: &mut kernel::fmt::Formatter <'_>) -> kernel::fmt::Result { + f.write_fmt (kernel::prelude::fmt!("{}" , self.as_nanos ()))I think this can just be
Quoted 4 lines
self.as_nanos().fmt(f)Though I think it would be nicer to include an 'ns' suffix here so it's not just the number?
I was planning to use this to format a delta to output via a configfs in rnull. The existing C driver does not include the "ns" suffix, which is why I did not either.
I am not opposed to adding the suffix for this particular implementation, but that would mean my user for this code will no longer be a user, and I would drop the patch in favor of another solution at the call site.
Please advise which of the following make most sense:
- Merge this patch without a suffix.
- Iterate on this patch and merge it without a user.
- Drop this patch for now.
Best regards,
Andreas Hindborg
Gary Guo
reviewerQuoted 40 lines · Andreas Hindborg
Quoted 24 lines · Alice Ryhl
Quoted 13 lines · Andreas Hindborg
Implement the
Displaytrait forDeltaso that it can be displayed by a formatter.Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/time.rs +0
-0rust/kernel/time.rs pub fn rem_nanos(self, dividend: i32) -> Self { · line 480
Quoted 7 lines
rust/kernel/time.rs +4
-0@@ -474,3 +474,9 @@ pub fn rem_nanos(self, dividend: i32) -> Self { } } } + +impl kernel::fmt::Display for Delta { +fn fmt (&self, f: &mut kernel::fmt::Formatter <'_>) -> kernel::fmt::Result { + f.write_fmt (kernel::prelude::fmt!("{}" , self.as_nanos ()))I think this can just be
Quoted 4 lines
self.as_nanos().fmt(f)Though I think it would be nicer to include an 'ns' suffix here so it's not just the number?
I was planning to use this to format a delta to output via a configfs in rnull. The existing C driver does not include the "ns" suffix, which is why I did not either.
I am not opposed to adding the suffix for this particular implementation, but that would mean my user for this code will no longer be a user, and I would drop the patch in favor of another solution at the call site.
Quoted 5 lines
Please advise which of the following make most sense:
- Merge this patch without a suffix.
- Iterate on this patch and merge it without a user.
- Drop this patch for now.
Display is supposed to be user-facing and readable. So to me it's weird that the "Display" impl would format "Delta" as nanoseconds without a suffix. I think if you need to depend on its behaviour to be just nanosecond numbers, you should perhaps just use delta.as_nanos() on the print side.
Best,
Gary
Quoted 6 lines · Andreas Hindborg
Please advise which of the following make most sense:
- Merge this patch without a suffix.
- Iterate on this patch and merge it without a user.
- Drop this patch for now.
Having ns may be a good idea.
In fact, if you do that, you could consider using separators every few digits if the number is too large (like Rust integer literals allow with underscores), e.g. 12_345ns
Or you could even consider picking other units depending on the magnitude, e.g. 12.345ms. After all, if we are going for human readability, then there is an argument to be made for that. Even about showing only a few significant figures, e.g. 12.3ms. Anyone that needs more should probably be doing something manually with the nanos anyway.
But it is up to you and timekeeping, of course. I guess it should be decided depending on the expected users and what most of them would want to see in the majority of cases.
Cheers,
Miguel