Re: [PATCH RESEND v2] rust: regulator: add a bare minimum regulator abstraction
From: Daniel Almeida
Date: Thu Mar 27 2025 - 07:52:41 EST
Hi Mark,
> On 27 Mar 2025, at 08:32, Mark Brown <broonie@xxxxxxxxxx> wrote:
>
> On Wed, Mar 26, 2025 at 04:49:26PM -0300, Daniel Almeida wrote:
>>> On 26 Mar 2025, at 15:56, Mark Brown <broonie@xxxxxxxxxx> wrote:
>
>>>> + /// Disables the regulator.
>>>> + pub fn disable(self) -> Result<Regulator> {
>>>> + // Keep the count on `regulator_get()`.
>>>> + let regulator = ManuallyDrop::new(self);
>
>>> This looks like user code could manually call it which feels like asking
>>> for trouble?
>
>> Yes, user code can call this. My understanding is that drivers may want to
>> disable the regulator at runtime, possibly to save power when the device is
>> idle?
>
>> What trouble are you referring to?
>
> My understanding was that the enable was done by transforming a
> Regulator into an EnabledRegulator but if you can explicitly call
> disable() on an EnabledRegulator without destroying it then you've got
> an EnabledRegulator which isn't actually enabled. Perhaps it's not
> clear to me how the API should work?
No, you misunderstood a bit, but that’s on me, I should have included examples.
> +impl EnabledRegulator {
> + /// Disables the regulator.
> + pub fn disable(self) -> Result<Regulator>
disable() consumes EnabledRegulator to return Regulator.
Any function that takes 'self' by value (i.e.: “self" instead of “&self” )
effectively kills it. So, in that sense, disable() performs a conversion
between the two types after calling regulator_disable().
— Daniel