Re: [PATCH v5 1/7] rust: time: make Delta generic over its time unit
FUJITA Tomonori <[email protected]>
| Newsgroups | org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <[email protected]> |
On Sun, 09 Aug 2026 11:26:49 +0900 "Alexandre Courbot" <[email protected]> wrote: > On Fri Aug 7, 2026 at 9:10 PM JST, FUJITA Tomonori wrote: >>>> + >>>> +impl TimeUnit for Nsec { >>>> + type Repr = i64; >>>> +} >>>> + >>>> /// A span of time. >>>> /// >>>> -/// This struct represents a span of time, with its value stored as nanoseconds. >>>> -/// The value can represent any valid i64 value, including negative, zero, and >>>> -/// positive numbers. >>>> +/// The span is stored in the unit given by the type parameter `U` (see >>>> +/// [`TimeUnit`]); its value has type `U::Repr`. `U` defaults to [`Nsec`], so a >>>> +/// plain [`Delta`] is a span in nanoseconds. The value can be negative, zero, or >>>> +/// positive. >>>> #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)] >>>> -pub struct Delta { >>>> - nanos: i64, >>>> +pub struct Delta<U: TimeUnit = Nsec> { >>>> + value: U::Repr, >>>> } >>>> >>>> impl ops::Add for Delta { >>> >>> When you add `Jiffy` later, this impl block will only cover >>> `Delta<Nsec>`. Is that intentional, or did you intend to support >>> all these operations operations for `Delta<Jiffy>` as well? >> >> Intentional. Delta<Jiffy> exists to carry a jiffies-valued timeout >> across the C boundary; it is not meant as a general arithmetic type. I >> can add them if a user needs them. > > Is there a reason for not doing it now? Common arithmetic sounds useful > for any unit, and since they are already agreed to panic at the bounds > we can constrain them on `U` implementing the corresponding traits. I was planning to do that in a separate patch on top of this series. You are right that `Add`, `Sub`, `Mul` and the `*Assign` variants are straightforward. `Div` isn't; unlike `Delta<Nsec>`, `Delta<Jiffy>` needs no CONFIG_64BIT split, as a single shared impl doesn't work. This part needs some discussion, so I'd rather keep it out of this series.