[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <63C9EBE0-771F-411E-93E0-B22DFDCB960D@collabora.com>
Date: Tue, 24 Jun 2025 14:31:05 -0300
From: Daniel Almeida <daniel.almeida@...labora.com>
To: Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>,
Boris Brezillon <boris.brezillon@...labora.com>,
Sebastian Reichel <sebastian.reichel@...labora.com>,
Liam Girdwood <lgirdwood@...il.com>,
Mark Brown <broonie@...nel.org>
Cc: linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org
Subject: Re: [PATCH v5 1/2] rust: regulator: add a bare minimum regulator
abstraction
Hi,
> + /// Attempts to convert the regulator to an enabled state.
> + pub fn try_into_enabled(mut self) -> Result<Regulator<Enabled>, Error<Disabled>> {
> + self.enable_internal()
> + .map(|()| Regulator {
> + inner: self.inner,
> + _phantom: PhantomData,
> + })
> + .map_err(|error| Error {
> + error,
> + regulator: self,
> + })
> + }
> +}
> +
> +impl Regulator<Enabled> {
> + /// Obtains a [`Regulator`] instance from the system and enables it.
> + ///
> + /// This is equivalent to calling `regulator_get_enable()` in the C API.
> + pub fn get(dev: &Device, name: &CStr) -> Result<Self> {
> + Regulator::<Disabled>::get_internal(dev, name)?
> + .try_into_enabled()
> + .map_err(|error| error.error)
> + }
I just realized that this is a bug.
The pre-typestate code was using ManuallyDrop<T> here, and it was forgotten on
the newer versions. This means that the destructor for self runs here, which
decreases the refcount by calling regulator_put().
My proposed solution is:
/// Attempts to convert the regulator to an enabled state.
pub fn try_into_enabled(mut self) -> Result<Regulator<Enabled>, Error<Disabled>> {
// We will be transferring the ownership of our regulator_get() count to Regulator<Enabled>
let mut regulator = ManuallyDrop::new(self);
regulator.enable_internal()
.map(|()| Regulator {
inner: regulator.inner,
_phantom: PhantomData,
})
.map_err(|error| Error {
error,
regulator: ManuallyDrop::into_inner(regulator),
})
}
}
Alex, Benno, thoughts?
— Daniel
Powered by blists - more mailing lists