Re: [PATCH v3] rust: time: implement `Debug` for `Delta`

From: Andreas Hindborg

Date: Thu Nov 26 2026 - 08:31:55 EST


Alice Ryhl <aliceryhl@xxxxxxxxxx> writes:

> On Thu, Nov 26, 2026 at 11:51:14AM +0100, Andreas Hindborg wrote:
>> Implement the `Debug` trait for `Delta` so that a span of time can be
>> printed by a formatter.
>>
>> The span is printed as a count of the time unit it is stored in, suffixed
>> with that unit, and with each group of three digits separated by an
>> underscore the way Rust integer literals are written. A span of 12345
>> nanoseconds is printed as `12_345 ns`.
>>
>> `Debug` is implemented rather than `Display`, because `Delta` has no single
>> obvious format and the other kernel types that carry a unit, `Hertz`,
>> `MicroVolt` and `MicroWatt`, implement `Debug` only. Callers that need a
>> specific format should use `Delta::as_nanos` and print the result
>> themselves.
>
> Thanks for addressing the earlier feedback! Aside from the 32-bit division
> issue that Tomonori already pointed out, this looks good to me.
>
>> + #[inline]
>> + fn repr_to_i64(repr: Self::Repr) -> i64 {
>> + // CAST: `isize` is 32 or 64 bits wide on the architectures the kernel
>> + // supports, so widening to `i64` preserves the value.
>> + repr as i64
>> + }
>
> Maybe we could add `impl FromSafeCast<isize> for i64` to
> `kernel::num::casts` and use `i64::from_safe_cast(repr)` here?

I can try to add that.

>
>> + let mut groups = [0u64; 7];
>
> Minor nit: since each group is `< 1000`, this could be `[0u16; 7]` instead
> of `[0u64; 7]`.

I chose the u64 so I would not have to narrow from 64 to 16. I valued
the cleaner code over the wasted bits.

Best regards,
Andreas Hindborg