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

FUJITA Tomonori
2025-04-03T04:40:57Z

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

> FUJITA Tomonori <fujita.tomonori@gmail.com> 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@collabora.com>
>> Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
>> Reviewed-by: Gary Guo <gary@garyguo.net>
>> Reviewed-by: Fiona Behrens <me@kloenk.dev>
>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
>
>
> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
>
>
> 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.