Re: [PATCH net-next v2 2/6] rust: time: Introduce Delta type

From: FUJITA Tomonori
Date: Mon Oct 07 2024 - 02:02:04 EST


On Sat, 5 Oct 2024 20:02:55 +0200
Andrew Lunn <andrew@xxxxxxx> wrote:

>> +/// A span of time.
>> +#[derive(Copy, Clone)]
>> +pub struct Delta {
>> + nanos: i64,
>
> Is there are use case for negative Deltas ? Should this be u64?

I thought that logically Delta could be negative but considering Ktime
APIs like the following, I think that u64 is more appropriate now.

static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec)
{
return ktime_add_ns(kt, usec * NSEC_PER_USEC);
}

static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
{
return ktime_sub_ns(kt, usec * NSEC_PER_USEC);
}

>> +}
>> +
>> +impl Delta {
>> + /// Create a new `Delta` from a number of nanoseconds.
>> + #[inline]
>> + pub fn from_nanos(nanos: u16) -> Self {
>
> So here you don't allow negative values.
>
> But why limit it to u16, when the base value is a 63 bits? 65535 nS is
> not very long.

I thought that from_secs(u16) gives long enough duration but
how about the following APIs?

pub fn from_nanos(nanos: u64)
pub fn from_micros(micros: u32)
pub fn from_millis(millis: u16)

You can create the maximum via from_nanos. from_micros and from_millis
don't cause wrapping.