Re: [PATCH v11 4/8] rust: time: Introduce Instant type

From: FUJITA Tomonori
Date: Thu Apr 03 2025 - 00:41:14 EST


On Sat, 22 Mar 2025 14:58:16 +0100
Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:

> FUJITA Tomonori <fujita.tomonori@xxxxxxxxx> writes:
>
>> Introduce a type representing a specific point in time. We could use
>> the Ktime type but C's ktime_t is used for both timestamp and
>> timedelta. To avoid confusion, introduce a new Instant type for
>> timestamp.
>>
>> Rename Ktime to Instant and modify their methods for timestamp.
>>
>> Implement the subtraction operator for Instant:
>>
>> Delta = Instant A - Instant B
>>
>> Tested-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
>> Reviewed-by: Boqun Feng <boqun.feng@xxxxxxxxx>
>> Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>
>> Reviewed-by: Fiona Behrens <me@xxxxxxxxxx>
>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
>
>
> Reviewed-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
>
>
> As Boqun mentioned, we should make this generic over `ClockId` when the
> hrtimer patches land.

Seems that I overlooked his mail. Can you give me a pointer?

I assume that you want the Instance type to vary depending on the
clock source.


>> -/// Returns the number of milliseconds between two ktimes.
>> -#[inline]
>> -pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 {
>> - (later - earlier).to_ms()
>> -}
>> -
>> -impl core::ops::Sub for Ktime {
>> - type Output = Ktime;
>> +impl core::ops::Sub for Instant {
>> + type Output = Delta;
>>
>> + // By the type invariant, it never overflows.
>> #[inline]
>> - fn sub(self, other: Ktime) -> Ktime {
>> - Self {
>> - inner: self.inner - other.inner,
>> + fn sub(self, other: Instant) -> Delta {
>> + Delta {
>> + nanos: self.inner - other.inner,
>
> If this never overflows by invariant, would it make sense to use
> `unchecked_sub` or `wraping_sub`? That would remove the overflow check.

Yeah, I think that it can. But I prefer to keep the code as is to
catch a bug.