rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: time: Seal the HrTimerMode trait
@ 2025-06-17 23:28 FUJITA Tomonori
  2025-06-19  0:28 ` Boqun Feng
  2025-06-30 12:07 ` Andreas Hindborg
  0 siblings, 2 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2025-06-17 23:28 UTC (permalink / raw)
  To: a.hindborg, alex.gaynor, ojeda
  Cc: aliceryhl, anna-maria, bjorn3_gh, boqun.feng, dakr, frederic,
	gary, jstultz, linux-kernel, lossin, lyude, rust-for-linux, sboyd,
	tglx, tmgross

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust: time: Seal the HrTimerMode trait
  2025-06-17 23:28 [PATCH] rust: time: Seal the HrTimerMode trait FUJITA Tomonori
@ 2025-06-19  0:28 ` Boqun Feng
  2025-06-30 12:07 ` Andreas Hindborg
  1 sibling, 0 replies; 3+ messages in thread
From: Boqun Feng @ 2025-06-19  0:28 UTC (permalink / raw)
  To: FUJITA Tomonori, Andreas Hindborg, alex.gaynor, Miguel Ojeda
  Cc: Alice Ryhl, Anna-Maria Gleixner, bjorn3_gh, Danilo Krummrich,
	Frederic Weisbecker, Gary Guo, John Stultz, linux-kernel, lossin,
	Lyude Paul, rust-for-linux, Stephen Boyd, Thomas Gleixner,
	Trevor Gross



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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust: time: Seal the HrTimerMode trait
  2025-06-17 23:28 [PATCH] rust: time: Seal the HrTimerMode trait FUJITA Tomonori
  2025-06-19  0:28 ` Boqun Feng
@ 2025-06-30 12:07 ` Andreas Hindborg
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Hindborg @ 2025-06-30 12:07 UTC (permalink / raw)
  To: alex.gaynor, ojeda, FUJITA Tomonori
  Cc: aliceryhl, anna-maria, bjorn3_gh, boqun.feng, dakr, frederic,
	gary, jstultz, linux-kernel, lossin, lyude, rust-for-linux, sboyd,
	tglx, tmgross


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>



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-30 12:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-17 23:28 [PATCH] rust: time: Seal the HrTimerMode trait FUJITA Tomonori
2025-06-19  0:28 ` Boqun Feng
2025-06-30 12:07 ` Andreas Hindborg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).