[PATCH] rust: time: Seal the HrTimerMode trait

FUJITA Tomonori posted 1 patch 3 months, 3 weeks ago
rust/kernel/time/hrtimer.rs | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
[PATCH] rust: time: Seal the HrTimerMode trait
Posted by FUJITA Tomonori 3 months, 3 weeks ago
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@gmail.com>
---
 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
Re: [PATCH] rust: time: Seal the HrTimerMode trait
Posted by Andreas Hindborg 3 months, 1 week ago
On Wed, 18 Jun 2025 08:28:06 +0900, 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`.
> 
> [...]

Applied, thanks!

[1/1] rust: time: Seal the HrTimerMode trait
      commit: fc38b7ff879683669bd9ff5dc7e7b6aeeb07bf2a

Best regards,
-- 
Andreas Hindborg <a.hindborg@kernel.org>
Re: [PATCH] rust: time: Seal the HrTimerMode trait
Posted by Boqun Feng 3 months, 3 weeks ago

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@gmail.com>

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

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