Re: [PATCH] rust: clk: use the type-state pattern

From: Daniel Sedlak
Date: Wed Jul 30 2025 - 09:28:05 EST



On 7/30/25 2:59 PM, Daniel Almeida wrote:
[…]

We essentially would like to have a `#[sealed]` attribute that we can
put on a trait to avoid the `mod private { pub trait Sealed }` dance.
(so a trait that cannot be implemented outside of the module declaring
it)

---
Cheers,
Benno

This is not exactly what you said, but how about a declarative macro? e.g.:

macro_rules! sealed {
($($ty:ident),* $(,)?) => {
mod private {
pub trait Sealed {}
$(impl Sealed for super::$ty {})*
}
use private::Sealed;
};
}

sealed!(Unprepared, Prepared, Enabled)

Note that I am just brainstorming the general idea here, I did not test it yet.

I think that API-wise it would be better to have a proc-macro #[sealed], something similar to [1], as it may provide better error messages, when used incorrectly. So the outcome could look like.

#[sealed]
pub trait ClkState {

}

And then

#[sealed]
impl ClkState for XXX {

}

If you are interested, I can try to look into that.

Link: https://crates.io/crates/sealed [1]

Thanks!
Daniel