[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <D9ZPO3LUEPJL.1S7W35YQJA54O@nvidia.com>
Date: Mon, 19 May 2025 09:29:50 +0900
From: "Alexandre Courbot" <acourbot@...dia.com>
To: "Benno Lossin" <lossin@...nel.org>, "Daniel Almeida"
<daniel.almeida@...labora.com>, "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 v3] rust: regulator: add a bare minimum regulator
abstraction
On Sun May 18, 2025 at 11:05 PM JST, Benno Lossin wrote:
> On Sun May 18, 2025 at 1:12 PM CEST, Alexandre Courbot wrote:
>> On Sun May 18, 2025 at 6:57 PM JST, Benno Lossin wrote:
>>> So just let users ensure that they always match each `enable` call with
>>> a `disable` call in the `Dynamic` typestate?
>>>
>>> That is ok, if no memory issues can arise from forgetting to do so,
>>> otherwise those functions need to be `unsafe`.
>>
>> There shouldn't be any, the only side effect would be that the regulator
>> stays enabled when it shouldn't.
>>
>> It's also easy to implement more behaviors using more states. For
>> instance, `Dynamic` just proxies the C API. But if we also think it its
>> useful to have a regulator which use count is clamped to 0 and 1, you
>> could have another state that includes a boolean (instead of being empty
>> lke the others) to track whether the regulator is enabled or not, and an
>> `enable` method that only calls the C `regulator_enable` if that boolean
>> is not already true. That way you remove the need to mirror the calls to
>> enable and disable, while only paying the memory overhead for doing so
>> when you explicitly state you want this behavior.
>
> Aren't we then duplicating the refcount from the C side?
Not exactly, we are just tracking whether the consumer has already
called `regulator_enable` or not. Unfortunately `regulator_is_enabled`
appears to track the enabled state of the regulator device (not the
consumer reference), so it cannot be used for that.
Maybe a better way would be to have a Rust helper that returns the
`enable_count` of the regulator and check that instead. And to be clear
I am not saying this one is the policy we should follow - I just want to
illustrate the fact that the typestate pattern allows us to implement
different kinds of behaviors.
Powered by blists - more mailing lists