Re: [PATCH v6 1/2] rust: regulator: add a bare minimum regulator abstraction
From: Daniel Almeida
Date: Wed Jul 02 2025 - 09:15:44 EST
>
>>>> +impl<T: RegulatorState + 'static> Drop for Regulator<T> {
>>>> + fn drop(&mut self) {
>>>> + if core::any::TypeId::of::<T>() == core::any::TypeId::of::<Enabled>() {
>>>
>>> I would avoid this kind of logic. Instead, you can add an
>>> `disable_on_drop()` method or constant to the trait and check it here.
>>>
>>> Alice
>>>
>>
>> Can you expand on this?
>
> Along these lines:
>
> pub trait RegulatorState: 'static {
> const DISABLE_ON_DROP: bool;
> }
>
> impl RegulatorState for Enabled {
> const DISABLE_ON_DROP: bool = true;
> }
> impl RegulatorState for Disabled {
> const DISABLE_ON_DROP: bool = false;
> }
> impl RegulatorState for Dynamic {
> const DISABLE_ON_DROP: bool = false;
> }
>
> impl<T: RegulatorState> Drop for Regulator<T> {
> fn drop(&mut self) {
> if T::DISABLE_ON_DROP {
> unsafe { bindings::regulator_disable(self.inner.as_ptr()) };
> }
> unsafe { bindings::regulator_put(self.inner.as_ptr()) };
> }
> }
Ah, that indeed looks better, thanks.
— Daniel