rust: time: Make HasHrTimer generic over HrTimerMode
authorFUJITA Tomonori <fujita.tomonori@gmail.com>
Tue, 10 Jun 2025 13:28:22 +0000 (22:28 +0900)
committerAndreas Hindborg <a.hindborg@kernel.org>
Tue, 24 Jun 2025 17:52:47 +0000 (19:52 +0200)
commite0c0ab04f6785abaa71b9b8dc252cb1a2072c225
treeb32661b36faa36ca993ffa3cab2ca69da171fc69
parentd9fc00dc73542eef98db74085447c57174ca290d
rust: time: Make HasHrTimer generic over HrTimerMode

Add a `TimerMode` associated type to the `HasHrTimer` trait to
represent the operational mode of the timer, such as absolute or
relative expiration. This new type must implement the `HrTimerMode`
trait, which defines how expiration values are interpreted.

Update the `start()` method to accept an `expires` parameter of type
`<Self::TimerMode as HrTimerMode>::Expires` instead of the fixed `Ktime`.
This enables different timer modes to provide strongly typed expiration
values, such as `Instant<C>` or `Delta`.

The `impl_has_hr_timer` macro is also extended to allow specifying the
`HrTimerMode`. In the following example, it guarantees that the
`start()` method for `Foo` only accepts `Instant<Monotonic>`. Using a
`Delta` or an `Instant` with a different clock source will result in a
compile-time error:

struct Foo {
    #[pin]
    timer: HrTimer<Self>,
}

impl_has_hr_timer! {
    impl HasHrTimer<Self> for Foo {
        mode : AbsoluteMode<Monotonic>,
        field : self.timer
    }
}

This design eliminates runtime mismatches between expires types and
clock sources, and enables stronger type-level guarantees throughout
hrtimer.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Link: https://lore.kernel.org/r/20250610132823.3457263-5-fujita.tomonori@gmail.com
[ changed conversion method names to `as_*` - Andreas ]
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
rust/kernel/time/hrtimer.rs
rust/kernel/time/hrtimer/arc.rs
rust/kernel/time/hrtimer/pin.rs
rust/kernel/time/hrtimer/pin_mut.rs
rust/kernel/time/hrtimer/tbox.rs