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 Thu, 06 Aug 2026 11:44:21 +0200 Andreas Hindborg <[email protected]> wrote: >> +/// A time unit of nanoseconds. >> +/// >> +/// A [`Delta<Nsec>`] stores its value as `i64` nanoseconds and can represent >> +/// any `i64` value, including negative, zero, and positive numbers. >> +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)] >> +pub struct Nsec; > > Should we make these enum with zero variants to indicate they should not > be constructed? Good idea, I will change it in v6. >> + >> +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.