Re: [PATCH] rust: time: Seal the HrTimerMode trait

From: Boqun Feng
Date: Wed Jun 18 2025 - 20:29:19 EST




On Tue, Jun 17, 2025, at 4:28 PM, FUJITA Tomonori wrote:
> Prevent downstream crates or drivers from implementing `HrTimerMode`
> for arbitrary types, which could otherwise leads to unsupported
> behavior.
>
> Introduce a `private::Sealed` trait and implement it for all types
> that implement `HrTimerMode`.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>

Reviewed-by: Boqun Feng <boqun.feng@xxxxxxxxx>

Thanks!

Regards,
Boqun

> ---
> rust/kernel/time/hrtimer.rs | 21 ++++++++++++++++++++-
> 1 file changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
> index a6468bc50e57..0028720cb4e9 100644
> --- a/rust/kernel/time/hrtimer.rs
> +++ b/rust/kernel/time/hrtimer.rs
> @@ -442,8 +442,27 @@ fn into_nanos(self) -> i64 {
> }
> }
>
> +mod private {
> + use crate::time::ClockSource;
> +
> + pub trait Sealed {}
> +
> + impl<C: ClockSource> Sealed for super::AbsoluteMode<C> {}
> + impl<C: ClockSource> Sealed for super::RelativeMode<C> {}
> + impl<C: ClockSource> Sealed for super::AbsolutePinnedMode<C> {}
> + impl<C: ClockSource> Sealed for super::RelativePinnedMode<C> {}
> + impl<C: ClockSource> Sealed for super::AbsoluteSoftMode<C> {}
> + impl<C: ClockSource> Sealed for super::RelativeSoftMode<C> {}
> + impl<C: ClockSource> Sealed for super::AbsolutePinnedSoftMode<C> {}
> + impl<C: ClockSource> Sealed for super::RelativePinnedSoftMode<C> {}
> + impl<C: ClockSource> Sealed for super::AbsoluteHardMode<C> {}
> + impl<C: ClockSource> Sealed for super::RelativeHardMode<C> {}
> + impl<C: ClockSource> Sealed for super::AbsolutePinnedHardMode<C> {}
> + impl<C: ClockSource> Sealed for super::RelativePinnedHardMode<C> {}
> +}
> +
> /// Operational mode of [`HrTimer`].
> -pub trait HrTimerMode {
> +pub trait HrTimerMode: private::Sealed {
> /// The C representation of hrtimer mode.
> const C_MODE: bindings::hrtimer_mode;
>
>
> base-commit: 994393295c89711531583f6de8f296a30b0d944a
> --
> 2.43.0