Re: [PATCH v8 2/7] rust: time: Introduce Delta type

From: FUJITA Tomonori
Date: Thu Jan 16 2025 - 07:01:07 EST


On Thu, 16 Jan 2025 10:36:07 +0100
Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:

> On Thu, Jan 16, 2025 at 5:42 AM FUJITA Tomonori
> <fujita.tomonori@xxxxxxxxx> wrote:
>>
>> Introduce a type representing a span of time. Define our own type
>> because `core::time::Duration` is large and could panic during
>> creation.
>>
>> time::Ktime could be also used for time duration but timestamp and
>> timedelta are different so better to use a new type.
>>
>> i64 is used instead of u64 to represent a span of time; some C drivers
>> uses negative Deltas and i64 is more compatible with Ktime using i64
>> too (e.g., ktime_[us|ms]_delta() APIs return i64 so we create Delta
>> object without type conversion.
>>
>> i64 is used instead of bindings::ktime_t because when the ktime_t
>> type is used as timestamp, it represents values from 0 to
>> KTIME_MAX, which different from Delta.
>>
>> Delta::from_[millis|secs] APIs take i64. When a span of time
>> overflows, i64::MAX is used.
>>
>> Reviewed-by: Andrew Lunn <andrew@xxxxxxx>
>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
>
> One nit below, otherwise LGTM
>
> Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>

Thanks!

>> + /// Return the number of nanoseconds in the `Delta`.
>> + #[inline]
>> + pub fn as_nanos(self) -> i64 {
>> + self.nanos
>> + }
>
> I added the ktime_ms_delta() function because I was going to use it.
> Can you add an `as_millis()` function too? That way I can use
> start_time.elapsed().as_millis() for my use-case.

Surely, I'll in the next version.

I dropped as_millis() method in v4 because I followed the rule, don't
add an API that may not be used.