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

From: Daniel Almeida
Date: Wed Jul 30 2025 - 09:54:50 EST



[…]

> }
>
> - /// Enable the clock.
> + /// Attempts to convert the [`Clk`] to a [`Prepared`] state.
> ///
> - /// Equivalent to the kernel's [`clk_enable`] API.
> + /// Equivalent to the kernel's [`clk_prepare`] API.
> ///
> - /// [`clk_enable`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_enable
> + /// [`clk_prepare`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_prepare
> #[inline]
> - pub fn enable(&self) -> Result {
> - // SAFETY: By the type invariants, self.as_raw() is a valid argument for
> - // [`clk_enable`].
> - to_result(unsafe { bindings::clk_enable(self.as_raw()) })
> + pub fn prepare(self) -> Result<Clk<Prepared>, Error<Unprepared>> {
> + // We will be transferring the ownership of our `clk_get()` count to
> + // `Clk<Prepared>`.
> + let clk = ManuallyDrop::new(self);
> +
> + // SAFETY: By the type invariants, self.0 is a valid argument for [`clk_prepare`].

I just noticed that some comments still refer to the old “self.0” field, but that doesn’t exist anymore.

I’ll fix that in v2.

> + to_result(unsafe { bindings::clk_prepare(clk.as_raw()) })
> + .map(|()| Clk {
> + inner: clk.inner,
> + _phantom: PhantomData,
> + })
> + .map_err(|error| Error {
> + error,
> + clk: ManuallyDrop::into_inner(clk),
> + })
> }
> + }
>

— Daniel