Re: [PATCH v5 1/7] rust: time: make Delta generic over its time unit
"Alexandre Courbot" <[email protected]>
| Newsgroups | org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <[email protected]> |
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.