rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Benno Lossin" <lossin@kernel.org>
To: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Boris Brezillon" <boris.brezillon@collabora.com>,
	"Sebastian Reichel" <sebastian.reichel@collabora.com>,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"Mark Brown" <broonie@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v5 1/2] rust: regulator: add a bare minimum regulator abstraction
Date: Tue, 24 Jun 2025 21:30:42 +0200	[thread overview]
Message-ID: <DAV0H83BZO0K.376YKJOOGL48H@kernel.org> (raw)
In-Reply-To: <63C9EBE0-771F-411E-93E0-B22DFDCB960D@collabora.com>

On Tue Jun 24, 2025 at 7:31 PM CEST, Daniel Almeida wrote:
> 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().

Yeah that is correct.

> 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,

This will only work if the type of `inner` implements `Copy`. I forgot
if it does.

>                 _phantom: PhantomData,
>             })
>             .map_err(|error| Error {
>                 error,
>                 regulator: ManuallyDrop::into_inner(regulator),
>             })
>     }
> }
>
>
>
> Alex, Benno, thoughts?

Looks correct.

---
Cheers,
Benno

  reply	other threads:[~2025-06-24 19:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-23 18:21 [PATCH v5 0/2] Add a bare-minimum Regulator abstraction Daniel Almeida
2025-06-23 18:21 ` [PATCH v5 1/2] rust: regulator: add a bare minimum regulator abstraction Daniel Almeida
2025-06-24 17:31   ` Daniel Almeida
2025-06-24 19:30     ` Benno Lossin [this message]
2025-06-23 18:21 ` [PATCH v5 2/2] MAINAINTERS: add regulator.rs to the regulator API entry Daniel Almeida

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DAV0H83BZO0K.376YKJOOGL48H@kernel.org \
    --to=lossin@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=boris.brezillon@collabora.com \
    --cc=broonie@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sebastian.reichel@collabora.com \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).